Linux namespaces
Let programs see separate parts of one computer
A running program is called a process. Linux can give a process a separate view of selected computer resources. One group of processes may see its own list of process numbers or network devices while still sharing the same physical computer and Linux kernel. These separate views are namespaces. They help build containers, but a namespace by itself does not make a full virtual computer.
Each namespace type separates a different view. A PID namespace changes process numbering. A network namespace has its own network devices, protocol stack, routes, firewall rules, and port numbers. A mount namespace has its own list of mounted filesystems. A user namespace maps user and group IDs and capabilities. A UTS namespace can give processes different host and domain names. IPC, cgroup, and time namespaces separate other kinds of resources. A process can belong to one namespace of each type, so its network view and process-number view can have different boundaries.
For example, changing a hostname inside a UTS namespace does not change the hostname outside it. Two processes may share a network namespace while seeing different hostnames. Being a parent or child process does not by itself determine whether two programs share every namespace.
Namespaces are only one part of container isolation. Containers also rely on controls such as cgroups, which track or limit resource use, filesystem restrictions, capabilities, and security policy. Namespaces share the host's kernel.
Compare a process's namespace views
The proc filesystem is a window that exposes information about processes and the kernel. On many Linux systems, namespace links for a process appear under /proc. For the current shell, inspect:
ls -l /proc/$$/ns
Entries such as mnt, net, pid, and user point to namespace objects. Their targets contain an identifier. Compare entries of the same type for two processes: matching targets mean they refer to the same namespace. Permissions and proc mount options can limit access to another process's entries.
Start a command in new namespaces
unshare can start a command in selected new namespaces. This demonstration requires a system that permits unprivileged user namespaces:
unshare --user --map-root-user --pid --fork --mount-proc sh -c 'printf "PID namespace: "; readlink /proc/self/ns/pid; ps -o pid,comm'
The command maps the invoking user to root inside a user namespace, then forks a child into a PID namespace. --mount-proc also creates a mount namespace and mounts a matching /proc view there. The shell appears as PID 1 inside the PID namespace, although it has a different PID in the outer view. System policy may block the command; do not enable namespace creation or add privileges just to run it.
unshare and nsenter change the namespace context of a launched process. They do not configure virtual interfaces, routes, mounts, or resource limits. Those operations need their own tools and permission checks. Mapping a host user to UID 0 inside a user namespace does not give it root access on the host. Capabilities are interpreted relative to namespaces, while shared kernel resources still follow kernel access checks.




