NAMESPACES
NAMEDESCRIPTION
EXAMPLE
SEE ALSO
COLOPHON
NAME
namespaces - overview of Linux namespaces
DESCRIPTION
A namespace wraps a global system resource in an abstraction that makes it appear to the processes within the namespace that they have their own isolated instance of the global resource. Changes to the global resource are visible to other processes that are members of the namespace, but are invisible to other processes. One use of namespaces is to implement containers.
Linux provides the following namespaces:
This page describes the various namespaces and the associated /proc files, and summarizes the APIs for working with namespaces. The namespaces API 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 below, 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 number of features unrelated to namespaces.) setns(2) The setns(2) system call allows the calling process to join an existing namespace. The namespace 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 below, 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 number of features unrelated to namespaces.) Creation of new namespaces using clone(2) and unshare(2) in most cases requires the CAP_SYS_ADMIN capability. User namespaces are the exception: since Linux 3.8, no privilege is required to create a user namespace. The
/proc/[pid]/ns/ directory $ ls -l
/proc/$$/ns 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. 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 remains open, the namespace will remain 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 as in the following example: $ readlink
/proc/$$/ns/uts The symbolic
links in this subdirectory are as follows: 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). /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.) /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 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_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 user namespaces that may be created in the user namespace. Note the following details about these files:
+
Cgroup
namespaces (CLONE_NEWCGROUP) IPC
namespaces (CLONE_NEWIPC) Each IPC namespace has its own set of System V IPC identifiers and its own POSIX message queue filesystem. Objects created in an IPC namespace are visible to all other processes that are members of that namespace, but are not visible to processes in other IPC namespaces. The following /proc interfaces are distinct in each IPC namespace:
When an IPC namespace is destroyed (i.e., when the last process that is a member of the namespace terminates), all IPC objects in the namespace are automatically destroyed. Use of IPC namespaces requires a kernel that is configured with the CONFIG_IPC_NS option. Network
namespaces (CLONE_NEWNET) Mount
namespaces (CLONE_NEWNS) PID
namespaces (CLONE_NEWPID) User
namespaces (CLONE_NEWUSER) UTS
namespaces (CLONE_NEWUTS) Use of UTS namespaces requires a kernel that is configured with the CONFIG_UTS_NS option. EXAMPLESee clone(2) and user_namespaces(7). SEE ALSOnsenter(1), readlink(1), unshare(1), clone(2), ioctl_ns(2), setns(2), unshare(2), proc(5), capabilities(7), cgroup_namespaces(7), cgroups(7), credentials(7), network_namespaces(7), pid_namespaces(7), user_namespaces(7), lsns(8), switch_root(8) COLOPHONThis page is part of release 4.16 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Manpage server at man.gnu.org.ua. Powered by mansrv 1.1 |