Part 3 of 7 · 2 min read
The eight kinds
Think of a house with separate systems for post, telephone, water and so on, where each tenant has its own mailbox, extension number and tap. Each system is renamed on its own, so a tenant can have a private phone book but share the water. The analogy stops in one place: a house has no rule that links the systems, and the kernel has a flag for each kind, so a program picks the kinds it wants.
In plain words: Linux has eight kinds. ls /proc/self/ns on the test machine lists them: cgroup, ipc, mnt, net, pid, time, user and uts.
| Kind | Flag | What the process sees privately |
|---|---|---|
| Mount | CLONE_NEWNS |
The tree of mounts and so the file system view (Level 9) |
| PID | CLONE_NEWPID |
Process numbers, with its own number 1 |
| Network | CLONE_NEWNET |
Network devices, addresses, routes, ports |
| UTS | CLONE_NEWUTS |
Host name and domain name |
| IPC | CLONE_NEWIPC |
Shared memory, semaphores and message queues (Level 7) |
| User | CLONE_NEWUSER |
User and group IDs, and capabilities (Level 10) |
| Cgroup | CLONE_NEWCGROUP |
The view of the control-group tree (page 5) |
| Time | CLONE_NEWTIME |
Offsets of the monotonic and boot clocks |
Three system calls handle them. clone creates a process in new namespaces, unshare moves the calling process into new ones, and setns joins an existing one (the way nsenter and docker exec enter a running container). lsns lists the namespaces that exist.
A worked example. The host's lsns on the test machine shows the first lines:
NS TYPE NPROCS PID USER COMMAND
4026531832 mnt 66 2 root kthreadd
4026531833 net 67 2 root kthreadd
4026531836 pid 67 2 root kthreadd
4026531838 uts 67 2 root kthreadd
All 67 processes share one network, one pid and one UTS namespace, which are the initial ones, shown by their number 4026531836 and similar. One mount namespace (4026531860) holds a single process: a kernel thread with its own mount tree. A new container would add four or five more lines, with new numbers.
The key lines. A namespace is identified by an inode number, and two processes share a namespace exactly if the numbers in /proc/PID/ns/... are equal.
unshare, clone and setns
clone creates a new process, and with flags it puts the child into new namespaces. unshare moves the calling process into new namespaces. setns moves the calling process into an existing namespace named by a file descriptor.
Example: unshare --uts sh starts a shell with its own host name, and nsenter --target 517 --uts sh joins the namespace of process 517.
Check yourself: 1. Name five of the eight kinds. 2. Which call joins a namespace that already exists? 3. How can you tell that two processes share a network namespace?