Sub topic 6
Namespaces: what a process can see
This chapter explains Linux namespaces, the kernel feature that gives a container its own view of the system. It assumes you can use a Linux or macOS command line, know what a process is, and understand that the kernel manages processes, memory, and devices for every program on the host.
The idea
A namespace wraps a global system resource so that processes inside it see their own isolated copy. Processes in different namespaces can use the same names for different things. Two containers can each have a process with ID 1, or a hostname called web, without conflict.
Namespaces limit what a process can see. They do not limit what it can use. Limiting use is the job of control groups, covered in the next chapter.
Common namespace types
Each type isolates one kind of resource.
| Namespace | Isolates | Effect inside it |
|---|---|---|
| PID | Process IDs | The first process is PID 1. Host processes are invisible. |
| UTS | Hostname and domain name | The hostname can change without affecting the host. |
| Mount | Mount points | The process sees its own filesystem tree. |
| Network | Interfaces, routes, ports | The process has its own IP addresses and port numbers. |
| IPC | Shared memory and message queues | Processes outside cannot access them. |
| User | User and group IDs | Root inside can map to an unprivileged user outside. |
A container runtime usually creates a new namespace of each type for every container. This combination is what makes the container look like a separate machine.
Trying it yourself
The unshare command starts a program in new namespaces. Run this on a Linux host:
sudo unshare --uts --pid --fork --mount-proc sh -c 'hostname demo; hostname; ps aux'The flags do the following:
--utscreates a new UTS namespace, sohostname demochanges the name only inside it.--pidcreates a new PID namespace.--forkmakes the command run as a child process. The new PID namespace applies to children, so this flag is required.--mount-procmounts a fresh/proc. Tools likepsread/proc, so without it they would still show host processes.
The output prints demo, then a process list with only sh and ps. Back in your normal shell, hostname is unchanged.
One process, two views
A process belongs to a namespace of each type. The host kernel still tracks every process. From the host, a container's "PID 1" has an ordinary, larger PID. You can list a process's namespaces on Linux:
ls -l /proc/self/nsEach entry is a link with an ID such as pid:[4026531836]. Two processes with the same ID for a type share that namespace.
Common errors
- Forgetting
--forkwith--pid.unsharefails or behaves oddly because the calling process cannot enter the new PID namespace. - Seeing host processes after
--pid. Thepscommand read the host's/proc. Add--mount-proc. - Running without root. Most namespace types need
sudounless you use a user namespace. - Assuming namespaces are a security boundary on their own. They hide resources, but all processes still call the same kernel. A kernel bug can cross the boundary.
- Trying this on macOS. Namespaces are a Linux feature. Docker Desktop on macOS runs containers inside a Linux VM, so run
unsharein that VM or a Linux host.
Exercise
You run sudo unshare --pid --fork sh -c 'ps aux' and still see every process on the host. What is the likely cause, and how do you fix it?
Show answer
The new PID namespace exists, but ps reads /proc, which is still the host's mount. Add --mount-proc so a fresh /proc reflecting the new namespace is mounted.