Skip to content
Tutorials Center
My learning

Topics

Lesson 88 of 95 ยท System Administration

Namespaces, cgroups and chroot

See what a container really is on Linux: namespaces isolate what a process sees, cgroups limit what it uses, and chroot changes its root. Includes hands-on /proc exploration.

A Docker container looks like a tiny virtual machine, but it is not one. There is no second kernel and no hardware emulation. A container is an ordinary Linux process (or a few of them) that the kernel has been told to treat specially: it sees its own process list, its own network, its own file tree, and it may only use a fixed amount of memory and CPU. Three kernel features make that possible, and each one is visible from the command line.

This lesson takes the mystery out of containers by looking at those features one by one: namespaces (what a process can see), cgroups (how much it can use) and chroot (which directory is its /). Our sandbox is itself locked down, so creating new namespaces is refused, but every process on Linux already lives in a namespace, and you can inspect that through /proc. The commands that need root are read-only examples. It builds on Processes, Links and The Linux Filesystem Layout.

flowchart TB
  K["One shared Linux kernel"]
  K --> C1["Container A<br/>own PID, net, mount, UTS namespaces<br/>cgroup: 256 MB, 50% CPU"]
  K --> C2["Container B<br/>own PID, net, mount, UTS namespaces<br/>cgroup: 1 GB"]
  K --> H["Host processes<br/>(initial namespaces)"]

Containers share the host kernel; each one gets private views (namespaces) and resource limits (cgroups).

Namespaces: a private view of the system

A namespace wraps a global resource so that processes inside see their own copy. Linux has several kinds:

Namespace Isolates Effect inside
pid Process IDs The first process is PID 1; the host's processes are invisible
net Network devices, routes, ports Its own lo, eth0, firewall rules, port 80
mnt Mount points Its own file tree and mounts
uts Hostname and domain name hostname can differ from the host
ipc Shared memory, message queues No cross-talk with the host
user User and group IDs Root inside can be an ordinary user outside
cgroup The view of the cgroup tree Its own cgroup root
time Boot and monotonic clocks A different uptime

Every process points to one namespace of each kind, and the kernel exposes those pointers as symbolic links in /proc/PID/ns/. The link target contains an inode number that identifies the namespace. Two processes are in the same namespace exactly when those numbers are equal.

Bash Who shares a namespace with me?

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.

A new namespace is created with unshare (run a command in new namespaces), nsenter (join the namespaces of an existing process) or the clone/unshare system calls that container runtimes use. Typical experiments on a real machine:

CONSOLE
$ sudo unshare --uts --fork bash          # new hostname namespace
# hostname demo-box                       # changes only inside this shell
# hostname
demo-box
# exit
$ hostname                                # the host is untouched

$ sudo unshare --pid --fork --mount-proc bash
# ps aux                                  # only a handful of processes
# echo $$
1                                         # this shell is PID 1 in its namespace

$ sudo nsenter --target 4242 --net ip addr   # run 'ip addr' inside the network namespace of PID 4242

Read-only: the sandbox forbids new namespaces, and the error is the same one you would see without sufficient privileges:

Bash What a refusal looks like

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.

A user namespace is the exception that does not require root on many distributions (unshare --user --map-root-user): you appear as UID 0 inside, while the kernel still treats you as yourself outside. This is how rootless containers work. It needs unprivileged user namespaces to be enabled, which is why a hardened host can refuse it.

cgroups: limits and accounting

Namespaces hide things; they do not stop a process from eating all the memory. Control groups (cgroups) put processes into a tree of groups, and each group can have limits and counters for CPU, memory, I/O and the number of processes. Modern systems use cgroup v2, one unified tree mounted at /sys/fs/cgroup. A group is a directory, and its settings are files you read and write:

File Meaning
cgroup.procs PIDs in this group (write a PID to move it in)
memory.max Hard memory limit in bytes, or max for none
memory.current Memory used right now
cpu.max QUOTA PERIOD in microseconds, e.g. 50000 100000 = half a CPU
pids.max Maximum number of processes

If a group goes over memory.max, the kernel's OOM killer picks a victim inside that group, so one runaway container does not take down the host. Which group a process belongs to is shown in /proc/PID/cgroup:

