Global resources and private views
On a Linux system some resources exist once for the whole machine: the process table, the mount table, the host name, the network stack, the list of inter-process communication queues, and the user and group IDs. Namespaces wrap one of these resources so that a set of processes sees a private copy. The kernel gives each process a pointer to its namespace set, and system calls that look up process IDs, mounts, or interfaces consult that pointer.
Namespaces are the isolation half of a container. They hide things. A container cannot see the host's process list, cannot mount over the host's files, and cannot see the host's network interfaces, because each of those views belongs to its own namespace. The other half is cgroups, which limit how much a group of processes may use.
The main namespace types
Linux has several namespace types, each named after what it isolates. The most common are these:
- mnt isolates the mount table, so mounts made inside the namespace are not seen outside it.
- pid gives processes their own numbering, and the first process in a new PID namespace is PID 1 inside it.
- uts isolates the host name and the domain name.
- net gives its own network interfaces, routing tables, and firewall rules.
- ipc isolates System V message queues and shared memory names.
- user maps user and group IDs, so a process can be root inside a namespace without being root outside.
- cgroup virtualizes the view of the cgroup hierarchy.
The man page namespaces(7) is the reference for the whole list and for how each one behaves.
Seeing your namespaces
The directory /proc/PID/ns contains one link per namespace type. Each link has a target of the form type:[inode], where the number is the inode of the namespace object. Two processes that are in the same namespace show the same number for that type. Changing namespace means the link changes to a different number.
ls -l /proc/self/nsThe output lists links such as mnt -> mnt:[4026531841]. The exact numbers are not important. What matters is that they exist, and that a separate command can read them and compare them. Some kernels show more entries, such as time or cgroup, and the lab kernel may show a subset.
readlink /proc/self/ns/utsThis prints one identifier, which names the UTS namespace of the shell that runs the command. A process that starts a new UTS namespace gets a different identifier, and its host name can change without affecting the rest of the system.
Comparing namespaces
Namespace identifiers are useful for answering a simple question: are two processes isolated from each other? If the net link of two processes has the same number, they share a network stack. If the pid numbers differ, one of them is in a separate PID view. Scripts can compare the identifiers with a plain string comparison.
Because inode numbers are assigned by the kernel and change on every boot, do not write them into a file that will be read later. Compare them while both values are current.
Namespace identifiers answer "same or different". They do not tell you what the namespace contains. For the contents, read the process's own status, mount table, and interfaces from inside the namespace.
Who creates namespaces
A process creates a namespace with the unshare system call or a clone flag such as CLONE_NEWPID, and it joins an existing one with setns. The next lesson uses the unshare and nsenter programs, which wrap those calls. Creating most namespaces needs privilege, and some kernels allow unprivileged users to create user namespaces, which then grant the rest. Whether this lab kernel allows that is something you will check directly.
Namespaces stay alive as long as at least one process is in them, even if the creating process has exited. This is why a container can keep running a shell after its init has exited, and why stale namespaces sometimes show up in /proc.
Nested PID namespaces
PID namespaces nest. A process in a child PID namespace has one PID in its own namespace and a different PID in each ancestor namespace, and the host sees the number from the outermost view. The process list from the host therefore shows every process, while the list from inside a container shows only the processes in that container, with small numbers starting from one. A tool that prints PIDs must say which view it is using, because the same process can have two numbers.
The other namespace types work differently. A mount table or a network interface belongs to exactly one namespace at a time, and moving an interface between namespaces is an explicit operation. Reading the namespace link tells you which namespace a process is in right now, and that is the only question the link answers.
Why the links help when debugging
When a tool reports one network interface and you see another, compare the net links of the tool and of your shell. A mismatch means the tool runs in a different network namespace, and the fix is to run it inside the namespace where the interface lives. The comparison takes seconds and avoids guesses about firewalls or routing tables.
A common mistake with inode numbers
Some people try to identify a namespace by its number and expect the same number to appear after a reboot. The kernel assigns each namespace an inode number from an internal pool, so the number has no meaning across boots. The number is only useful for comparing live processes, and that is the use the lab makes of it. Treat a saved listing as a snapshot of one moment, and read the links again before you compare them.