Resource isolation — study guide
The concept's fragments, read in order.
For your capstone A personal cloud you can destroy and rebuild
Several of your homelab's services share one kernel, and namespaces and cgroups keep them from treading on each other: the media server cannot see the notes app's processes, and a backup job cannot take the CPU the media server needs.
Say the media server and the nightly backup share a host. You give the media group a cpu.weight of 300 and the backup group 100. When both are busy, the media server gets 300 / 400, three quarters of the CPU. When nobody is watching anything, the backup takes the whole machine, because weights hold nothing back from a group that wants it. Write 50000 100000 into the backup's cpu.max instead, and it crawls along at half a CPU even on an idle box.
Walls inside one kernel
On Linux you can run a program so that it believes it has the machine to itself: its own list of processes, its own hostname, its own network, its own idea of who root is. Nothing is being faked at the hardware level to pull this off. There is still one kernel, the same one running everything else on the box, and the program is still an ordinary process with a PID. The kernel simply shows it a different picture and holds it to a budget.
That splits isolation into two separate questions. The first is what a process can see, and namespaces answer it: each namespace gives the processes inside it their own private copy of one kind of system resource, such as process IDs or the hostname. The second is how much a process can use, and control groups (cgroups) answer it: a cgroup caps or shares out CPU time, memory, disk I/O and the number of processes a group may run. A process can be walled off in either sense without the other, and full isolation needs both.
It works like an apartment building, where every unit has its own walls and its own door number but all of them stand on one shared foundation and share the same plumbing and wiring. There is one foundation and one set of pipes, and every unit sits on them, but each unit has its own walls and its own door number. The alternative, a virtual machine, is more like building a separate house with its own foundation: a guest operating system with a kernel of its own, running on hardware that a hypervisor pretends to provide. Namespaces and cgroups skip all of that, which is why they are cheap, and share the foundation, which is why their walls are thinner.
Changing what a process can see
A namespace wraps one kind of global system resource so that the processes inside it appear to have their own isolated instance of it. Change the resource from inside, and only the other members of that namespace see the change; everyone else keeps their own view. Give a group of processes their own UTS namespace, for example, and a hostname set inside it is the hostname only inside it; the rest of the machine never notices.
Membership works per type. Every process is a member of exactly one namespace of each kind at all times, and processes that nobody has deliberately isolated all share the same set, which is why an ordinary shell sees the whole machine. A new process starts in its parent's namespaces unless it is created with a flag asking for a fresh one, so isolation is inherited down the family tree: put one process in a new namespace and every child it starts lands there too. The kernel offers three system calls for this, clone to start a process in new namespaces, unshare to move the caller into new ones, and setns to join a namespace that already exists.
You can read a process's memberships directly. The directory /proc/PID/ns holds one link per namespace type (plus pid_for_children and time_for_children, which say where the process's future children will land), and each link reads as the type and a number, such as uts:[4026531838]. The number names the namespace instance, so two processes whose links show the same number are in the same namespace, and different numbers mean different walls. ls -l /proc/self/ns shows your own; lsns lists the namespaces you are allowed to see and how many processes share each.
A namespace lasts as long as something is in it. When the last member process exits, the kernel tears it down, unless something else is holding it open, such as an open file handle to its /proc link.
Eight kinds of private view
The Linux namespaces documentation lists eight types, and each one hides a different slice of the machine. A mount namespace gives a process its own list of mount points, so it can have a different set of filesystems attached and a different idea of what lives where. A PID namespace gives it its own numbering of process IDs. A network namespace gives it its own network devices, addresses, routing and ports, so two programs in different network namespaces can each listen on port 80 without colliding. A UTS namespace gives it its own hostname.
The other four are quieter. An IPC namespace separates the older inter-process messaging channels (System V IPC objects and POSIX message queues), so processes cannot reach each other through them. A user namespace gives a process its own set of user and group IDs, which is how a process can be root inside without being root on the host. A cgroup namespace changes where the cgroup tree appears to start, so a process sees its own control group as the root rather than the full path above it. A time namespace gives a process its own offsets for the boot-time and monotonic clocks, the clocks that measure how long the system has been up.
What no namespace hides is the kernel itself. Every namespaced process still makes its system calls into the one shared kernel, and some things belong to that kernel alone: a time namespace does not cover the real calendar clock, and changing the system time or loading a kernel module can only be done with privilege in the machine's initial user namespace. The walls are views over shared machinery, not separate machinery.
PID 1 in a world of its own
A PID namespace gives its processes their own numbering, starting again from the bottom. The first process created in a new PID namespace gets PID 1, the number the first process at boot normally holds, and the processes it starts are numbered 2, 3 and so on, as if the machine had just come up. From inside, ps (once a fresh /proc is mounted to match) lists only those few processes.
The same process still has a PID in the namespace that created it, and that number is ordinary. A process has one PID in each layer of namespaces in which it is visible, so the program that believes it is PID 1 might be PID 4127 to the host. Visibility runs one way, like a one-way mirror between two rooms: people on the outer side can watch everything in the inner room, while people inside see only their own room: processes in a parent namespace can see and number everything in the child, and signal it subject to the usual permission checks, while processes in the child cannot see anything outside it. That is why you can stop an isolated program from the host by its outside PID, and why it cannot do the same to you.
Being PID 1 inside comes with duties. When a process in the namespace exits and leaves children behind, those orphans are handed to the namespace's PID 1 for adoption, the job the boot-time first process does for the whole machine. PID 1 is also protected: other processes in its own namespace can deliver only the signals it has installed a handler for, though SIGKILL from a parent namespace still gets through. And it is load-bearing. If PID 1 of a namespace exits, the kernel kills every remaining process in that namespace with SIGKILL, so the first process's life sets the life of everything inside.
Changing how much a process can use
A control group, or cgroup, is a set of processes the kernel accounts for and limits together. The groups form a tree, and the kernel shows that tree as an ordinary-looking directory structure, usually mounted at /sys/fs/cgroup: each directory is a group, and the files inside it are its settings and its live counters. Every process belongs to exactly one cgroup, and a new process starts in the group its parent was in at the time, so a limit placed on a group covers everything that group's processes go on to start.
The limiting itself is done by controllers, one per kind of resource. The cpu controller shares out and caps processor time, memory caps memory use, io shares out and caps disk reads and writes, and pids caps how many processes the group may hold. A controller's settings are files in the group's directory, such as cpu.max, memory.max and pids.max, and the file cgroup.controllers lists which controllers are available there. Limits nest: whatever a child group's own settings say, it cannot escape its parent's limits, so a cap set near the root binds everything beneath.
There are two versions of this interface, and both still exist. The original, cgroup v1, let each controller have its own separate tree. Its replacement, cgroup v2, has a single unified tree that all of its controllers share, and it is what this concept describes. Current releases of the major distributions (Debian, Ubuntu, Fedora, RHEL and others) boot with cgroup v2, and recent versions of systemd, the program that manages services on those distributions, have dropped v1 support altogether. Older machines may still run v1 or a hybrid of the two, where the file names and layout differ.
You can check which you have. cat /proc/self/cgroup prints a single line beginning 0:: followed by the group's path on a v2 machine, and several numbered lines, one per v1 tree, on a v1 machine (a hybrid machine shows both kinds). stat -fc %T /sys/fs/cgroup prints cgroup2fs for v2 and tmpfs for v1 or hybrid.
CPU limits, a quota per period or a share of the contest
cgroup v2 offers two different CPU controls, and they answer different questions. The hard cap is cpu.max, which holds two numbers in microseconds: a quota and a period. The group may use up to the quota of CPU time in each period, and the default is max 100000, meaning no quota over a 100 ms period. Write 50000 100000 and the group gets 50 ms of CPU time per 100 ms, which is half of one CPU: 50000 / 100000 = 0.5. The quota may be larger than the period on a machine with several cores, so 200000 100000 allows two CPUs' worth: 200000 / 100000 = 2.
What happens at the cap is throttling, like a tap metered to a fixed amount per interval: once this interval's allowance has run out, you wait for the next interval to start drawing again, slowed down but never cut off. Once the group has spent its quota, its threads are not allowed to run again until the next period begins and the quota refills. A single busy thread under 50000 100000 runs for 50 ms, waits 50 ms, and repeats. The quota counts CPU time across all cores, not wall-clock time, so two busy threads on two cores spend the same 50 ms in 25 ms (2 x 25 = 50) and then both sit idle for the remaining 75 ms of the period. Nothing is killed and nothing errors; the work just takes longer, and the group's cpu.stat file counts in how many periods it was throttled (nr_throttled) and for how long (throttled_usec).
The other control, cpu.weight, is not a cap at all but a share. Weights run from 1 to 10000 with a default of 100, and they only matter when groups are competing for the same CPU: each busy group gets the fraction its weight is of the total. With weights 100 and 300 and both groups busy, the split is 100 / 400 = 1/4 and 300 / 400 = 3/4. If the heavier group goes idle, the lighter one may use everything, because weights are work-conserving: they do not hold CPU time back from a group that wants it while the others are idle. That is the difference worth holding onto: a quota slows a group down even on an idle machine, and a weight only decides how a busy machine is split.
Memory, the limit that kills
CPU time can be handed out later, so a CPU limit only makes a group wait. Memory cannot be postponed the same way: a program that needs another page needs it now. That is why the cgroup v2 memory controller has two lines with very different consequences. The soft one is memory.high. When a group goes over it, the kernel throttles the group's processes and puts them under heavy reclaim pressure, pushing out pages that can be dropped or written elsewhere, but going over memory.high never invokes the out-of-memory killer. The group slows down instead.
The hard line is memory.max. If a group's usage reaches it and the kernel cannot reclaim enough to get back under, the out-of-memory (OOM) killer is invoked in that cgroup: it picks a process inside the group and ends it with SIGKILL, the signal that cannot be caught, so the victim gets no chance to clean up. The important word is inside. The kernel documentation is explicit that an OOM kill invoked in a cgroup will not kill any task outside it, so the group that overran pays for it while its neighbors carry on untouched. Setting memory.oom.group to 1 changes the choice from one victim to all of them: the group's processes are killed together or not at all, for workloads that make no sense half-alive.
A third limit catches a different runaway. pids.max caps how many processes the group may hold, and once the group is at that number, any attempt to start another (a fork or clone) simply fails with an error, EAGAIN, rather than killing anything. A program that forks out of control hits that wall and stops growing, and the rest of the machine keeps room to start its own.
Put side by side, the limits fail in different ways, and the symptom tells you which you hit: a CPU limit looks like slowness, memory.high looks like slowness under memory pressure, memory.max looks like a process that vanished, and pids.max looks like a program complaining it cannot start a child. Each group's memory.events file counts the OOM kills that happened inside it, under oom_kill.
Root inside, nobody special outside
A user namespace gives a process its own set of user and group IDs, and the interesting consequence is that a process can be user ID 0, root, inside the namespace while being an ordinary unprivileged user outside it. Since Linux 3.8 the kernel has let unprivileged users create one, and the process that does starts with a full set of capabilities, the kernel's individual privileges, inside the new namespace and none at all in the one it came from. Some distributions narrow this by policy; Ubuntu 24.04, for one, lets unprivileged programs create the namespace but denies them those capabilities unless an AppArmor profile allows it.
What connects the two worlds is a mapping, which you can read in /proc/PID/uid_map. Each line holds three numbers: where a range of IDs starts inside, where it starts outside, and how long the range is. The line 0 1000 1 says that ID 0 inside is ID 1000 outside, a range of one. On a machine's initial namespace the file reads 0 0 4294967295, everything mapped to itself, which is the baseline every other namespace changes. Rootless podman builds on exactly this: podman unshare cat /proc/self/uid_map shows your own user mapped to root, followed by a block of extra IDs your account has been granted in /etc/subuid, so a rootless container can have a root and other users of its own while every one of them is, to the host, just a numbered ID your account was lent, and none of them is the host's root.
So what does root inside actually get? The capabilities it holds only work on resources that its own user namespace owns: it can change the hostname of a UTS namespace it created, mount some kinds of filesystem in its own mount namespace, and manage processes in its own PID namespace. It works like an actor playing a king, whose commands are obeyed by everyone on the stage but carry no weight once anyone steps off it. Whenever such a process touches a file on the host, the kernel checks permissions using the mapped outside ID, so root inside asking to read a host file only root may read gets Permission denied, exactly as user 1000 would. Host files owned by IDs that have no mapping appear inside as the overflow ID, 65534, usually shown as nobody. And some powers belong only to the machine's initial namespace, such as changing the system clock or loading a kernel module, so no amount of root inside reaches them.
Lighter than a VM, and a thinner wall
Namespaces and cgroups are cheap because they add almost nothing. An isolated process is still an ordinary process making ordinary system calls into the kernel that is already running; it just carries different namespace memberships and sits in a different cgroup. Nothing has to boot, so starting one is about as fast as starting any program, and no memory is set aside for a second operating system. A virtual machine pays for all of that: it runs a complete guest operating system, kernel included, on virtual hardware that a hypervisor provides, and that guest has to boot and occupy memory before your program even starts.
The same sharing is what makes the wall thinner. Every isolated process on the host talks to one kernel, and that kernel is the thing enforcing the walls. If an attacker inside one namespace finds a bug in any kernel code it can reach, it is attacking the referee directly, and a successful exploit can reach past every wall at once. This is not hypothetical: kernel flaws have been published whose stated impact includes escaping a container to the host. NIST's guide to application container security puts it plainly: a shared kernel gives a larger attack surface between workloads than a hypervisor does, and the isolation it provides is not as strong.
A virtual machine's boundary is narrower. A bug in a guest's kernel compromises that guest, and getting out of it means finding a flaw in the hypervisor beneath, a narrower interface than the full set of system calls a shared kernel exposes. That is why namespaces and cgroups are best treated as a strong fence between programs of similar trust, and why NIST's guidance is to group workloads onto hosts by sensitivity, so that workloads that must not share a kernel run on separate hosts, virtual or physical.
The fence can be thickened without changing its shape. Running isolated workloads as unprivileged users, mapping root inside to an ordinary user outside, granting as few capabilities as the job needs, and keeping the host kernel patched all shrink what an attacker can reach. None of them changes the basic fact: one kernel, shared by everything above it.
Reading the walls you run inside
Sooner or later the services you run will live inside these walls, whether they are the steps of an agent pipeline, the handful of services on a homelab box, or an app you have taken to production. Container tools assemble exactly these pieces, a set of namespaces for what each workload sees and a cgroup for what it may use, so knowing the pieces turns the tool's behavior from magic into mechanism. When something isolated misbehaves, the question is no longer what the tool did but which wall the program hit.
Two symptoms are worth recognizing on sight. The first is slowness with no errors: a service that responds sluggishly while the machine as a whole looks idle may be spending part of every period throttled by its CPU quota, and its cgroup's cpu.stat will show nr_throttled climbing. The second is a sudden death with no log message of its own: a process that vanishes mid-task may have been chosen by the OOM killer when its group reached memory.max. The kill arrives as SIGKILL, signal 9, so the shell that started it reports the exit status as 137, which is 128 + 9, and the group's memory.events shows its oom_kill count going up. Raising the limit or fixing the leak are different cures, but both start from knowing which wall it was.
The readings are already there on a modern Linux machine. /proc/self/cgroup names the group a process is in, the files in that group's directory under /sys/fs/cgroup hold its limits and counters, and /proc/self/ns shows which walls it sits behind. The same habit as with any process applies: before restarting or guessing, read what the kernel already knows.
Next on your path to A personal cloud you can destroy and rebuild: Containers ▸