1 .\" Copyright (c) 2013, 2016, 2017 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2012 by Eric W. Biederman <ebiederm@xmission.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
27 .TH NAMESPACES 7 2021-03-22 "Linux" "Linux Programmer's Manual"
29 namespaces \- overview of Linux namespaces
31 A namespace wraps a global system resource in an abstraction that
32 makes it appear to the processes within the namespace that they
33 have their own isolated instance of the global resource.
34 Changes to the global resource are visible to other processes
35 that are members of the namespace, but are invisible to other processes.
36 One use of namespaces is to implement containers.
38 This page provides pointers to information on the various namespace types,
39 describes the associated
41 files, and summarizes the APIs for working with namespaces.
44 The following table shows the namespace types available on Linux.
45 The second column of the table shows the flag value that is used to specify
46 the namespace type in various APIs.
47 The third column identifies the manual page that provides details
48 on the namespace type.
49 The last column is a summary of the resources that are isolated by
56 Namespace Flag Page Isolates
57 Cgroup CLONE_NEWCGROUP \fBcgroup_namespaces\fP(7) T{
60 IPC CLONE_NEWIPC \fBipc_namespaces\fP(7) T{
64 Network CLONE_NEWNET \fBnetwork_namespaces\fP(7) T{
68 Mount CLONE_NEWNS \fBmount_namespaces\fP(7) Mount points
69 PID CLONE_NEWPID \fBpid_namespaces\fP(7) Process IDs
70 Time CLONE_NEWTIME \fBtime_namespaces\fP(7) T{
74 User CLONE_NEWUSER \fBuser_namespaces\fP(7) T{
77 UTS CLONE_NEWUTS \fButs_namespaces\fP(7) T{
85 .\" ==================== The namespaces API ====================
87 .SS The namespaces API
90 files described below,
91 the namespaces API includes the following system calls:
96 system call creates a new process.
99 argument of the call specifies one or more of the
101 flags listed above, then new namespaces are created for each flag,
102 and the child process is made a member of those namespaces.
103 (This system call also implements a number of features
104 unrelated to namespaces.)
109 system call allows the calling process to join an existing namespace.
110 The namespace to join is specified via a file descriptor that refers to
113 files described below.
118 system call moves the calling process to a new namespace.
121 argument of the call specifies one or more of the
123 flags listed above, then new namespaces are created for each flag,
124 and the calling process is made a member of those namespaces.
125 (This system call also implements a number of features
126 unrelated to namespaces.)
131 operations can be used to discover information about namespaces.
132 These operations are described in
135 Creation of new namespaces using
139 in most cases requires the
141 capability, since, in the new namespace,
142 the creator will have the power to change global resources
143 that are visible to other processes that are subsequently created in,
144 or join the namespace.
145 User namespaces are the exception: since Linux 3.8,
146 no privilege is required to create a user namespace.
148 .\" ==================== The /proc/[pid]/ns/ directory ====================
150 .SS The /proc/[pid]/ns/ directory
153 .\" See commit 6b4e306aa3dc94a0545eb9279475b1ab6209a31f
154 subdirectory containing one entry for each namespace that
155 supports being manipulated by
160 $ \fBls \-l /proc/$$/ns | awk \(aq{print $1, $9, $10, $11}\(aq\fP
162 lrwxrwxrwx. cgroup \-> cgroup:[4026531835]
163 lrwxrwxrwx. ipc \-> ipc:[4026531839]
164 lrwxrwxrwx. mnt \-> mnt:[4026531840]
165 lrwxrwxrwx. net \-> net:[4026531969]
166 lrwxrwxrwx. pid \-> pid:[4026531836]
167 lrwxrwxrwx. pid_for_children \-> pid:[4026531834]
168 lrwxrwxrwx. time \-> time:[4026531834]
169 lrwxrwxrwx. time_for_children \-> time:[4026531834]
170 lrwxrwxrwx. user \-> user:[4026531837]
171 lrwxrwxrwx. uts \-> uts:[4026531838]
177 one of the files in this directory
178 to somewhere else in the filesystem keeps
179 the corresponding namespace of the process specified by
181 alive even if all processes currently in the namespace terminate.
183 Opening one of the files in this directory
184 (or a file that is bind mounted to one of these files)
185 returns a file handle for
186 the corresponding namespace of the process specified by
188 As long as this file descriptor remains open,
189 the namespace will remain alive,
190 even if all processes in the namespace terminate.
191 The file descriptor can be passed to
194 In Linux 3.7 and earlier, these files were visible as hard links.
196 .\" commit bf056bfa80596a5d14b26b17276a56a0dcb080e5
197 they appear as symbolic links.
198 If two processes are in the same namespace,
199 then the device IDs and inode numbers of their
200 .IR /proc/[pid]/ns/xxx
201 symbolic links will be the same; an application can check this using the
203 .\" Eric Biederman: "I reserve the right for st_dev to be significant
204 .\" when comparing namespaces."
205 .\" https://lore.kernel.org/lkml/87poky5ca9.fsf@xmission.com/
206 .\" Re: Documenting the ioctl interfaces to discover relationships...
207 .\" Date: Mon, 12 Dec 2016 11:30:38 +1300
212 The content of this symbolic link is a string containing
213 the namespace type and inode number as in the following example:
217 $ \fBreadlink /proc/$$/ns/uts\fP
222 The symbolic links in this subdirectory are as follows:
224 .IR /proc/[pid]/ns/cgroup " (since Linux 4.6)"
225 This file is a handle for the cgroup namespace of the process.
227 .IR /proc/[pid]/ns/ipc " (since Linux 3.0)"
228 This file is a handle for the IPC namespace of the process.
230 .IR /proc/[pid]/ns/mnt " (since Linux 3.8)"
231 .\" commit 8823c079ba7136dc1948d6f6dcb5f8022bde438e
232 This file is a handle for the mount namespace of the process.
234 .IR /proc/[pid]/ns/net " (since Linux 3.0)"
235 This file is a handle for the network namespace of the process.
237 .IR /proc/[pid]/ns/pid " (since Linux 3.8)"
238 .\" commit 57e8391d327609cbf12d843259c968b9e5c1838f
239 This file is a handle for the PID namespace of the process.
240 This handle is permanent for the lifetime of the process
241 (i.e., a process's PID namespace membership never changes).
243 .IR /proc/[pid]/ns/pid_for_children " (since Linux 4.12)"
244 .\" commit eaa0d190bfe1ed891b814a52712dcd852554cb08
245 This file is a handle for the PID namespace of
246 child processes created by this process.
247 This can change as a consequence of calls to
252 .BR pid_namespaces (7)),
253 so the file may differ from
254 .IR /proc/[pid]/ns/pid .
255 The symbolic link gains a value only after the first child process
256 is created in the namespace.
259 of the symbolic link will return an empty buffer.)
261 .IR /proc/[pid]/ns/time " (since Linux 5.6)"
262 This file is a handle for the time namespace of the process.
264 .IR /proc/[pid]/ns/time_for_children " (since Linux 5.6)"
265 This file is a handle for the time namespace of
266 child processes created by this process.
267 This can change as a consequence of calls to
272 .BR time_namespaces (7)),
273 so the file may differ from
274 .IR /proc/[pid]/ns/time .
276 .IR /proc/[pid]/ns/user " (since Linux 3.8)"
277 .\" commit cde1975bc242f3e1072bde623ef378e547b73f91
278 This file is a handle for the user namespace of the process.
280 .IR /proc/[pid]/ns/uts " (since Linux 3.0)"
281 This file is a handle for the UTS namespace of the process.
283 Permission to dereference or read
285 these symbolic links is governed by a ptrace access mode
286 .B PTRACE_MODE_READ_FSCREDS
290 .\" ==================== The /proc/sys/user directory ====================
292 .SS The /proc/sys/user directory
295 directory (which is present since Linux 4.9) expose limits
296 on the number of namespaces of various types that can be created.
297 The files are as follows:
299 .IR max_cgroup_namespaces
300 The value in this file defines a per-user limit on the number of
301 cgroup namespaces that may be created in the user namespace.
303 .IR max_ipc_namespaces
304 The value in this file defines a per-user limit on the number of
305 ipc namespaces that may be created in the user namespace.
307 .IR max_mnt_namespaces
308 The value in this file defines a per-user limit on the number of
309 mount namespaces that may be created in the user namespace.
311 .IR max_net_namespaces
312 The value in this file defines a per-user limit on the number of
313 network namespaces that may be created in the user namespace.
315 .IR max_pid_namespaces
316 The value in this file defines a per-user limit on the number of
317 PID namespaces that may be created in the user namespace.
319 .IR max_time_namespaces " (since Linux 5.7)"
320 .\" commit eeec26d5da8248ea4e240b8795bb4364213d3247
321 The value in this file defines a per-user limit on the number of
322 time namespaces that may be created in the user namespace.
324 .IR max_user_namespaces
325 The value in this file defines a per-user limit on the number of
326 user namespaces that may be created in the user namespace.
328 .IR max_uts_namespaces
329 The value in this file defines a per-user limit on the number of
330 uts namespaces that may be created in the user namespace.
332 Note the following details about these files:
334 The values in these files are modifiable by privileged processes.
336 The values exposed by these files are the limits for the user namespace
337 in which the opening process resides.
339 The limits are per-user.
340 Each user in the same user namespace
341 can create namespaces up to the defined limit.
343 The limits apply to all users, including UID 0.
345 These limits apply in addition to any other per-namespace
346 limits (such as those for PID and user namespaces) that may be enforced.
348 Upon encountering these limits,
355 For the initial user namespace,
356 the default value in each of these files is half the limit on the number
357 of threads that may be created
358 .RI ( /proc/sys/kernel/threads\-max ).
359 In all descendant user namespaces, the default value in each file is
362 When a namespace is created, the object is also accounted
363 against ancestor namespaces.
367 Each user namespace has a creator UID.
369 When a namespace is created,
370 it is accounted against the creator UIDs in each of the
371 ancestor user namespaces,
372 and the kernel ensures that the corresponding namespace limit
373 for the creator UID in the ancestor namespace is not exceeded.
375 The aforementioned point ensures that creating a new user namespace
376 cannot be used as a means to escape the limits in force
377 in the current user namespace.
380 .SS Namespace lifetime
381 Absent any other factors,
382 a namespace is automatically torn down when the last process in
383 the namespace terminates or leaves the namespace.
384 However, there are a number of other factors that may pin
385 a namespace into existence even though it has no member processes.
386 These factors include the following:
388 An open file descriptor or a bind mount exists for the corresponding
392 The namespace is hierarchical (i.e., a PID or user namespace),
393 and has a child namespace.
395 It is a user namespace that owns one or more nonuser namespaces.
397 It is a PID namespace,
398 and there is a process that refers to the namespace via a
399 .IR /proc/[pid]/ns/pid_for_children
402 It is a time namespace,
403 and there is a process that refers to the namespace via a
404 .IR /proc/[pid]/ns/time_for_children
407 It is an IPC namespace, and a corresponding mount of an
411 refers to this namespace.
413 It is a PID namespace, and a corresponding mount of a
415 filesystem refers to this namespace.
420 .BR user_namespaces (7).
430 .BR capabilities (7),
431 .BR cgroup_namespaces (7),
434 .BR ipc_namespaces (7),
435 .BR network_namespaces (7),
436 .BR pid_namespaces (7),
437 .BR user_namespaces (7),
438 .BR uts_namespaces (7),