OSDN Git Service

LDP: Update original to LDP v3.75
[linuxjm/LDP_man-pages.git] / original / man7 / namespaces.7
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 2014-09-21 "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.
37
38 Linux provides the following namespaces:
39
40 .TS
41 lB lB lB
42 l lB l.
43 Namespace       Constant        Isolates
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
51
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)
101 and
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.
108 .\"
109 .\" ==================== The /proc/[pid]/ns/ directory ====================
110 .\"
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):
118
119 .in +4n
120 .nf
121 $ \fBls -l /proc/$$/ns\fP
122 total 0
123 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 ipc -> ipc:[4026531839]
124 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 mnt -> mnt:[4026531840]
125 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 net -> net:[4026531956]
126 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 pid -> pid:[4026531836]
127 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 user -> user:[4026531837]
128 lrwxrwxrwx. 1 mtk mtk 0 Jan 14 01:20 uts -> uts:[4026531838]
129 .fi
130 .in
131
132 Bind mounting (see
133 .BR mount (2))
134 one of the files in this directory
135 to somewhere else in the filesystem keeps
136 the corresponding namespace of the process specified by
137 .I pid
138 alive even if all processes currently in the namespace terminate.
139
140 Opening one of the files in this directory
141 (or a file that is bind mounted to one of these files)
142 returns a file handle for
143 the corresponding namespace of the process specified by
144 .IR pid .
145 As long as this file descriptor remains open,
146 the namespace will remain alive,
147 even if all processes in the namespace terminate.
148 The file descriptor can be passed to
149 .BR setns (2).
150
151 In Linux 3.7 and earlier, these files were visible as hard links.
152 Since Linux 3.8, they appear as symbolic links.
153 If two processes are in the same namespace, then the inode numbers of their
154 .IR /proc/[pid]/ns/xxx
155 symbolic links will be the same; an application can check this using the
156 .I stat.st_ino
157 field returned by
158 .BR stat (2).
159 The content of this symbolic link is a string containing
160 the namespace type and inode number as in the following example:
161
162 .in +4n
163 .nf
164 $ \fBreadlink /proc/$$/ns/uts\fP
165 uts:[4026531838]
166 .fi
167 .in
168
169 The files in this subdirectory are as follows:
170 .TP
171 .IR /proc/[pid]/ns/ipc " (since Linux 3.0)"
172 This file is a handle for the IPC namespace of the process.
173 .TP
174 .IR /proc/[pid]/ns/mnt " (since Linux 3.8)"
175 This file is a handle for the mount namespace of the process.
176 .TP
177 .IR /proc/[pid]/ns/net " (since Linux 3.0)"
178 This file is a handle for the network namespace of the process.
179 .TP
180 .IR /proc/[pid]/ns/pid " (since Linux 3.8)"
181 This file is a handle for the PID namespace of the process.
182 .TP
183 .IR /proc/[pid]/ns/user " (since Linux 3.8)"
184 This file is a handle for the user namespace of the process.
185 .TP
186 .IR /proc/[pid]/ns/uts " (since Linux 3.0)"
187 This file is a handle for the UTS namespace of the process.
188 .\"
189 .\" ==================== IPC namespaces ====================
190 .\"
191 .SS IPC namespaces (CLONE_NEWIPC)
192 IPC namespaces isolate certain IPC resources,
193 namely, System V IPC objects (see
194 .BR svipc (7))
195 and (since Linux 2.6.30)
196 .\" commit 7eafd7c74c3f2e67c27621b987b28397110d643f
197 .\" https://lwn.net/Articles/312232/
198 POSIX message queues (see
199 .BR mq_overview (7).
200 The common characteristic of these IPC mechanisms is that IPC
201 objects are identified by mechanisms other than filesystem
202 pathnames.
203
204 Each IPC namespace has its own set of System V IPC identifiers and
205 its own POSIX message queue filesystem.
206 Objects created in an IPC namespace are visible to all other processes
207 that are members of that namespace,
208 but are not visible to processes in other IPC namespaces.
209
210 The following
211 .I /proc
212 interfaces are distinct in each IPC namespace:
213 .IP * 3
214 The POSIX message queue interfaces in
215 .IR /proc/sys/fs/mqueue .
216 .IP *
217 The System V IPC interfaces in
218 .IR /proc/sys/kernel ,
219 namely:
220 .IR msgmax ,
221 .IR msgmnb  ,
222 .IR msgmni ,
223 .IR sem ,
224 .IR shmall ,
225 .IR shmmax ,
226 .IR shmmni ,
227 and
228 .IR shm_rmid_forced .
229 .IP *
230 The System V IPC interfaces in
231 .IR /proc/sysvipc .
232 .PP
233 When an IPC namespace is destroyed
234 (i.e., when the last process that is a member of the namespace terminates),
235 all IPC objects in the namespace are automatically destroyed.
236
237 Use of IPC namespaces requires a kernel that is configured with the
238 .B CONFIG_IPC_NS
239 option.
240 .\"
241 .\" ==================== Network namespaces ====================
242 .\"
243 .SS Network namespaces (CLONE_NEWNET)
244 Network namespaces provide isolation of the system resources associated
245 with networking: network devices, IPv4 and IPv6 protocol stacks,
246 IP routing tables, firewalls, the
247 .I /proc/net
248 directory, the
249 .I /sys/class/net
250 directory, port numbers (sockets), and so on.
251 A physical network device can live in exactly one
252 network namespace.
253 A virtual network device ("veth") pair provides a pipe-like abstraction
254 .\" FIXME Add pointer to veth(4) page when it is eventually completed
255 that can be used to create tunnels between network namespaces,
256 and can be used to create a bridge to a physical network device
257 in another namespace.
258
259 When a network namespace is freed
260 (i.e., when the last process in the namespace terminates),
261 its physical network devices are moved back to the
262 initial network namespace (not to the parent of the process).
263
264 Use of network namespaces requires a kernel that is configured with the
265 .B CONFIG_NET_NS
266 option.
267 .\"
268 .\" ==================== Mount namespaces ====================
269 .\"
270 .SS Mount namespaces (CLONE_NEWNS)
271 Mount namespaces isolate the set of filesystem mount points,
272 meaning that processes in different mount namespaces can
273 have different views of the filesystem hierarchy.
274 The set of mounts in a mount namespace is modified using
275 .BR mount (2)
276 and
277 .BR umount (2).
278
279 The
280 .IR /proc/[pid]/mounts
281 file (present since Linux 2.4.19)
282 lists all the filesystems currently mounted in the
283 process's mount namespace.
284 The format of this file is documented in
285 .BR fstab (5).
286 Since kernel version 2.6.15, this file is pollable:
287 after opening the file for reading, a change in this file
288 (i.e., a filesystem mount or unmount) causes
289 .BR select (2)
290 to mark the file descriptor as readable, and
291 .BR poll (2)
292 and
293 .BR epoll_wait (2)
294 mark the file as having an error condition.
295
296 The
297 .IR /proc/[pid]/mountstats
298 file (present since Linux 2.6.17)
299 exports information (statistics, configuration information)
300 about the mount points in the process's mount namespace.
301 This file is only readable by the owner of the process.
302 Lines in this file have the form:
303 .RS
304 .in 12
305 .nf
306
307 device /dev/sda7 mounted on /home with fstype ext3 [statistics]
308 (       1      )            ( 2 )             (3 ) (4)
309 .fi
310 .in
311
312 The fields in each line are:
313 .TP 5
314 (1)
315 The name of the mounted device
316 (or "nodevice" if there is no corresponding device).
317 .TP
318 (2)
319 The mount point within the filesystem tree.
320 .TP
321 (3)
322 The filesystem type.
323 .TP
324 (4)
325 Optional statistics and configuration information.
326 Currently (as at Linux 2.6.26), only NFS filesystems export
327 information via this field.
328 .RE
329 .\"
330 .\" ==================== PID namespaces ====================
331 .\"
332 .SS PID namespaces (CLONE_NEWPID)
333 See
334 .BR pid_namespaces (7).
335 .\"
336 .\" ==================== User namespaces ====================
337 .\"
338 .SS User namespaces (CLONE_NEWUSER)
339 See
340 .BR user_namespaces (7).
341 .\"
342 .\" ==================== UTS namespaces ====================
343 .\"
344 .SS UTS namespaces (CLONE_NEWUTS)
345 UTS namespaces provide isolation of two system identifiers:
346 the hostname and the NIS domain name.
347 These identifiers are set using
348 .BR sethostname (2)
349 and
350 .BR setdomainname (2),
351 and can be retrieved using
352 .BR uname (2),
353 .BR gethostname (2),
354 and
355 .BR getdomainname (2).
356
357 Use of UTS namespaces requires a kernel that is configured with the
358 .B CONFIG_UTS_NS
359 option.
360 .SH CONFORMING TO
361 Namespaces are a Linux-specific feature.
362 .SH EXAMPLE
363 See
364 .BR user_namespaces (7).
365 .SH SEE ALSO
366 .BR nsenter (1),
367 .BR readlink (1),
368 .BR unshare (1),
369 .BR clone (2),
370 .BR setns (2),
371 .BR unshare (2),
372 .BR proc (5),
373 .BR credentials (7),
374 .BR capabilities (7),
375 .BR pid_namespaces (7),
376 .BR user_namespaces (7),
377 .BR switch_root (8)
378 .SH COLOPHON
379 This page is part of release 3.75 of the Linux
380 .I man-pages
381 project.
382 A description of the project,
383 information about reporting bugs,
384 and the latest version of this page,
385 can be found at
386 \%http://www.kernel.org/doc/man\-pages/.