]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/namespaces.7
namespaces.7: Adjust example program to show device major and minor numbers
[thirdparty/man-pages.git] / man7 / namespaces.7
CommitLineData
020357e8 1.\" Copyright (c) 2013 by Michael Kerrisk <mtk.manpages@gmail.com>
7a30282c 2.\" and Copyright (c) 2012 by Eric W. Biederman <ebiederm@xmission.com>
020357e8 3.\"
c228b4b4 4.\" %%%LICENSE_START(VERBATIM)
020357e8
MK
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.
c228b4b4 24.\" %%%LICENSE_END
020357e8
MK
25.\"
26.\"
3df541c0 27.TH NAMESPACES 7 2016-07-17 "Linux" "Linux Programmer's Manual"
020357e8
MK
28.SH NAME
29namespaces \- overview of Linux namespaces
30.SH DESCRIPTION
31A namespace wraps a global system resource in an abstraction that
32makes it appear to the processes within the namespace that they
33have their own isolated instance of the global resource.
34Changes to the global resource are visible to other processes
35that are members of the namespace, but are invisible to other processes.
36One use of namespaces is to implement containers.
37
0b497138 38Linux provides the following namespaces:
0b497138
MK
39.TS
40lB lB lB
41l lB l.
42Namespace Constant Isolates
d4d37f0a 43Cgroup CLONE_NEWCGROUP Cgroup root directory
b23c9a79 44IPC CLONE_NEWIPC System V IPC, POSIX message queues
0b497138
MK
45Network CLONE_NEWNET Network devices, stacks, ports, etc.
46Mount CLONE_NEWNS Mount points
47PID CLONE_NEWPID Process IDs
48User CLONE_NEWUSER User and group IDs
49UTS CLONE_NEWUTS Hostname and NIS domain name
50.TE
51
020357e8
MK
52This page describes the various namespaces and the associated
53.I /proc
54files, and summarizes the APIs for working with namespaces.
6be09bd8
MK
55.\"
56.\" ==================== The namespaces API ====================
57.\"
020357e8 58.SS The namespaces API
020357e8
MK
59As well as various
60.I /proc
61files described below,
291e9237 62the namespaces API includes the following system calls:
020357e8
MK
63.TP
64.BR clone (2)
65The
66.BR clone (2)
67system call creates a new process.
68If the
69.I flags
70argument of the call specifies one or more of the
71.B CLONE_NEW*
72flags listed below, then new namespaces are created for each flag,
73and the child process is made a member of those namespaces.
74(This system call also implements a number of features
75unrelated to namespaces.)
020357e8
MK
76.TP
77.BR setns (2)
78The
79.BR setns (2)
80system call allows the calling process to join an existing namespace.
81The namespace to join is specified via a file descriptor that refers to
82one of the
83.IR /proc/[pid]/ns
84files described below.
020357e8
MK
85.TP
86.BR unshare (2)
87The
88.BR unshare (2)
89system call moves the calling process to a new namespace.
90If the
91.I flags
92argument of the call specifies one or more of the
93.B CLONE_NEW*
94flags listed below, then new namespaces are created for each flag,
95and the calling process is made a member of those namespaces.
96(This system call also implements a number of features
97unrelated to namespaces.)
3c7103af 98.PP
027a0716
MK
99Creation of new namespaces using
100.BR clone (2)
101and
102.BR unshare (2)
103in most cases requires the
104.BR CAP_SYS_ADMIN
105capability.
106User namespaces are the exception: since Linux 3.8,
2a4cbd77 107no privilege is required to create a user namespace.
6be09bd8
MK
108.\"
109.\" ==================== The /proc/[pid]/ns/ directory ====================
110.\"
cf8bfe6d 111.SS The /proc/[pid]/ns/ directory
f5d401dd 112Each process has a
cf8bfe6d
MK
113.IR /proc/[pid]/ns/
114.\" See commit 6b4e306aa3dc94a0545eb9279475b1ab6209a31f
115subdirectory containing one entry for each namespace that
116supports being manipulated by
f2752f90
MK
117.BR setns (2):
118
119.in +4n
120.nf
ced6277a 121$ \fBls \-l /proc/$$/ns\fP
f2752f90 122total 0
ced6277a
MK
123lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 cgroup \-> cgroup:[4026531835]
124lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 ipc \-> ipc:[4026531839]
125lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 mnt \-> mnt:[4026531840]
126lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 net \-> net:[4026531969]
127lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 pid \-> pid:[4026531836]
128lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 user \-> user:[4026531837]
129lrwxrwxrwx. 1 mtk mtk 0 Apr 28 12:46 uts \-> uts:[4026531838]
f2752f90
MK
130.fi
131.in
cf8bfe6d
MK
132
133Bind mounting (see
134.BR mount (2))
135one of the files in this directory
ab3311aa 136to somewhere else in the filesystem keeps
cf8bfe6d
MK
137the corresponding namespace of the process specified by
138.I pid
139alive even if all processes currently in the namespace terminate.
140
141Opening one of the files in this directory
142(or a file that is bind mounted to one of these files)
143returns a file handle for
144the corresponding namespace of the process specified by
145.IR pid .
146As long as this file descriptor remains open,
147the namespace will remain alive,
148even if all processes in the namespace terminate.
149The file descriptor can be passed to
150.BR setns (2).
151
152In Linux 3.7 and earlier, these files were visible as hard links.
1dc3d91d
MK
153Since Linux 3.8,
154.\" commit bf056bfa80596a5d14b26b17276a56a0dcb080e5
155they appear as symbolic links.
cf8bfe6d
MK
156If two processes are in the same namespace, then the inode numbers of their
157.IR /proc/[pid]/ns/xxx
158symbolic links will be the same; an application can check this using the
159.I stat.st_ino
160field returned by
161.BR stat (2).
162The content of this symbolic link is a string containing
163the namespace type and inode number as in the following example:
164
165.in +4n
166.nf
167$ \fBreadlink /proc/$$/ns/uts\fP
168uts:[4026531838]
169.fi
170.in
171
7575dbc5 172The symbolic links in this subdirectory are as follows:
cf8bfe6d 173.TP
d4d37f0a
MK
174.IR /proc/[pid]/ns/cgroup " (since Linux 4.6)"
175This file is a handle for the cgroup namespace of the process.
176.TP
cf8bfe6d
MK
177.IR /proc/[pid]/ns/ipc " (since Linux 3.0)"
178This file is a handle for the IPC namespace of the process.
cf8bfe6d
MK
179.TP
180.IR /proc/[pid]/ns/mnt " (since Linux 3.8)"
7eb8372d 181.\" commit 8823c079ba7136dc1948d6f6dcb5f8022bde438e
cf8bfe6d 182This file is a handle for the mount namespace of the process.
cf8bfe6d
MK
183.TP
184.IR /proc/[pid]/ns/net " (since Linux 3.0)"
185This file is a handle for the network namespace of the process.
cf8bfe6d
MK
186.TP
187.IR /proc/[pid]/ns/pid " (since Linux 3.8)"
7eb8372d 188.\" commit 57e8391d327609cbf12d843259c968b9e5c1838f
cf8bfe6d 189This file is a handle for the PID namespace of the process.
cf8bfe6d
MK
190.TP
191.IR /proc/[pid]/ns/user " (since Linux 3.8)"
7eb8372d 192.\" commit cde1975bc242f3e1072bde623ef378e547b73f91
cf8bfe6d 193This file is a handle for the user namespace of the process.
cf8bfe6d
MK
194.TP
195.IR /proc/[pid]/ns/uts " (since Linux 3.0)"
258e6b6c 196This file is a handle for the UTS namespace of the process.
33a1ab5d
MK
197.PP
198Permission to dereference or read
199.RB ( readlink (2))
200these symbolic links is governed by a ptrace access mode
201.B PTRACE_MODE_READ_FSCREDS
202check; see
203.BR ptrace (2).
6be09bd8 204.\"
d4d37f0a
MK
205.\" ==================== Cgroup namespaces ====================
206.\"
207.SS Cgroup namespaces (CLONE_NEWCGROUP)
a2ee61a3
MK
208See
209.BR cgroup_namespaces (7).
d4d37f0a 210.\"
6be09bd8
MK
211.\" ==================== IPC namespaces ====================
212.\"
020357e8 213.SS IPC namespaces (CLONE_NEWIPC)
020357e8
MK
214IPC namespaces isolate certain IPC resources,
215namely, System V IPC objects (see
216.BR svipc (7))
9343f8e7
MK
217and (since Linux 2.6.30)
218.\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
219.\" https://lwn.net/Articles/312232/
220POSIX message queues (see
f7611a00 221.BR mq_overview (7)).
9343f8e7 222The common characteristic of these IPC mechanisms is that IPC
ab3311aa 223objects are identified by mechanisms other than filesystem
9343f8e7
MK
224pathnames.
225
020357e8 226Each IPC namespace has its own set of System V IPC identifiers and
ab3311aa 227its own POSIX message queue filesystem.
9343f8e7
MK
228Objects created in an IPC namespace are visible to all other processes
229that are members of that namespace,
230but are not visible to processes in other IPC namespaces.
231
f344e055
MK
232The following
233.I /proc
234interfaces are distinct in each IPC namespace:
235.IP * 3
236The POSIX message queue interfaces in
237.IR /proc/sys/fs/mqueue .
238.IP *
beb9df9e 239The System V IPC interfaces in
f344e055
MK
240.IR /proc/sys/kernel ,
241namely:
242.IR msgmax ,
243.IR msgmnb ,
244.IR msgmni ,
245.IR sem ,
246.IR shmall ,
247.IR shmmax ,
248.IR shmmni ,
249and
250.IR shm_rmid_forced .
251.IP *
beb9df9e 252The System V IPC interfaces in
f344e055
MK
253.IR /proc/sysvipc .
254.PP
9343f8e7
MK
255When an IPC namespace is destroyed
256(i.e., when the last process that is a member of the namespace terminates),
257all IPC objects in the namespace are automatically destroyed.
258
259Use of IPC namespaces requires a kernel that is configured with the
260.B CONFIG_IPC_NS
261option.
6be09bd8
MK
262.\"
263.\" ==================== Network namespaces ====================
264.\"
020357e8 265.SS Network namespaces (CLONE_NEWNET)
020357e8 266Network namespaces provide isolation of the system resources associated
7d8d64eb
MK
267with networking: network devices, IPv4 and IPv6 protocol stacks,
268IP routing tables, firewalls, the
020357e8 269.I /proc/net
f5d401dd
MK
270directory, the
271.I /sys/class/net
c6d54e1f 272directory, port numbers (sockets), and so on.
73680728
MK
273A physical network device can live in exactly one
274network namespace.
275A virtual network device ("veth") pair provides a pipe-like abstraction
b237b37c 276.\" FIXME . Add pointer to veth(4) page when it is eventually completed
73680728
MK
277that can be used to create tunnels between network namespaces,
278and can be used to create a bridge to a physical network device
279in another namespace.
280
281When a network namespace is freed
282(i.e., when the last process in the namespace terminates),
283its physical network devices are moved back to the
284initial network namespace (not to the parent of the process).
285
286Use of network namespaces requires a kernel that is configured with the
287.B CONFIG_NET_NS
288option.
6be09bd8
MK
289.\"
290.\" ==================== Mount namespaces ====================
291.\"
357002ec 292.SS Mount namespaces (CLONE_NEWNS)
da031af1
MK
293See
294.BR mount_namespaces (7).
6be09bd8
MK
295.\"
296.\" ==================== PID namespaces ====================
297.\"
020357e8 298.SS PID namespaces (CLONE_NEWPID)
024d6a84
MK
299See
300.BR pid_namespaces (7).
6be09bd8
MK
301.\"
302.\" ==================== User namespaces ====================
303.\"
020357e8 304.SS User namespaces (CLONE_NEWUSER)
67d1131f
MK
305See
306.BR user_namespaces (7).
6be09bd8
MK
307.\"
308.\" ==================== UTS namespaces ====================
309.\"
020357e8 310.SS UTS namespaces (CLONE_NEWUTS)
020357e8
MK
311UTS namespaces provide isolation of two system identifiers:
312the hostname and the NIS domain name.
313These identifiers are set using
314.BR sethostname (2)
315and
316.BR setdomainname (2),
317and can be retrieved using
318.BR uname (2),
319.BR gethostname (2),
320and
321.BR getdomainname (2).
322
83d9e9b2
MK
323Use of UTS namespaces requires a kernel that is configured with the
324.B CONFIG_UTS_NS
325option.
6143dbbf
MK
326.\"
327.\" ============================================================
328.\"
329.SS Introspecting namespace relationships
330Since Linux 4.9,
331.\" commit bcac25a58bfc6bd79191ac5d7afb49bea96da8c9
332.\" commit 6786741dbf99e44fb0c0ed85a37582b8a26f1c3b
333.\" commit a7306ed8d94af729ecef8b6e37506a1c6fc14788
334.\" commit 6ad92bf63e45f97e306da48cd1cbce6e4fef1e5d
335two
336.BR ioctl (2)
337operations are provided to allow introspection of namespace relationships
338(see
339.BR user_namespaces (7)
340and
341.BR pid_namespaces (7)).
342The form of the calls is:
343
57422589 344 new_fd = ioctl(fd, request);
6143dbbf
MK
345
346In each case,
347.I fd
348refers to a
349.IR /proc/[pid]/ns/*
350file.
57422589 351Both operations return a new file descriptor on success.
6143dbbf
MK
352.TP
353.BR NS_GET_USERNS
354Returns a file descriptor that refers to the owning user namespace
355for the namespace referred to by
356.IR fd .
357.TP
358.BR NS_GET_PARENT
359Returns a file descriptor that refers to the parent namespace of
360the namespace referred to by
361.IR fd .
362This operation is valid only for hierarchical namespaces
363(i.e., PID and user namespaces).
364For user namespaces,
365.BR NS_GET_PARENT
366is synonymous with
367.BR NS_GET_USERNS .
368.PP
57422589 369The new file descriptor returned by these operations is opened with the
6143dbbf
MK
370.BR O_RDONLY
371and
372.BR O_CLOEXEC
57422589
MK
373(close-on-exec; see
374.BR fcntl (2)) flags.
6143dbbf
MK
375.PP
376By applying
377.BR fstat (2)
378to the returned file descriptor, one obtains a
379.I stat
380structure whose
e0ff749f
MK
381.I st_dev
382(resident device) and
6143dbbf 383.I st_ino
e0ff749f 384(inode number) fields together identify the owning/parent namespace.
6143dbbf
MK
385This inode number can be matched with the inode number of another
386.IR /proc/[pid]/ns/{pid,user}
387file to determine whether that is the owning/parent namespace.
388
389Either of these
390.BR ioctl (2)
58615b43 391operations can fail with the following errors:
6143dbbf
MK
392.TP
393.B EPERM
394The requested namespace is outside of the caller's namespace scope.
395This error can occur if, for example, the owning user namespace is an
396ancestor of the caller's current user namespace.
397It can also occur on attempts to obtain the parent of the initial
398user or PID namespace.
58615b43
MK
399.TP
400.B ENOTTY
401The operation is not supported by this kernel version.
6143dbbf
MK
402.PP
403Additionally, the
404.B NS_GET_PARENT
405operation can fail with the following error:
406.TP
407.B EINVAL
408.I fd
409refers to a nonhierarchical namespace.
410.PP
411See the EXAMPLE section for an example of the use of these operations.
020357e8
MK
412.SH CONFORMING TO
413Namespaces are a Linux-specific feature.
fa88d1a4 414.SH EXAMPLE
6143dbbf 415For one example,
fa88d1a4 416.BR user_namespaces (7).
6143dbbf
MK
417
418The example shown below uses the
419.BR ioctl (2)
420operations described above to perform simple
421introspection of namespace relationships.
422The following shell sessions show various examples of the use
423of this program.
424
425Trying to get the parent of the initial user namespace fails,
426for the reasons explained earlier:
427
428.nf
429.in +4n
430$ \fB./ns_introspect /proc/self/ns/user p\fP
431The parent namespace is outside your namespace scope
432.in
433.fi
434
435Create a process running
436.BR sleep (1)
437that resides in new user and UTS namespaces,
438and show that new UTS namespace is associated with the new user namespace:
439
440.nf
441.in +4n
442$ \fBunshare \-Uu sleep 1000 &\fP
443[1] 23235
444$ \fB./ns_introspect /proc/23235/ns/uts\fP
e6f1b08f 445Device/Inode of owning user namespace is: [0,3] / 4026532448
6143dbbf
MK
446$ \fBreadlink /proc/23235/ns/user \fP
447user:[4026532448]
448.in
449.fi
450
451Then show that the parent of the new user namespace in the preceding
452example is the initial user namespace:
453
454.nf
455.in +4n
456$ \fBreadlink /proc/self/ns/user\fP
457user:[4026531837]
458$ \fB./ns_introspect /proc/23235/ns/user\fP
e6f1b08f 459Device/Inode of owning user namespace is: [0,3] / 4026531837
6143dbbf
MK
460.in
461.fi
462
463Start a shell in a new user namespace, and show that from within
464this shell, the parent user namespace can't be discovered.
465Similarly, the UTS namespace
466(which is associated with the initial user namespace)
467can't be discovered.
468
469.nf
470.in +4n
519949ec 471$ \fBPS1="sh2$ " unshare \-U bash\fP
6143dbbf
MK
472sh2$ \fB./ns_introspect /proc/self/ns/user p\fP
473The parent namespace is outside your namespace scope
474sh2$ \fB./ns_introspect /proc/self/ns/uts u\fP
475The owning user namespace is outside your namespace scope
476.in
477.fi
478.SS Program source
479\&
480.nf
481/* ns_introspect.c
482
e6f1b08f 483 Licensed under the GNU General Public License v2 or later.
6143dbbf
MK
484*/
485#include <stdlib.h>
486#include <unistd.h>
487#include <stdio.h>
6143dbbf 488#include <fcntl.h>
6143dbbf 489#include <string.h>
e6f1b08f
MK
490#include <sys/stat.h>
491#include <sys/ioctl.h>
6143dbbf 492#include <errno.h>
e6f1b08f 493#include <sys/sysmacros.h>
6143dbbf
MK
494
495#ifndef NS_GET_USERNS
496#define NSIO 0xb7
497#define NS_GET_USERNS _IO(NSIO, 0x1)
498#define NS_GET_PARENT _IO(NSIO, 0x2)
499#endif
500
501int
502main(int argc, char *argv[])
503{
504 int fd, userns_fd, parent_fd;
505 struct stat sb;
506
507 if (argc < 2) {
508 fprintf(stderr, "Usage: %s /proc/[pid]/ns/[file] [p|u]\\n",
509 argv[0]);
510 fprintf(stderr, "\\nDisplay the result of one or both "
511 "of NS_GET_USERNS (u) or NS_GET_PARENT (p)\\n"
512 "for the specified /proc/[pid]/ns/[file]. If neither "
513 "\(aqp\(aq nor \(aqu\(aq is specified,\\n"
514 "NS_GET_USERNS is the default.\\n");
515 exit(EXIT_FAILURE);
516 }
517
518 /* Obtain a file descriptor for the \(aqns\(aq file specified
519 in argv[1] */
520
521 fd = open(argv[1], O_RDONLY);
522 if (fd == \-1) {
523 perror("open");
524 exit(EXIT_FAILURE);
525 }
526
527 /* Obtain a file descriptor for the owning user namespace and
528 then obtain and display the inode number of that namespace */
529
530 if (argc < 3 || strchr(argv[2], \(aqu\(aq)) {
531 userns_fd = ioctl(fd, NS_GET_USERNS);
532
533 if (userns_fd == \-1) {
534 if (errno == EPERM)
535 printf("The owning user namespace is outside "
536 "your namespace scope\\n");
537 else
538 perror("ioctl\-NS_GET_USERNS");
539 exit(EXIT_FAILURE);
540 }
541
542 if (fstat(userns_fd, &sb) == \-1) {
543 perror("fstat\-userns");
544 exit(EXIT_FAILURE);
545 }
e6f1b08f
MK
546 printf("Device/Inode of owning user namespace is: "
547 "[%lx,%lx] / %ld\\n",
548 (long) major(sb.st_dev), (long) minor(sb.st_dev),
6143dbbf
MK
549 (long) sb.st_ino);
550
551 close(userns_fd);
552 }
553
554 /* Obtain a file descriptor for the parent namespace and
555 then obtain and display the inode number of that namespace */
556
557 if (argc > 2 && strchr(argv[2], \(aqp\(aq)) {
558 parent_fd = ioctl(fd, NS_GET_PARENT);
559
560 if (parent_fd == \-1) {
561 if (errno == EINVAL)
562 printf("Can\(aq get parent namespace of a "
563 "nonhierarchical namespace\\n");
564 else if (errno == EPERM)
565 printf("The parent namespace is outside "
566 "your namespace scope\\n");
567 else
568 perror("ioctl\-NS_GET_PARENT");
569 exit(EXIT_FAILURE);
570 }
571
572 if (fstat(parent_fd, &sb) == \-1) {
573 perror("fstat\-parentns");
574 exit(EXIT_FAILURE);
575 }
e6f1b08f
MK
576 printf("Device/Inode of parent namespace is: [%lx,%lx] / %ld\\n",
577 (long) major(sb.st_dev), (long) minor(sb.st_dev),
6143dbbf
MK
578 (long) sb.st_ino);
579
580 close(parent_fd);
581 }
582
583 exit(EXIT_SUCCESS);
584}
585.fi
020357e8 586.SH SEE ALSO
86499a6b 587.BR nsenter (1),
020357e8 588.BR readlink (1),
86499a6b 589.BR unshare (1),
020357e8
MK
590.BR clone (2),
591.BR setns (2),
592.BR unshare (2),
593.BR proc (5),
029ae9e3 594.BR capabilities (7),
a2ee61a3 595.BR cgroup_namespaces (7),
35fae0aa 596.BR cgroups (7),
10f8f8cb 597.BR credentials (7),
024d6a84 598.BR pid_namespaces (7),
67d1131f 599.BR user_namespaces (7),
8512495a 600.BR lsns (8),
029ae9e3 601.BR switch_root (8)