1NAMESPACES(7) Linux Programmer's Manual NAMESPACES(7)
2
3
4
6 namespaces - overview of Linux namespaces
7
9 A namespace wraps a global system resource in an abstraction that makes
10 it appear to the processes within the namespace that they have their
11 own isolated instance of the global resource. Changes to the global
12 resource are visible to other processes that are members of the names‐
13 pace, but are invisible to other processes. One use of namespaces is
14 to implement containers.
15
16 This page provides pointers to information on the various namespace
17 types, describes the associated /proc files, and summarizes the APIs
18 for working with namespaces.
19
20 Namespace types
21 The following table shows the namespace types available on Linux. The
22 second column of the table shows the flag value that is used to specify
23 the namespace type in various APIs. The third column identifies the
24 manual page that provides details on the namespace type. The last col‐
25 umn is a summary of the resources that are isolated by the namespace
26 type.
27
28 Namespace Flag Page Isolates
29 Cgroup CLONE_NEWCGROUP cgroup_namespaces(7) Cgroup root directory
30 IPC CLONE_NEWIPC ipc_namespaces(7) System V IPC,
31 POSIX message queues
32 Network CLONE_NEWNET network_namespaces(7) Network devices,
33 stacks, ports, etc.
34 Mount CLONE_NEWNS mount_namespaces(7) Mount points
35 PID CLONE_NEWPID pid_namespaces(7) Process IDs
36 User CLONE_NEWUSER user_namespaces(7) User and group IDs
37 UTS CLONE_NEWUTS uts_namespaces(7) Hostname and NIS
38 domain name
39
40 The namespaces API
41 As well as various /proc files described below, the namespaces API
42 includes the following system calls:
43
44 clone(2)
45 The clone(2) system call creates a new process. If the flags
46 argument of the call specifies one or more of the CLONE_NEW*
47 flags listed below, then new namespaces are created for each
48 flag, and the child process is made a member of those names‐
49 paces. (This system call also implements a number of features
50 unrelated to namespaces.)
51
52 setns(2)
53 The setns(2) system call allows the calling process to join an
54 existing namespace. The namespace to join is specified via a
55 file descriptor that refers to one of the /proc/[pid]/ns files
56 described below.
57
58 unshare(2)
59 The unshare(2) system call moves the calling process to a new
60 namespace. If the flags argument of the call specifies one or
61 more of the CLONE_NEW* flags listed below, then new namespaces
62 are created for each flag, and the calling process is made a
63 member of those namespaces. (This system call also implements a
64 number of features unrelated to namespaces.)
65
66 ioctl(2)
67 Various ioctl(2) operations can be used to discover information
68 about namespaces. These operations are described in
69 ioctl_ns(2).
70
71 Creation of new namespaces using clone(2) and unshare(2) in most cases
72 requires the CAP_SYS_ADMIN capability, since, in the new namespace, the
73 creator will have the power to change global resources that are visible
74 to other processes that are subsequently created in, or join the names‐
75 pace. User namespaces are the exception: since Linux 3.8, no privilege
76 is required to create a user namespace.
77
78 The /proc/[pid]/ns/ directory
79 Each process has a /proc/[pid]/ns/ subdirectory containing one entry
80 for each namespace that supports being manipulated by setns(2):
81
82 $ ls -l /proc/$$/ns
83 total 0
84 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 cgroup -> cgroup:[4026531835]
85 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 ipc -> ipc:[4026531839]
86 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 mnt -> mnt:[4026531840]
87 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 net -> net:[4026531969]
88 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 pid -> pid:[4026531836]
89 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 pid_for_children -> pid:[4026531834]
90 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 user -> user:[4026531837]
91 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 uts -> uts:[4026531838]
92
93 Bind mounting (see mount(2)) one of the files in this directory to
94 somewhere else in the filesystem keeps the corresponding namespace of
95 the process specified by pid alive even if all processes currently in
96 the namespace terminate.
97
98 Opening one of the files in this directory (or a file that is bind
99 mounted to one of these files) returns a file handle for the corre‐
100 sponding namespace of the process specified by pid. As long as this
101 file descriptor remains open, the namespace will remain alive, even if
102 all processes in the namespace terminate. The file descriptor can be
103 passed to setns(2).
104
105 In Linux 3.7 and earlier, these files were visible as hard links.
106 Since Linux 3.8, they appear as symbolic links. If two processes are
107 in the same namespace, then the device IDs and inode numbers of their
108 /proc/[pid]/ns/xxx symbolic links will be the same; an application can
109 check this using the stat.st_dev and stat.st_ino fields returned by
110 stat(2). The content of this symbolic link is a string containing the
111 namespace type and inode number as in the following example:
112
113 $ readlink /proc/$$/ns/uts
114 uts:[4026531838]
115
116 The symbolic links in this subdirectory are as follows:
117
118 /proc/[pid]/ns/cgroup (since Linux 4.6)
119 This file is a handle for the cgroup namespace of the process.
120
121 /proc/[pid]/ns/ipc (since Linux 3.0)
122 This file is a handle for the IPC namespace of the process.
123
124 /proc/[pid]/ns/mnt (since Linux 3.8)
125 This file is a handle for the mount namespace of the process.
126
127 /proc/[pid]/ns/net (since Linux 3.0)
128 This file is a handle for the network namespace of the process.
129
130 /proc/[pid]/ns/pid (since Linux 3.8)
131 This file is a handle for the PID namespace of the process.
132 This handle is permanent for the lifetime of the process (i.e.,
133 a process's PID namespace membership never changes).
134
135 /proc/[pid]/ns/pid_for_children (since Linux 4.12)
136 This file is a handle for the PID namespace of child processes
137 created by this process. This can change as a consequence of
138 calls to unshare(2) and setns(2) (see pid_namespaces(7)), so the
139 file may differ from /proc/[pid]/ns/pid. The symbolic link
140 gains a value only after the first child process is created in
141 the namespace. (Beforehand, readlink(2) of the symbolic link
142 will return an empty buffer.)
143
144 /proc/[pid]/ns/user (since Linux 3.8)
145 This file is a handle for the user namespace of the process.
146
147 /proc/[pid]/ns/uts (since Linux 3.0)
148 This file is a handle for the UTS namespace of the process.
149
150 Permission to dereference or read (readlink(2)) these symbolic links is
151 governed by a ptrace access mode PTRACE_MODE_READ_FSCREDS check; see
152 ptrace(2).
153
154 The /proc/sys/user directory
155 The files in the /proc/sys/user directory (which is present since Linux
156 4.9) expose limits on the number of namespaces of various types that
157 can be created. The files are as follows:
158
159 max_cgroup_namespaces
160 The value in this file defines a per-user limit on the number of
161 cgroup namespaces that may be created in the user namespace.
162
163 max_ipc_namespaces
164 The value in this file defines a per-user limit on the number of
165 ipc namespaces that may be created in the user namespace.
166
167 max_mnt_namespaces
168 The value in this file defines a per-user limit on the number of
169 mount namespaces that may be created in the user namespace.
170
171 max_net_namespaces
172 The value in this file defines a per-user limit on the number of
173 network namespaces that may be created in the user namespace.
174
175 max_pid_namespaces
176 The value in this file defines a per-user limit on the number of
177 pid namespaces that may be created in the user namespace.
178
179 max_user_namespaces
180 The value in this file defines a per-user limit on the number of
181 user namespaces that may be created in the user namespace.
182
183 max_uts_namespaces
184 The value in this file defines a per-user limit on the number of
185 uts namespaces that may be created in the user namespace.
186
187 Note the following details about these files:
188
189 * The values in these files are modifiable by privileged processes.
190
191 * The values exposed by these files are the limits for the user names‐
192 pace in which the opening process resides.
193
194 * The limits are per-user. Each user in the same user namespace can
195 create namespaces up to the defined limit.
196
197 * The limits apply to all users, including UID 0.
198
199 * These limits apply in addition to any other per-namespace limits
200 (such as those for PID and user namespaces) that may be enforced.
201
202 * Upon encountering these limits, clone(2) and unshare(2) fail with
203 the error ENOSPC.
204
205 * For the initial user namespace, the default value in each of these
206 files is half the limit on the number of threads that may be created
207 (/proc/sys/kernel/threads-max). In all descendant user namespaces,
208 the default value in each file is MAXINT.
209
210 * When a namespace is created, the object is also accounted against
211 ancestor namespaces. More precisely:
212
213 + Each user namespace has a creator UID.
214
215 + When a namespace is created, it is accounted against the creator
216 UIDs in each of the ancestor user namespaces, and the kernel
217 ensures that the corresponding namespace limit for the creator
218 UID in the ancestor namespace is not exceeded.
219
220 + The aforementioned point ensures that creating a new user names‐
221 pace cannot be used as a means to escape the limits in force in
222 the current user namespace.
223
224 Namespace lifetime
225 Absent any other factors, a namespace is automatically torn down when
226 the last process in the namespace terminates or leaves the namespace.
227 However, there are a number of other factors that may pin a namespace
228 into existence even though it has no member processes. These factors
229 include the following:
230
231 * An open file descriptor or a bind mount exists for the corresponding
232 /proc/[pid]/ns/* file.
233
234 * The namespace is hierarchical (i.e., a PID or user namespace), and
235 has a child namespace.
236
237 * It is a user namespace that owns one or more nonuser namespaces.
238
239 * It is a PID namespace, and there is a process that refers to the
240 namespace via a /proc/[pid]/ns/pid_for_children symbolic link.
241
242 * It is an IPC namespace, and a corresponding mount of an mqueue
243 filesystem (see mq_overview(7)) refers to this namespace.
244
245 * It is a PID namespace, and a corresponding mount of a proc(5)
246 filesystem refers to this namespace.
247
249 See clone(2) and user_namespaces(7).
250
252 nsenter(1), readlink(1), unshare(1), clone(2), ioctl_ns(2), setns(2),
253 unshare(2), proc(5), capabilities(7), cgroup_namespaces(7), cgroups(7),
254 credentials(7), ipc_namespaces(7), network_namespaces(7), pid_names‐
255 paces(7), user_namespaces(7), uts_namespaces(7), lsns(8), pam_names‐
256 pace(8), switch_root(8)
257
259 This page is part of release 5.04 of the Linux man-pages project. A
260 description of the project, information about reporting bugs, and the
261 latest version of this page, can be found at
262 https://www.kernel.org/doc/man-pages/.
263
264
265
266Linux 2019-08-02 NAMESPACES(7)