Linux namespaces and cgroups are the foundation of container isolation. This deep dive explains how each namespace type works, how cgroups control resources, and how Docker, containerd, and Kubernetes use them to create secure multi-tenant environments.
Why Namespaces and Cgroups Matter
Containers are not virtual machines. They share the host kernel but provide isolation through two Linux kernel features:
- Namespaces – Isolate what processes can see (PIDs, network, filesystem, users, etc.)
- Cgroups (Control Groups) – Limit what processes can use (CPU, memory, I/O, devices)
Understanding these internals is essential for debugging container issues, designing secure systems, and passing security audits (CIS, PCI-DSS, SOC2).
Linux Namespaces: The 8 Types
As of Linux 6.x, there are 8 namespace types. Each wraps a global system resource into an isolated instance.
| Namespace | Constant | Isolates | Since |
|---|---|---|---|
| PID | CLONE_NEWPID | Process ID numbers | Linux 2.6.24 |
| Network | CLONE_NEWNET | Network interfaces, routing, firewall rules | Linux 2.6.29 |
| Mount | CLONE_NEWNS | Filesystem mount points | Linux 2.4.19 |
| UTS | CLONE_NEWUTS | Hostname and domain name | Linux 2.6.19 |
| IPC | CLONE_NEWIPC | System V IPC, POSIX message queues | Linux 2.6.19 |
| User | CLONE_NEWUSER | User and group IDs | Linux 3.8 |
| Cgroup | CLONE_NEWCGROUP | Cgroup root directory | Linux 4.6 |
| Time | CLONE_NEWTIME | CLOCK_MONOTONIC, CLOCK_BOOTTIME | Linux 5.6 |
Exploring Namespaces Hands-On
1. PID Namespace – Process Isolation
# Create a new PID namespace
sudo unshare --pid --fork --mount-proc /bin/bash
# Inside the namespace - PID 1!
echo $$
# Output: 1
# Outside the namespace
ps aux | grep bash
# Shows actual PID (e.g., 12456)
Processes inside see only themselves and their children. The first process becomes PID 1 (init) and adopts orphaned children.
2. Network Namespace – Network Stack Isolation
# Create network namespace
sudo ip netns add container-net
# List namespaces
sudo ip netns list
# Execute commands in namespace
sudo ip netns exec container-net ip link show
sudo ip netns exec container-net ip addr add 10.0.0.1/24 dev lo
# Connect namespaces with veth pair
sudo ip link add veth0 type veth peer name veth1
sudo ip link set veth1 netns container-net
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip netns exec container-net ip addr add 10.0.0.2/24 dev veth1
sudo ip link set veth0 up
sudo ip netns exec container-net ip link set veth1 up
Each network namespace has its own: interfaces, routing tables, iptables rules, /proc/net/, sockets.
3. Mount Namespace – Filesystem Isolation
# Create mount namespace
sudo unshare --mount /bin/bash
# Mount something visible only here
mount -t tmpfs tmpfs /mnt/secret
echo "hidden data" > /mnt/secret/data.txt
# In another terminal - not visible!
cat /mnt/secret/data.txt
# No such file or directory
Mount propagation types control how mounts propagate between namespaces: shared, slave, private, unbindable.
4. User Namespace – UID/GID Mapping
# Create user namespace - root inside maps to non-root outside
unshare --user --map-root-user /bin/bash
# Inside: uid 0 (root)
id
# uid=0(root) gid=0(root) groups=0(root)
# Outside: actual user
ps -o uid,pid,cmd -C bash
# 1000 12456 /bin/bash
# /etc/subuid and /etc/subgid define mapping ranges
# Format: username:start_id:count
# Example: ubuntu:100000:65536
# Allows root inside container to map to unprivileged user outside
# Critical for rootless containers (Podman, rootless Docker)
5. UTS Namespace – Hostname Isolation
sudo unshare --uts /bin/bash
hostname container-host
hostname
# container-host
# Outside - unchanged
hostname
# host-machine
6. IPC Namespace – Inter-Process Communication Isolation
sudo unshare --ipc /bin/bash
# Separate System V semaphores, shared memory, message queues
ipcs -m # Shows only this namespace's shared memory
7. Cgroup Namespace – Cgroup Hierarchy Isolation
sudo unshare --cgroup /bin/bash
cat /proc/self/cgroup
# Shows only subtree from namespace root
Prevents processes from seeing host cgroup hierarchy. Important for container security.
8. Time Namespace – Time Isolation
# New in Linux 5.6
sudo unshare --time /bin/bash
# Can set different CLOCK_MONOTONIC and CLOCK_BOOTTIME offsets
# Useful for container migration, testing time-sensitive apps
Cgroups: Resource Control
Cgroup v1 vs v2
| Feature | v1 | v2 (Unified) |
|---|---|---|
| Hierarchy | Multiple (per controller) | Single unified hierarchy |
| Controllers | Mounted separately | All in one hierarchy |
| Thread granularity | Process-level only | Thread-level (cgroup.subtree_control) |
| Default in | Older distros | systemd 232+, kernel 4.5+ |
Key Controllers
| Controller | File | Controls |
|---|---|---|
| cpu | cpu.max | CPU quota (period/quota) and weight |
| memory | memory.max | Memory limit, swap limit, OOM behavior |
| io | io.max | I/O bandwidth/IOPS limits per device |
| pids | pids.max | Maximum number of processes/threads |
| cpuset | cpuset.cpus | CPU/core assignment, memory nodes |
| hugetlb | hugetlb..max | Huge page limits |
Hands-On Cgroup v2
# Check cgroup version
stat -fc %T /sys/fs/cgroup/
# cgroup2fs = v2
# Create a cgroup
sudo mkdir /sys/fs/fs/cgroup/mycontainer
# Or use systemd-run (recommended)
systemd-run --scope --user -p MemoryMax=500M -p CPUQuota=50% /bin/bash
# Inside the cgroup - check limits
cat /sys/fs/cgroup/mycontainer/memory.max
# 524288000 (500M)
# Set CPU limit: 50% of one CPU (quota/period)
echo "50000 100000" | sudo tee /sys/fs/cgroup/mycontainer/cpu.max
# Add process to cgroup
echo $$ | sudo tee /sys/fs/cgroup/mycontainer/cgroup.procs
Memory Controller Details
# Key memory files
memory.current # Current usage
memory.max # Hard limit (OOM kill if exceeded)
memory.high # Soft limit (throttle/reclaim)
memory.low # Protection from reclaim
memory.swap.max # Swap limit
memory.oom_group # Kill entire group on OOM
CPU Controller Details
# cpu.max = "quota period" (microseconds)
# 100% of 1 CPU = "100000 100000"
# 50% of 1 CPU = "50000 100000"
# 2 CPUs = "200000 100000"
# cpu.weight = 1-10000 (default 100) - relative share when contention
echo 200 | sudo tee /sys/fs/cgroup/mycontainer/cpu.weight
How Docker Uses Namespaces and Cgroups
Container Creation Flow
docker run -d --name web nginx
# 1. Client sends request to dockerd
# 2. dockerd calls containerd via CRI
# 3. containerd creates shim process
# 4. shim calls runc with spec
# 5. runc creates namespaces + cgroups + pivots root
# 6. runc execs container process (PID 1 in namespace)
Default Namespace Isolation (Docker)
| Namespace | Isolated | Note |
|---|---|---|
| PID | ✅ Yes | –pid=host to disable |
| Network | ✅ Yes | –network=host to disable |
| Mount | ✅ Yes | Private by default |
| UTS | ✅ Yes | Container gets hostname |
| IPC | ✅ Yes | –ipc=host to disable |
| User | ❌ No (default) | –userns-remap or –user to enable |
| Cgroup | ✅ Yes | v2 with systemd |
| Time | ❌ No | Not isolated by default |
Default Cgroup Limits (Docker)
# No limits by default! Container can use all host resources.
# Set limits at runtime:
docker run -d
--memory=512m
--memory-swap=1g
--cpus=1.5
--pids-limit=100
--oom-kill-disable=false
nginx
# Or update running container:
docker update --memory=1g --cpus=2 web
Kubernetes and CRI-O/containerd
Pod-Level Cgroups
# Kubernetes creates cgroup hierarchy:
/kubepods/
├── besteffort/
│ └── podUID/
│ └── containerID/
├── burstable/
│ └── podUID/
│ └── containerID/
└── guaranteed/
└── podUID/
└── containerID/
# QoS class determined by requests/limits:
# Guaranteed: requests = limits for CPU and memory
# Burstable: requests < limits, or only one set
# BestEffort: no requests or limits
Namespace Sharing in Pods
Containers in a pod share:
- Network namespace (same localhost, same ports)
- IPC namespace (can use shared memory)
- UTS namespace (same hostname)
- Optionally: PID namespace (shareProcessNamespace: true)
Each container gets its own:
- Mount namespace
- Cgroup (under pod cgroup)
Security Implications
Namespace Escapes
If a process can enter another namespace, it can escape:
# Dangerous: entering host namespaces
nsenter -t 1 -m -u -n -i -p # Enters ALL host namespaces as PID 1
# Mitigation:
# - Drop CAP_SYS_ADMIN (required for unshare/nsenter)
# - Use user namespaces (root inside != root outside)
# - Seccomp filter blocking unshare, setns, nsenter
# - Run containers rootless
Cgroup Escape (Release Agent)
# Cgroup v1 release_agent exploit (patched in v2)
# If you can write to cgroup.notify_on_release and cgroup.release_agent
# You can execute arbitrary commands as host root when last process exits
# Mitigation: Use cgroup v2 (no release_agent), run rootless
Debugging Commands
# List all namespaces for a process
ls -la /proc//ns/
# Enter a namespace
nsenter -t -n # Network namespace
nsenter -t -m # Mount namespace
nsenter -t -a # All namespaces
# Check cgroup membership
cat /proc//cgroup
# View cgroup hierarchy
systemd-cgls
# Check container runtime cgroups
crictl stats # For containerd/CRI-O
# Docker-specific
docker inspect --format '{{.State.Pid}}'
docker exec cat /proc/self/cgroup
Rootless Containers
# Rootless Docker
dockerd-rootless-setuptool.sh install
# Uses user namespaces + cgroup v2 delegation
# Podman (rootless by default)
podman run -d nginx
# Check user namespace mapping
cat /proc//uid_map
# 0 100000 65536 -> container root (0) maps to host 100000
Verification Checklist
- [ ] Run
unshare --pid --fork --mount-proc– verify PID 1 isolation - [ ] Create network namespace – verify separate interfaces with
ip netns - [ ] Test mount namespace – mount tmpfs, verify host can’t see it
- [ ] Create user namespace – verify UID mapping with
cat /proc/self/uid_map - [ ] Set cgroup memory limit – verify OOM kill at limit
- [ ] Set cgroup CPU quota – verify throttling with
stress-ng --cpu 2 - [ ] Run rootless container – verify no host root privileges
- [ ] Check
docker inspectfor namespace/cgroup config
Related Reading
For Kubernetes security hardening using these primitives, see Kubernetes Security Hardening: CIS Benchmarks Guide. For systemd service hardening directives that control namespaces and cgroups, see systemd Service Hardening: A Directive Reference.