ioctl_userfaultfd.2: Update UFFDIO_API description
[man-pages.git] / man7 / namespaces.7
blob36bd72710964b4b1c2346020e9872a20407c2125
1 .\" Copyright (c) 2013 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2012 by Eric W. Biederman <ebiederm@xmission.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\"
27 .TH NAMESPACES 7 2016-12-12 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 namespaces \- overview of Linux namespaces
30 .SH DESCRIPTION
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 Linux provides the following namespaces:
39 .TS
40 lB lB lB
41 l lB l.
42 Namespace       Constant        Isolates
43 Cgroup  CLONE_NEWCGROUP Cgroup root directory
44 IPC     CLONE_NEWIPC    System V IPC, POSIX message queues
45 Network CLONE_NEWNET    Network devices, stacks, ports, etc.
46 Mount   CLONE_NEWNS     Mount points
47 PID     CLONE_NEWPID    Process IDs
48 User    CLONE_NEWUSER   User and group IDs
49 UTS     CLONE_NEWUTS    Hostname and NIS domain name
50 .TE
52 This page describes the various namespaces and the associated
53 .I /proc
54 files, and summarizes the APIs for working with namespaces.
55 .\"
56 .\" ==================== The namespaces API ====================
57 .\"
58 .SS The namespaces API
59 As well as various
60 .I /proc
61 files described below,
62 the namespaces API includes the following system calls:
63 .TP
64 .BR clone (2)
65 The
66 .BR clone (2)
67 system call creates a new process.
68 If the
69 .I flags
70 argument of the call specifies one or more of the
71 .B CLONE_NEW*
72 flags listed below, then new namespaces are created for each flag,
73 and the child process is made a member of those namespaces.
74 (This system call also implements a number of features
75 unrelated to namespaces.)
76 .TP
77 .BR setns (2)
78 The
79 .BR setns (2)
80 system call allows the calling process to join an existing namespace.
81 The namespace to join is specified via a file descriptor that refers to
82 one of the
83 .IR /proc/[pid]/ns
84 files described below.
85 .TP
86 .BR unshare (2)
87 The
88 .BR unshare (2)
89 system call moves the calling process to a new namespace.
90 If the
91 .I flags
92 argument of the call specifies one or more of the
93 .B CLONE_NEW*
94 flags listed below, then new namespaces are created for each flag,
95 and the calling process is made a member of those namespaces.
96 (This system call also implements a number of features
97 unrelated to namespaces.)
98 .PP
99 Creation of new namespaces using
100 .BR clone (2)
102 .BR unshare (2)
103 in most cases requires the
104 .BR CAP_SYS_ADMIN
105 capability.
106 User namespaces are the exception: since Linux 3.8,
107 no privilege is required to create a user namespace.
109 .\" ==================== The /proc/[pid]/ns/ directory ====================
111 .SS The /proc/[pid]/ns/ directory
112 Each process has a
113 .IR /proc/[pid]/ns/
114 .\" See commit 6b4e306aa3dc94a0545eb9279475b1ab6209a31f
115 subdirectory containing one entry for each namespace that
116 supports being manipulated by
117 .BR setns (2):
119 .in +4n
121 $ \fBls \-l /proc/$$/ns\fP
122 total 0
123 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 cgroup \-> cgroup:[4026531835]
124 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 ipc \-> ipc:[4026531839]
125 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 mnt \-> mnt:[4026531840]
126 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 net \-> net:[4026531969]
127 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 pid \-> pid:[4026531836]
128 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 user \-> user:[4026531837]
129 lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 uts \-> uts:[4026531838]
133 Bind mounting (see
134 .BR mount (2))
135 one of the files in this directory
136 to somewhere else in the filesystem keeps
137 the corresponding namespace of the process specified by
138 .I pid
139 alive even if all processes currently in the namespace terminate.
141 Opening one of the files in this directory
142 (or a file that is bind mounted to one of these files)
143 returns a file handle for
144 the corresponding namespace of the process specified by
145 .IR pid .
146 As long as this file descriptor remains open,
147 the namespace will remain alive,
148 even if all processes in the namespace terminate.
149 The file descriptor can be passed to
150 .BR setns (2).
152 In Linux 3.7 and earlier, these files were visible as hard links.
153 Since Linux 3.8,
154 .\" commit bf056bfa80596a5d14b26b17276a56a0dcb080e5
155 they appear as symbolic links.
156 If two processes are in the same namespace, then the inode numbers of their
157 .IR /proc/[pid]/ns/xxx
158 symbolic links will be the same; an application can check this using the
159 .I stat.st_ino
160 field returned by
161 .BR stat (2).
162 The content of this symbolic link is a string containing
163 the namespace type and inode number as in the following example:
165 .in +4n
167 $ \fBreadlink /proc/$$/ns/uts\fP
168 uts:[4026531838]
172 The symbolic links in this subdirectory are as follows:
174 .IR /proc/[pid]/ns/cgroup " (since Linux 4.6)"
175 This file is a handle for the cgroup namespace of the process.
177 .IR /proc/[pid]/ns/ipc " (since Linux 3.0)"
178 This file is a handle for the IPC namespace of the process.
180 .IR /proc/[pid]/ns/mnt " (since Linux 3.8)"
181 .\" commit 8823c079ba7136dc1948d6f6dcb5f8022bde438e
182 This file is a handle for the mount namespace of the process.
184 .IR /proc/[pid]/ns/net " (since Linux 3.0)"
185 This file is a handle for the network namespace of the process.
187 .IR /proc/[pid]/ns/pid " (since Linux 3.8)"
188 .\" commit 57e8391d327609cbf12d843259c968b9e5c1838f
189 This file is a handle for the PID namespace of the process.
191 .IR /proc/[pid]/ns/user " (since Linux 3.8)"
192 .\" commit cde1975bc242f3e1072bde623ef378e547b73f91
193 This file is a handle for the user namespace of the process.
195 .IR /proc/[pid]/ns/uts " (since Linux 3.0)"
196 This file is a handle for the UTS namespace of the process.
198 Permission to dereference or read
199 .RB ( readlink (2))
200 these symbolic links is governed by a ptrace access mode
201 .B PTRACE_MODE_READ_FSCREDS
202 check; see
203 .BR ptrace (2).
205 .\" ==================== The /proc/sys/user directory ====================
207 .SS The /proc/sys/user directory
208 The files in the
209 .I /proc/sys/user
210 directory (which is present since Linux 4.9) expose limits
211 on the number of namespaces of various types that can be created.
212 The files are as follows:
214 .IR max_cgroup_namespaces
215 The value in this file defines a per-user limit on the number of
216 cgroup namespaces that may be created in the user namespace.
218 .IR max_ipc_namespaces
219 The value in this file defines a per-user limit on the number of
220 ipc namespaces that may be created in the user namespace.
222 .IR max_mnt_namespaces
223 The value in this file defines a per-user limit on the number of
224 mount namespaces that may be created in the user namespace.
226 .IR max_net_namespaces
227 The value in this file defines a per-user limit on the number of
228 network namespaces that may be created in the user namespace.
230 .IR max_pid_namespaces
231 The value in this file defines a per-user limit on the number of
232 pid namespaces that may be created in the user namespace.
234 .IR max_user_namespaces
235 The value in this file defines a per-user limit on the number of
236 user namespaces that may be created in the user namespace.
238 .IR max_uts_namespaces
239 The value in this file defines a per-user limit on the number of
240 user namespaces that may be created in the user namespace.
242 Note the following details about these files:
243 .IP * 3
244 The values in these files are modifiable by privileged processes.
245 .IP *
246 The values exposed by these files are the limits for the user namespace
247 in which the opening process resides.
248 .IP *
249 The limits are per-user.
250 Each user in the same user namespace
251 can create namespaces up to the defined limit.
252 .IP *
253 The limits apply to all users, including UID 0.
254 .IP *
255 These limits apply in addition to any other per-namespace
256 limits (such as those for PID and user namespaces) that may be enforced.
257 .IP *
258 Upon encountering these limits,
259 .BR clone (2)
261 .BR unshare (2)
262 fail with the error
263 .BR ENOSPC .
264 .IP *
265 For the initial user namespace,
266 the default value in each of these files is half the limit on the number
267 of threads that may be created
268 .RI ( /proc/sys/kernel/threads-max ).
269 In all descendant user namespaces, the default value in each file is
270 .BR MAXINT .
271 .IP *
272 When a namespace is created, the object is also accounted
273 against ancestor namespaces.
274 More precisely:
276 .IP + 3
277 Each user namespace has a creator UID.
278 .IP +
279 When a namespace is created,
280 it is accounted against the creator UIDs in each of the
281 ancestor user namespaces,
282 and the kernel ensures that the corresponding namespace limit
283 for the creator UID in the ancestor namespace is not exceeded.
284 .IP +
285 The aforementioned point ensures that creating a new user namespace
286 cannot be used as a means to escape the limits in force
287 in the current user namespace.
291 .\" ==================== Cgroup namespaces ====================
293 .SS Cgroup namespaces (CLONE_NEWCGROUP)
295 .BR cgroup_namespaces (7).
297 .\" ==================== IPC namespaces ====================
299 .SS IPC namespaces (CLONE_NEWIPC)
300 IPC namespaces isolate certain IPC resources,
301 namely, System V IPC objects (see
302 .BR svipc (7))
303 and (since Linux 2.6.30)
304 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
305 .\" https://lwn.net/Articles/312232/
306 POSIX message queues (see
307 .BR mq_overview (7)).
308 The common characteristic of these IPC mechanisms is that IPC
309 objects are identified by mechanisms other than filesystem
310 pathnames.
312 Each IPC namespace has its own set of System V IPC identifiers and
313 its own POSIX message queue filesystem.
314 Objects created in an IPC namespace are visible to all other processes
315 that are members of that namespace,
316 but are not visible to processes in other IPC namespaces.
318 The following
319 .I /proc
320 interfaces are distinct in each IPC namespace:
321 .IP * 3
322 The POSIX message queue interfaces in
323 .IR /proc/sys/fs/mqueue .
324 .IP *
325 The System V IPC interfaces in
326 .IR /proc/sys/kernel ,
327 namely:
328 .IR msgmax ,
329 .IR msgmnb  ,
330 .IR msgmni ,
331 .IR sem ,
332 .IR shmall ,
333 .IR shmmax ,
334 .IR shmmni ,
336 .IR shm_rmid_forced .
337 .IP *
338 The System V IPC interfaces in
339 .IR /proc/sysvipc .
341 When an IPC namespace is destroyed
342 (i.e., when the last process that is a member of the namespace terminates),
343 all IPC objects in the namespace are automatically destroyed.
345 Use of IPC namespaces requires a kernel that is configured with the
346 .B CONFIG_IPC_NS
347 option.
349 .\" ==================== Network namespaces ====================
351 .SS Network namespaces (CLONE_NEWNET)
352 Network namespaces provide isolation of the system resources associated
353 with networking: network devices, IPv4 and IPv6 protocol stacks,
354 IP routing tables, firewalls, the
355 .I /proc/net
356 directory, the
357 .I /sys/class/net
358 directory, port numbers (sockets), and so on.
359 A physical network device can live in exactly one
360 network namespace.
361 A virtual network device ("veth") pair provides a pipe-like abstraction
362 .\" FIXME . Add pointer to veth(4) page when it is eventually completed
363 that can be used to create tunnels between network namespaces,
364 and can be used to create a bridge to a physical network device
365 in another namespace.
367 When a network namespace is freed
368 (i.e., when the last process in the namespace terminates),
369 its physical network devices are moved back to the
370 initial network namespace (not to the parent of the process).
372 Use of network namespaces requires a kernel that is configured with the
373 .B CONFIG_NET_NS
374 option.
376 .\" ==================== Mount namespaces ====================
378 .SS Mount namespaces (CLONE_NEWNS)
380 .BR mount_namespaces (7).
382 .\" ==================== PID namespaces ====================
384 .SS PID namespaces (CLONE_NEWPID)
386 .BR pid_namespaces (7).
388 .\" ==================== User namespaces ====================
390 .SS User namespaces (CLONE_NEWUSER)
392 .BR user_namespaces (7).
394 .\" ==================== UTS namespaces ====================
396 .SS UTS namespaces (CLONE_NEWUTS)
397 UTS namespaces provide isolation of two system identifiers:
398 the hostname and the NIS domain name.
399 These identifiers are set using
400 .BR sethostname (2)
402 .BR setdomainname (2),
403 and can be retrieved using
404 .BR uname (2),
405 .BR gethostname (2),
407 .BR getdomainname (2).
409 Use of UTS namespaces requires a kernel that is configured with the
410 .B CONFIG_UTS_NS
411 option.
413 .SH EXAMPLE
415 .BR user_namespaces (7).
416 .SH SEE ALSO
417 .BR nsenter (1),
418 .BR readlink (1),
419 .BR unshare (1),
420 .BR clone (2),
421 .BR ioctl_ns (2),
422 .BR setns (2),
423 .BR unshare (2),
424 .BR proc (5),
425 .BR capabilities (7),
426 .BR cgroup_namespaces (7),
427 .BR cgroups (7),
428 .BR credentials (7),
429 .BR pid_namespaces (7),
430 .BR user_namespaces (7),
431 .BR ip-netns (8),
432 .BR lsns (8),
433 .BR switch_root (8)