CONSOLE
$ cat /proc/self/cgroup
0::/user.slice/user-1000.slice/session-3.scope

Creating a limited group by hand needs root:

CONSOLE
$ sudo mkdir /sys/fs/cgroup/demo
$ echo 100000000 | sudo tee /sys/fs/cgroup/demo/memory.max       # 100 MB
$ echo "50000 100000" | sudo tee /sys/fs/cgroup/demo/cpu.max     # 50% of one CPU
$ echo $$ | sudo tee /sys/fs/cgroup/demo/cgroup.procs            # move this shell in

In practice you rarely do this. systemd manages cgroups for every service (see Packages, Services and systemd); systemd-run --scope -p MemoryMax=200M mycommand runs a command with a limit, and systemd-cgtop shows usage per group. Docker's --memory=256m and --cpus=0.5 simply set these same files.

The files are plain text, so reading a limit is a one-liner. A small helper that converts cgroup values into human units, tested on sample data:

Bash Reading cgroup-style settings

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.

chroot: a different root directory

chroot is the oldest of the three. It changes what / means for a process: after chroot /srv/jail /bin/sh, the shell cannot name anything outside /srv/jail. It is a file view, not a security boundary: a process with root privileges inside can often escape, and it does nothing about processes, networking or memory. Container runtimes therefore use pivot_root inside a mount namespace, which is the robust version of the same idea.

The directory you point chroot at must contain everything the program needs: the program itself, its shared libraries, and any files it opens. That is why a container image is, at heart, a root filesystem. You can discover a program's libraries with ldd:

CONSOLE
$ ldd /bin/ls
        linux-vdso.so.1
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
        /lib64/ld-linux-x86-64.so.2

Building a minimal jail copies the binary and each library to the same relative path:

CONSOLE
$ mkdir -p jail/bin jail/lib/x86_64-linux-gnu jail/lib64
$ cp /bin/ls jail/bin/
$ # copy every library that ldd listed, keeping its path ...
$ sudo chroot jail /bin/ls /
bin  lib  lib64

The runnable version below builds the tree and verifies it contains the right files, but stops short of chroot, because that call needs root. The script uses ldd and cp --parents to preserve paths.

Bash Assemble a mini root filesystem

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.

Putting the three together

A container runtime does roughly this, in order:

  1. Create new pid, net, mnt, uts, ipc (and often user) namespaces with clone.
  2. Unpack the image layers into a directory and make it the new root (pivot_root in the mount namespace).
  3. Put the process into a fresh cgroup with the requested limits.
  4. Drop capabilities and apply a syscall filter (seccomp) to reduce what root inside can do.
  5. exec the container's command as PID 1.

That is why containers start in milliseconds: nothing boots. It is also why a container is not a strong security boundary on its own: all of them share one kernel. The Docker tutorial builds on these ideas for images, networks and volumes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a PID namespace change?

    A PID namespace gives processes their own numbering and hides outside processes. Counting processes is a cgroup job (pids.max) and memory is limited by cgroups too.
  2. 2. Which file sets a hard memory limit for a cgroup v2 group?

    memory.max is the limit in bytes. memory.current only reports usage, cpu.max limits CPU time and cgroup.procs lists the member PIDs.
  3. 3. How can you tell that two processes share the same network namespace?

    Each namespace has an inode number shown in the link target (such as net:[4026531840]). Equal numbers mean the same namespace.
  4. 4. Why is chroot alone not enough to build a secure container?

    chroot is a file view. Real containers add namespaces, cgroups, dropped capabilities and seccomp filters.

Summary

  • A container is a normal process with a restricted view (namespaces), limited resources (cgroups) and its own root directory (chroot/pivot_root). There is one shared kernel.
  • Namespace kinds: pid, net, mnt, uts, ipc, user, cgroup, time. Compare /proc/PID/ns/* link targets to see who shares what.
  • unshare creates namespaces, nsenter joins existing ones; user namespaces enable rootless containers.
  • cgroup v2 is a directory tree under /sys/fs/cgroup; files such as memory.max, cpu.max and pids.max set limits, and systemd manages them for services.
  • A jail needs the program and its libraries (ldd lists them); chroot is a view, not a security boundary.

Next: Setting Up a Web Server: nginx, TLS, Let's Encrypt