Namespaces
The eight Linux namespaces, what each one hides, and how to create, inspect and enter them with unshare, lsns and nsenter.
Namespaces
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A namespace gives a group of processes their own copy of one global resource. Changes inside are invisible outside.
- Linux has eight types: mount, PID, network, UTS, IPC, user, cgroup and time. Each is created with a CLONE_NEW* flag.
- A container is ordinary processes in a set of new namespaces, limited by a cgroup. Nothing else is needed from the kernel.
- In a new PID namespace the first process is PID 1 inside, with a different PID outside. It must reap orphans like init does.
- A user namespace maps IDs, so a process can be root inside and an unprivileged user outside. That is what makes rootless containers possible.
- /proc/<pid>/ns/ holds one link per namespace. Two processes are in the same namespace when the inode numbers match.
- `lsns` lists namespaces, `unshare` creates them, and `nsenter -t PID` enters a running process's namespaces, often the easiest way into a container for debugging.
One kernel, many views
Run hostname in a container and you get the container’s name, not the host’s. Run ps and you see a handful of processes, with your app as PID 1. Run ip addr and there is an eth0 the host does not have. Yet there is only one kernel, and from the host every one of those processes is visible in an ordinary ps.
The kernel is showing different processes different views of the same machine. Each view is a namespace. namespaces(7) describes one as wrapping a global resource so that processes inside appear to have their own isolated instance of it. It is like a building with one set of plumbing, where each apartment gets its own door numbers and its own mailbox list: the infrastructure is shared, the view is not.
The devops lesson on containers makes the case that a container is just a process. This lesson is the Linux side: the namespaces themselves and the tools to handle them.
The eight namespaces
From the table in namespaces(7), man-pages 6.19:
| Namespace | Flag | Isolates |
|---|---|---|
| Mount | CLONE_NEWNS |
The list of mount points |
| PID | CLONE_NEWPID |
Process IDs |
| Network | CLONE_NEWNET |
Interfaces, routes, firewall rules, ports |
| UTS | CLONE_NEWUTS |
Hostname and NIS domain name |
| IPC | CLONE_NEWIPC |
System V IPC and POSIX message queues |
| User | CLONE_NEWUSER |
User and group IDs |
| Cgroup | CLONE_NEWCGROUP |
The visible root of the cgroup tree |
| Time | CLONE_NEWTIME |
Boot and monotonic clocks |
Mount was first, which is why its flag is the generic CLONE_NEWNS. Time is the newest. Three system calls handle them: clone() creates a process in new namespaces, unshare() moves the caller into new ones, and setns() joins existing ones.
A container runtime asks for most of these at once, puts the process in a cgroup from the previous lesson and switches its root filesystem. systemd’s sandboxing options in the services group use the same mechanism on a smaller scale: PrivateTmp= is a mount namespace, PrivateNetwork= a network namespace.
Seeing them
Every process has /proc/<pid>/ns/, with one symbolic link per type, such as net -> 'net:[4026531840]'. The number is an inode. Two processes are in the same namespace exactly when the numbers match. Per namespaces(7), holding one of these files open, or bind-mounting it somewhere, keeps the namespace alive after its last process exits, which is how ip netns keeps named network namespaces around.
lsns lists every namespace with its type, process count, lowest PID and command. lsns -t net narrows to one type.
Making them with unshare
Two examples from the unshare(1) man page, util-linux 2.43 development version. They are the man page’s output, not mine, as there is no Linux machine here.
A new PID namespace, where the command is PID 1:
# unshare --fork --pid --mount-proc readlink /proc/self
1
--fork matters because the PID namespace applies to children of the caller, so unshare has to fork the command for it to be the first process inside. --mount-proc creates a mount namespace and mounts a fresh /proc. Without it, ps inside would still read the host’s /proc and show every host process.
A user namespace, as an ordinary user:
$ id -u; id -g
1000
1000
$ unshare --user --map-root-user \
sh -c 'whoami; cat /proc/self/uid_map /proc/self/gid_map'
root
0 1000 1
0 1000 1
Inside, the shell is root. The map line reads: ID 0 inside corresponds to 1000 outside, for a range of 1 ID. Files it creates on the host’s disk belong to 1000. That mapping is the basis of rootless containers: root powers inside the namespace, an ordinary user’s powers over everything outside. The flip side is that user namespaces let unprivileged users reach kernel code that used to need real root, so they enlarge the kernel’s attack surface, and some hardened setups restrict them.
PID 1 inside
In a new PID namespace the first process has PID 1 there, and a different PID on the host. PID 1 has special duties: it adopts orphaned processes and must reap them, or they become zombies, the Z state from the /proc lesson. Per pid_namespaces(7), other processes in the namespace can only send it signals it has installed a handler for, so a default SIGTERM does nothing. And if it exits, the kernel kills every other process in the namespace with SIGKILL. An app that was never written to be init can leak zombies or ignore SIGTERM when it runs as PID 1 in a container. That is why container setups often run a small init such as tini as PID 1.
Getting inside with nsenter
nsenter runs a program inside the namespaces of an existing process. -t PID picks the target, and flags pick which namespaces: -m mount, -u UTS, -i IPC, -n network, -p PID, -U user, or -a for all. Two common uses, from nsenter(1), not run here:
nsenter -t 4123 -n ip addr # the container's network, the host's ip binary
nsenter -t 4123 -n ss -tlnp # what it is listening on
nsenter -t 4123 -a sh # a shell in all of its namespaces
The first two are the useful trick. Entering only the network namespace means you get the container’s interfaces and sockets with the host’s tools, so a minimal image with no ip or tcpdump is no obstacle.
About the video
The embed is all of LiveOverflow’s “How Docker Works - Intro to Namespaces”, February 2020, 13 minutes. Its chapters walk from Docker’s documentation of namespace types, through comparing users and processes inside and outside, the dockerd, containerd and runc process tree, tracing container creation with strace, finding the unshare syscall, creating a PID namespace and its init process, PID, mount and network isolation, inspecting namespaces in /proc, to user namespaces, fake root and escape risks. Tracing a real container down to the system call shows that namespaces are all there is. It is framed around Docker and does not cover lsns, nsenter, the time namespace or the cgroup namespace, which the lesson adds. English was judged from the channel and its manual English captions, the content from its chapters. Kerrisk’s talk in deeper is the full treatment.
What was run: nothing. Linux namespaces do not exist on macOS, so the outputs shown are quoted from unshare(1), and the rest is from namespaces(7), nsenter(1) and lsns(8) in man-pages 6.19 and util-linux, checked 2026-11-28.
Where this fits
That ends processes and resources. Next, where the data lives: block devices and partitions.
Check yourself
Inside a new PID namespace, `ps` still shows every host process. What was forgotten?
/proc was not remounted. ps reads /proc, which still shows the host's PID namespace. unshare --mount-proc mounts a fresh /proc in a new mount namespace.
How can you tell whether two processes share a network namespace?
Compare readlink /proc/<pid1>/ns/net and /proc/<pid2>/ns/net. The same inode number in brackets means the same namespace. lsns -t net also lists them.
A container image has no ip or tcpdump. How do you look at its network from the host?
Find a PID of a process in the container and run `nsenter -t PID -n ip addr` or `-n tcpdump`. You enter only its network namespace and use the host's tools.
Why can an unprivileged user run `unshare --user --map-root-user` and become root?
They become root only inside a new user namespace, where their own UID is mapped to 0. Outside, their processes still have their original UID, so they gain no power over the host.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.