namespaces(7)

๐Ÿฆ‹ namespaces(7) โ€” the manual page, but make it make sense ๐Ÿ’…

Miscellaneous Information Manual โœจ this is namespaces(7), bestie โ€” the big overview of Linux namespaces. Buckle up, it's a long one, but it's genuinely iconic.


๐ŸŒธ NAME ๐ŸŒธ

namespaces โ€” overview of Linux namespaces. that's the whole vibe.


โœจ DESCRIPTION โœจ

Okay so picture this: a namespace wraps a global system resource in an abstraction that makes it look to the processes inside the namespace like they've got their very own isolated instance of that global resource. Like a little private world ๐ŸŒโœจ

Changes to the global resource are visible to other processes that are members of the namespace, but they're invisible to other processes. One use of namespaces โ€” and honestly the main character use โ€” is to implement containers. ๐Ÿซถ

This page gives you the pointers to info on the various namespace types, describes the associated /proc files, and summarizes the APIs for working with namespaces. iykyk.


๐ŸŽ€ Namespace types ๐ŸŽ€

Here's the lineup of namespace types available on Linux. The second column is the flag value used to specify the namespace type in various APIs. The third column points you at the manual page with the deets. The last column is a lil summary of the resources that get isolated by that namespace type.

  • Cgroup โ€” flag: CLONE_NEWCGROUP โ€” page: cgroup_namespaces(7) โ€” isolates: Cgroup root directory
  • IPC โ€” flag: CLONE_NEWIPC โ€” page: ipc_namespaces(7) โ€” isolates: System V IPC, POSIX message queues
  • Network โ€” flag: CLONE_NEWNET โ€” page: network_namespaces(7) โ€” isolates: Network devices, stacks, ports, etc.
  • Mount โ€” flag: CLONE_NEWNS โ€” page: mount_namespaces(7) โ€” isolates: Mount points
  • PID โ€” flag: CLONE_NEWPID โ€” page: pid_namespaces(7) โ€” isolates: Process IDs
  • Time โ€” flag: CLONE_NEWTIME โ€” page: time_namespaces(7) โ€” isolates: Boot and monotonic clocks
  • User โ€” flag: CLONE_NEWUSER โ€” page: user_namespaces(7) โ€” isolates: User and group IDs
  • UTS โ€” flag: CLONE_NEWUTS โ€” page: uts_namespaces(7) โ€” isolates: Hostname and NIS domain name

๐Ÿ”ฅ The namespaces API ๐Ÿ”ฅ

So besides the various /proc files described further down, the namespaces API includes these system calls:

  • clone(2) โ€” the clone(2) system call creates a new process. If the flags argument of the call specifies one or more of the CLONE_NEW* flags listed above, then new namespaces are created for each flag, and the child process is made a member of those namespaces. (This system call also implements a bunch of features that have literally nothing to do with namespaces, fyi.)
  • setns(2) โ€” the setns(2) system call lets the calling process join an existing namespace. The namespace you want to join is specified via a file descriptor that refers to one of the /proc/pid/ns files described below.
  • unshare(2) โ€” the unshare(2) system call moves the calling process to a new namespace. If the flags argument of the call specifies one or more of the CLONE_NEW* flags listed above, then new namespaces are created for each flag, and the calling process is made a member of those namespaces. (This system call also implements a bunch of features unrelated to namespaces too.)
  • ioctl(2) โ€” various ioctl(2) operations can be used to discover information about namespaces. These operations are described in ioctl_nsfs(2).

Creating new namespaces with clone(2) and unshare(2) in most cases requires the CAP_SYS_ADMIN capability, because in the new namespace, the creator will have the power to change global resources that are visible to other processes that are subsequently created in, or join, the namespace. ๐Ÿ‘‘

User namespaces are the exception though โ€” since Linux 3.8, no privilege is required to create a user namespace. no cap, no cap needed. ๐Ÿ™ˆ

๐Ÿฆ‹ The /proc/pid/ns/ directory ๐Ÿฆ‹

