Course contentsShow
← Cloud and Infrastructure
Lesson 1391 of 2,98928. Kubernetes SecurityFree lesson

Running as Non-Root Users

Configure containers to run as non-root users to limit privilege escalation and reduce attack surface.

Running as Non-Root Users

What you'll learn: How to configure containers to run as non-root users to minimize security risks in Kubernetes.

By default, many container images run processes as the root user (UID 0), which grants full administrative privileges inside the container. If an attacker exploits a vulnerability in your application, running as root gives them maximum power to escalate privileges, potentially compromising the entire node.

Why Non-Root Matters

Think of it like giving house keys versus visitor badges. Running as root is like giving every guest full house keys—they can access everything, change locks, and modify the foundation. Running as non-root is like giving a visitor badge with limited access to specific rooms.

When you configure containers to run as a non-root user, you create a security boundary. Even if the container is compromised, the attacker has limited capabilities and cannot perform privileged operations.

How It Works

You've already learned about Pod Security Context and Container Security Context. Now you apply those to enforce non-root execution:

apiVersion: v1
kind: Pod
metadata:
  name: secure-app
spec:
  securityContext:
    runAsUser: 1000
    runAsNonRoot: true
  containers:
  - name: app
    image: myapp:latest

The runAsUser: 1000 explicitly sets the user ID to 1000 (a non-root user). The runAsNonRoot: true flag tells Kubernetes to reject the pod if it tries to run as root—acting as a safety valve.

You can set these at the pod level (applies to all containers) or override at the container level for fine-grained control.

Key Takeaway: Running containers as non-root users dramatically reduces your attack surface by limiting what compromised processes can do, making privilege escalation significantly harder for attackers.