Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

Namespaces and cgroups

A container is just a process with namespaces that limit what it sees and cgroups that limit what it uses. Learn the kinds of each and how to inspect them.

A Linux container is not a special kind of object. It is an ordinary process (or group of processes) wrapped in two kernel features. Namespaces change what the process can see: its own process IDs, network interfaces, mount points, host name and users. Control groups (cgroups) limit what it can use: CPU time, memory, process count and I/O.

Container engines such as Docker, Podman and containerd add images, layered filesystems, networking glue and security profiles on top, but the isolation itself comes from these two features. Because the kernel is shared, containers start in milliseconds and cost far less than virtual machines, which run a whole separate kernel.

Terminal
ls -l /proc/$$/ns            # namespaces of the current process
cat /proc/self/cgroup        # its cgroup membership

Try it

Bash Namespaces of this shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash cgroup membership and limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Namespaces

Each namespace type isolates one global resource. A process belongs to one namespace of each type, and children inherit them unless told otherwise.

Namespace Isolates Effect
pid Process IDs The first process inside sees itself as PID 1; the host's processes are invisible.
net Network stack Own interfaces, routing table, firewall rules and ports.
mnt Mount points Own view of the filesystem tree (the basis of chroot-like roots).
uts Host and domain name A container can have its own host name.
ipc System V IPC and POSIX queues Separate shared memory and semaphores.
user User and group IDs UID 0 inside can map to an unprivileged user outside (rootless containers).
cgroup The cgroup root Hides the host's cgroup layout.
time Boot and monotonic clocks Different offsets for uptime (newer kernels).

The tools are unshare (start a command in new namespaces), nsenter (join the namespaces of an existing process) and lsns (list them). Creating most namespaces needs root privileges, except user namespaces, which unprivileged users can create on many systems.

cgroups

Control groups form a tree of groups; each process belongs to one group per hierarchy, and controllers apply limits to a group and everything beneath it. Modern systems use cgroup v2, a single unified hierarchy mounted at /sys/fs/cgroup. Typical controllers and files:

Controller Example file Purpose
cpu cpu.max, cpu.weight Cap or weight CPU time.
memory memory.max, memory.current Hard limit and current use; exceeding the limit triggers the OOM killer inside the group.
pids pids.max Limit the number of processes (stops fork bombs).
io io.max, io.weight Throttle block device I/O.

systemd places every service in its own cgroup, which is how systemctl status shows the processes of a unit and how MemoryMax= or CPUQuota= in a unit file work. See sandboxing.

flowchart TB
    K["Kernel (shared)"] --> NS["Namespaces: what a process sees"]
    K --> CG["cgroups: what a process may use"]
    NS --> C["Container = process + namespaces + cgroups + root filesystem"]
    CG --> C

A container combines isolation (namespaces) with limits (cgroups).

Usage notes

  • A container is not a security boundary by itself. The kernel is shared, so a kernel bug can cross the boundary. Engines add seccomp filters, capability dropping and AppArmor or SELinux profiles. Use virtual machines when you need stronger isolation.
  • Inside a container /proc shows only its PID namespace, because /proc is mounted per PID namespace. That is why ps in a container shows few processes.
  • PID 1 is special. The first process in a new PID namespace must reap orphans and handles signals differently; many images use a small init (tini, --init) for this.
  • Check from inside. /proc/1/cgroup and /.dockerenv hint at a container. The file /proc/self/status includes NSpid: lines when namespaces are nested.
  • Memory limits vs. what tools report. free often shows the host's memory; read memory.max and memory.current in the cgroup for the container's real limits.
  • A chroot is only the mnt part done crudely; it does not hide processes, networks or users.
  • Our runner is itself sandboxed with these features, so it can list its own namespaces but cannot create new ones.
  • Hands-on build. The lesson below creates a container by hand with unshare, chroot and cgroup files.