---
source: https://iinventedlight.com/library/notes/kubernetes-security-context.md
title: "Kubernetes securityContext - run as non-root, drop capabilities, read-only root filesystem"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC-BY-4.0 (Kubernetes docs); CC-BY-4.0 (kubernetes/website (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Configure a Security Context for a Pod or Container; kubernetes/website (GitHub), security-context.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-devops: kubernetes security context"
tags: [kubernetes, securitycontext, pods, capabilities, seccomp, hardening]
client: Librarian
sources:
  - id: S1
    title: "Configure a Security Context for a Pod or Container"
    publisher: Kubernetes docs
    url: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
    accessed: 2026-11-18
    license: "CC-BY-4.0"
  - id: S2
    title: "security-context.md (source page)"
    publisher: kubernetes/website (GitHub)
    url: https://github.com/kubernetes/website/blob/main/content/en/docs/tasks/configure-pod-container/security-context.md
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# Kubernetes securityContext - run as non-root, drop capabilities, read-only root filesystem
**Answer:** Include the `securityContext` field in the Pod specification for settings that apply to all Containers, and in a Container manifest for settings that apply only to that container. A security context defines privilege and access control settings for a Pod or Container.

## Example

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
    seccompProfile:
      type: RuntimeDefault
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: busybox:1.28
    command: ["sh", "-c", "sleep 1h"]
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities:
        drop: ["ALL"]
        add: ["NET_BIND_SERVICE"]
```

## Pod-level fields

- `runAsUser: 1000` makes all processes in every container run with user ID 1000.
- `runAsGroup` sets the primary group ID; if omitted, the primary group of the containers will be root(0).
- With `fsGroup: 2000`, processes are also part of supplementary group ID 2000, which owns the volume and files created in it.

## Container-level fields

- Container settings apply only to that container and override Pod-level settings when there is overlap. They do not affect the Pod's Volumes.
- `allowPrivilegeEscalation` controls whether a process can gain more privileges than its parent, for example through a setuid binary. If not defined, it defaults to true.
- `allowPrivilegeEscalation: false` cannot be combined with a privileged container or one with `CAP_SYS_ADMIN`.
- `readOnlyRootFilesystem` mounts the container's root filesystem as read-only.
- `capabilities` add or drop Linux capabilities, granting certain privileges without all the privileges of the root user.

## seccomp

`seccompProfile.type` can be `RuntimeDefault`, `Unconfined`, or `Localhost`; `localhostProfile` is only set with `Localhost` and is a path relative to the kubelet's seccomp directory.

## See also

- [kubernetes-pods](https://iinventedlight.com/library/notes/kubernetes-pods/)
- [kubernetes-rbac](https://iinventedlight.com/library/notes/kubernetes-rbac/)

## Claims

- id: C1
  claim: allowPrivilegeEscalation defaults to true when not defined.
  source: S2
  evidence: "then it will default to true."

- id: C2
  claim: Container security settings override Pod-level settings.
  source: S2
  evidence: "they override settings made at the Pod level when there is overlap."

- id: C3
  claim: readOnlyRootFilesystem mounts the root filesystem read-only.
  source: S1
  evidence: "Mounts the container's root filesystem as read-only."

## Sources

- [S1] Configure a Security Context for a Pod or Container. Kubernetes docs. https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ . License: CC-BY-4.0. Accessed 2026-11-18.

- [S2] security-context.md (source page). kubernetes/website (GitHub). https://github.com/kubernetes/website/blob/main/content/en/docs/tasks/configure-pod-container/security-context.md . License: CC-BY-4.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Kubernetes docs, Configure a Security Context for a Pod or Container; kubernetes/website (GitHub), security-context.md (source page). CC-BY-4.0 (https://raw.githubusercontent.com/kubernetes/website/main/LICENSE). Copyright The Kubernetes Authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
