SecurityContext: runAsNonRoot, readOnlyRootFilesystem, Dropping Capabilities
Harden pods with non-root users, read-only roots, and dropped Linux capabilities.
In plain words
SecurityContext gives a worker a numbered helmet, removes dangerous tools, and locks the main toolbox while leaving one approved scratch desk. The simple Kubernetes rule is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
Think of it like…
SecurityContext gives a worker a numbered helmet, removes dangerous tools, and locks the main toolbox while leaving one approved scratch desk.
Where you’ve seen it
During an access review, the security engineer runs kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status. Expected evidence: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. If the run instead shows this symptom, stop and restore the last known state: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
Words you’ll hear
- runAsNonRoot
- For this lesson, runAsNonRoot is the controlling idea: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- readOnlyRootFilesystem
- readOnlyRootFilesystem is the checkpoint; run
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/statusand inspect the owner chain. An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. - Capabilities
- Capabilities is the outcome to preserve; the cost is explicit: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
- Verification evidence
- The evidence command is
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status. It connects the declaration to this behavior: Harden pods with non-root users, read-only roots, and dropped Linux capabilities.
Visual diagram
Theory & deep dive
How runAsNonRoot reaches the intended state
Harden pods with non-root users, read-only roots, and dropped Linux capabilities. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
The concrete checkpoint is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status; compare the runAsNonRoot owner, readOnlyRootFilesystem state, and Capabilities outcome instead of treating YAML acceptance as proof.
Run kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status after the change and correlate the result with Events and the owning object. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
readOnlyRootFilesystem: failure boundary and production cost
An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. The prerequisite contract is concrete: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files. A safe design limits blast radius, records the last good configuration, and uses kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status as a deterministic rollback checkpoint.
Capture the current state with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
How it works, step by step
Step 1 of 41. Model the trust boundary
Name the identity, action, resource, and trust boundary before choosing a control: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Goal
- Harden pods with non-root users, read-only roots, and dropped Linux capabilities.
- Owner
- runAsNonRoot
When to use it & trade-offs
What it is
Harden pods with non-root users, read-only roots, and dropped Linux capabilities. A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
Why it matters: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. The invariant at risk is specific: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. The quickest fact check before changing the design is kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.
When to reach for it
- Use runAsNonRoot to enforce this invariant: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Use readOnlyRootFilesystem evidence from
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/statusin reviews and incident timelines. Harden pods with non-root users, read-only roots, and dropped Linux capabilities. - Choose the hosted security service route only when this cost is justified by governance or scale: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
When to avoid or be careful
- Avoid assuming runAsNonRoot is complete after YAML submission; the required behavior is concrete: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/statusfirst: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. - Avoid adopting a heavier platform wrapper before measuring its cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
Native, manual, and managed paths for runAsNonRoot
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.Strengths: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
Watch out: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files. Recovery must also recognize this signal: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status; the expected invariant is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.Strengths: kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike runAsNonRoot, it does not make this symptom self-documenting: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
Watch out: The added platform ownership buys value at this cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Trace runAsNonRoot to Capabilities through readOnlyRootFilesystem; the invariant is: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Recognize the symptom: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
- Justify the cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
- Verify with kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status instead of only inspecting YAML.
Benchmarks & rules of thumb
- 0
- UID is unacceptable for runAsNonRoot workloads
- 3
- controls to inspect: identity, filesystem, and capabilities
- 1
- readOnlyRootFilesystem does not make mounted volumes immutable
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the runAsNonRoot owner contract: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities.
- Ignoring this telltale condition leads to unrelated changes: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling.
- Repeating
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/statuswithout an owner for remediation wastes retry cost. Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities?
- Which owner object, logs, and Events would you inspect for this symptom-An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling-?
- At what threshold would this cost become unacceptable: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files?
kubectl get pod api-0 -o jsonpath='{.spec.containers[0].securityContext}'; kubectl debug api-0 --image=busybox -- cat /proc/self/status, name the failure, then justify the cost. Invariant: A Pod SecurityContext or container SecurityContext can runAsNonRoot, force a UID/GID, use a readOnlyRootFilesystem, disallow privilege escalation, and drop unneeded Linux capabilities. Failure: An image whose numeric user is absent from passwd or whose application writes to root can fail with runAsNonRoot or a read-only root, and dropping NET_RAW can break health or packet tooling. Cost: Hardening removes common breakout paths, but writable scratch volumes and exact application permissions must replace assumptions about mutable root files.Knowledge check
Put it into practice
Practice related interview Q&A
Apply SecurityContext: runAsNonRoot, readOnlyRootFilesystem, Dropping Capabilities with generative AI interview Q&A and hands-on practice.