Every process has a /proc/pid/ns/ subdirectory containing one entry for each namespace that supports being manipulated by setns(2):

$ ls -l /proc/$$/ns | awk '{print $1, $9, $10, $11}'
total 0
lrwxrwxrwx. cgroup -> cgroup:[4026531835]
lrwxrwxrwx. ipc -> ipc:[4026531839]
lrwxrwxrwx. mnt -> mnt:[4026531840]
lrwxrwxrwx. net -> net:[4026531969]
lrwxrwxrwx. pid -> pid:[4026531836]
lrwxrwxrwx. pid_for_children -> pid:[4026531834]
lrwxrwxrwx. time -> time:[4026531834]
lrwxrwxrwx. time_for_children -> time:[4026531834]
lrwxrwxrwx. user -> user:[4026531837]
lrwxrwxrwx. uts -> uts:[4026531838]

Bind mounting (see mount(2)) one of the files in this directory to somewhere else in the filesystem keeps the corresponding namespace of the process specified by pid alive even if all processes currently in the namespace terminate. it's giving immortality tbh ๐Ÿ’€โœจ

Opening one of the files in this directory (or a file that is bind mounted to one of these files) returns a file handle for the corresponding namespace of the process specified by pid. As long as this file descriptor stays open, the namespace stays alive, even if all processes in the namespace terminate. The file descriptor can be passed to setns(2). ๐ŸŽ€

In Linux 3.7 and earlier, these files were visible as hard links. Since Linux 3.8, they appear as symbolic links. If two processes are in the same namespace, then the device IDs and inode numbers of their /proc/pid/ns/xxx symbolic links will be the same; an application can check this using the stat.st_dev and stat.st_ino fields returned by stat(2). The content of this symbolic link is a string containing the namespace type and inode number, like this:

$ readlink /proc/$$/ns/uts
uts:[4026531838]

The symbolic links in this subdirectory are as follows:

  • /proc/pid/ns/cgroup (since Linux 4.6) โ€” this file is a handle for the cgroup namespace of the process.
  • /proc/pid/ns/ipc (since Linux 3.0) โ€” this file is a handle for the IPC namespace of the process.
  • /proc/pid/ns/mnt (since Linux 3.8) โ€” this file is a handle for the mount namespace of the process.
  • /proc/pid/ns/net (since Linux 3.0) โ€” this file is a handle for the network namespace of the process.
  • /proc/pid/ns/pid (since Linux 3.8) โ€” this file is a handle for the PID namespace of the process. This handle is permanent for the lifetime of the process (i.e., a process's PID namespace membership never changes). periodt.
  • /proc/pid/ns/pid_for_children (since Linux 4.12) โ€” this file is a handle for the PID namespace of child processes created by this process. This can change as a consequence of calls to unshare(2) and setns(2) (see pid_namespaces(7)), so the file may differ from /proc/pid/ns/pid. The symbolic link gains a value only after the first child process is created in the namespace. (Beforehand, readlink(2) of the symbolic link will return an empty buffer.) ๐Ÿ˜ญ rude but true
  • /proc/pid/ns/time (since Linux 5.6) โ€” this file is a handle for the time namespace of the process.
  • /proc/pid/ns/time_for_children (since Linux 5.6) โ€” this file is a handle for the time namespace of child processes created by this process. This can change as a consequence of calls to unshare(2) and setns(2) (see time_namespaces(7)), so the file may differ from /proc/pid/ns/time.
  • /proc/pid/ns/user (since Linux 3.8) โ€” this file is a handle for the user namespace of the process.
  • /proc/pid/ns/uts (since Linux 3.0) โ€” this file is a handle for the UTS namespace of the process.

Permission to dereference or read (readlink(2)) these symbolic links is governed by a ptrace access mode PTRACE_MODE_READ_FSCREDS check; see ptrace(2). ๐Ÿ”


๐ŸŒˆ The /proc/sys/user directory ๐ŸŒˆ

The files in the /proc/sys/user directory (present since Linux 4.9) expose limits on the number of namespaces of various types that can be created. The files are:

  • max_cgroup_namespaces โ€” the value in this file defines a per-user limit on the number of cgroup namespaces that may be created in the user namespace.
  • max_ipc_namespaces โ€” the value in this file defines a per-user limit on the number of ipc namespaces that may be created in the user namespace.
  • max_mnt_namespaces โ€” the value in this file defines a per-user limit on the number of mount namespaces that may be created in the user namespace.
  • max_net_namespaces โ€” the value in this file defines a per-user limit on the number of network namespaces that may be created in the user namespace.
  • max_pid_namespaces โ€” the value in this file defines a per-user limit on the number of PID namespaces that may be created in the user namespace.
  • max_time_namespaces (since Linux 5.7) โ€” the value in this file defines a per-user limit on the number of time namespaces that may be created in the user namespace.
  • max_user_namespaces โ€” the value in this file defines a per-user limit on the number of user namespaces that may be created in the user namespace.
  • max_uts_namespaces โ€” the value in this file defines a per-user limit on the number of uts namespaces that may be created in the user namespace.

Note these details about these files, bestie โ€” they matter:

  • The values in these files are modifiable by privileged processes. ๐Ÿ‘‘
  • The values exposed by these files are the limits for the user namespace in which the opening process resides.
  • The limits are per-user. Each user in the same user namespace can create namespaces up to the defined limit.
  • The limits apply to all users, including UID 0.
  • These limits apply in addition to any other per-namespace limits (such as those for PID and user namespaces) that may be enforced.
  • Upon encountering these limits, clone(2) and unshare(2) fail with the error ENOSPC. ๐Ÿ’€
  • For the initial user namespace, the default value in each of these files is half the limit on the number of threads that may be created (/proc/sys/kernel/threads-max). In all descendant user namespaces, the default value in each file is MAXINT.
  • When a namespace is created, the object is also accounted against ancestor namespaces. More precisely:
    • Each user namespace has a creator UID.
    • When a namespace is created, it is accounted against the creator UIDs in each of the ancestor user namespaces, and the kernel ensures that the corresponding namespace limit for the creator UID in the ancestor namespace is not exceeded. ๐Ÿงšโ€โ™€๏ธ
  • And like, the point above? That's the whole reason a fresh user namespace can't be used as a sneaky little escape hatch from the limits that are currently in force in your current user namespace. no loopholes bestie, the vibes are contained ๐Ÿ’…

โœจ Namespace lifetime โœจ

So basically, if nothing else is going on, a namespace justโ€ฆ gets torn down automatically the second the last process inside it terminates or dips out and leaves. Poof. Gone. ๐Ÿซถ

But โ€” and this is the plot twist โ€” there are a bunch of other things that can pin a namespace into existence even when it has literally zero member processes. #clingy. Here's the list of what's holding it hostage:

  • There's an open file descriptor or a bind mount hanging around for the matching /proc/pid/ns/* file. ๐Ÿ“Ž
  • The namespace is hierarchical (so like, a PID or user namespace) and it's got a child namespace. Family obligations fr fr. ๐Ÿ‘ถ
  • It's a user namespace that owns one or more nonuser namespaces. She's a landlord. ๐Ÿ 
  • It's a PID namespace, and there's a process pointing at it through a /proc/pid/ns/pid_for_children symbolic link. ๐Ÿ”—
  • It's a time namespace, and there's a process pointing at it through a /proc/pid/ns/time_for_children symbolic link. โฐ
  • It's an IPC namespace, and a corresponding mount of an mqueue filesystem (go read mq_overview(7) ๐Ÿ’Œ) refers to this namespace.
  • It's a PID namespace, and a corresponding mount of a proc(5) filesystem refers to this namespace. ๐Ÿ—‚๏ธ

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

Honestly just go look at clone(2) and user_namespaces(7) โ€” they've got the receipts. โœจ


๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

The whole bestie roster, go say hi:


okay that's the whole page bestie, we ATE that. ๐Ÿ’…โœจ This lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08 โ€” going down in history fr fr. now go namespace something responsibly, mwah ๐ŸŽ€๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms