1 .\" Copyright (c) 2013 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (c) 2012 by Eric W. Biederman <ebiederm@xmission.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PID_NAMESPACES 7 2013-01-14 "Linux" "Linux Programmer's Manual"
27 pid_namespaces \- overview of Linux PID namespaces
29 For an overview of namespaces, see
32 PID namespaces isolate the process ID number space,
33 meaning that processes in different PID namespaces can have the same PID.
34 PID namespaces allow containers to provide functionality
35 such as suspending/resuming the set of processes in the container and
36 migrating the container to a new host
37 while the processes inside the container maintain the same PIDs.
39 PIDs in a new PID namespace start at 1,
40 somewhat like a standalone system, and calls to
45 will produce processes with PIDs that are unique within the namespace.
47 Use of PID namespaces requires a kernel that is configured with the
51 .\" ============================================================
53 .SS The namespace "init" process
54 The first process created in a new namespace
55 (i.e., the process created using
59 flag, or the first child created by a process after a call to
63 flag) has the PID 1, and is the "init" process for the namespace (see
65 A child process that is orphaned within the namespace will be reparented
66 to this process rather than
68 (unless one of the ancestors of the child
69 in the same PID namespace employed the
71 .B PR_GET_CHILD_SUBREAPER
72 command to mark itself as the reaper of orphaned descendant processes).
74 If the "init" process of a PID namespace terminates,
75 the kernel terminates all of the processes in the namespace via a
78 This behavior reflects the fact that the "init" process
79 is essential for the correct operation of a PID namespace.
80 In this case, a subsequent
82 into this PID namespace (e.g., from a process that has done a
84 into the namespace using an open file descriptor for a
86 file corresponding to a process that was in the namespace)
87 will fail with the error
89 it is not possible to create a new processes in a PID namespace whose "init"
90 process has terminated.
92 Only signals for which the "init" process has established a signal handler
93 can be sent to the "init" process by other members of the PID namespace.
94 This restriction applies even to privileged processes,
95 and prevents other members of the PID namespace from
96 accidentally killing the "init" process.
98 Likewise, a process in an ancestor namespace
99 can\(emsubject to the usual permission checks described in
101 signals to the "init" process of a child PID namespace only
102 if the "init" process has established a handler for that signal.
103 (Within the handler, the
112 are treated exceptionally:
113 these signals are forcibly delivered when sent from an ancestor PID namespace.
114 Neither of these signals can be caught by the "init" process,
115 and so will result in the usual actions associated with those signals
116 (respectively, terminating and stopping the process).
118 .\" ============================================================
120 .SS Nesting PID namespaces
121 PID namespaces can be nested:
122 each PID namespace has a parent,
123 except for the initial ("root") PID namespace.
124 The parent of a PID namespace is the PID namespace of the process that
125 created the namespace using
129 PID namespaces thus form a tree,
130 with all namespaces ultimately tracing their ancestry to the root namespace.
132 A process is visible to other processes in its PID namespace,
133 and to the processes in each direct ancestor PID namespace
134 going back to the root PID namespace.
135 In this context, "visible" means that one process
136 can be the target of operations by another process using
137 system calls that specify a process ID.
138 Conversely, the processes in a child PID namespace can't see
139 processes in the parent and further removed ancestor namespace.
140 More succinctly: a process can see (e.g., send signals with
144 etc.) only processes contained in its own PID namespace
145 and in descendants of that namespace.
147 A process has one process ID in each of the layers of the PID
148 namespace hierarchy in which is visible,
149 and walking back though each direct ancestor namespace
150 through to the root PID namespace.
151 System calls that operate on process IDs always
152 operate using the process ID that is visible in the
153 PID namespace of the caller.
156 always returns the PID associated with the namespace in which
157 the process was created.
159 Some processes in a PID namespace may have parents
160 that are outside of the namespace.
161 For example, the parent of the initial process in the namespace
164 process with PID 1) is necessarily in another namespace.
165 Likewise, the direct children of a process that uses
167 to cause its children to join a PID namespace are in a different
168 PID namespace from the caller of
172 for such processes return 0.
174 .\" ============================================================
176 .SS setns(2) and unshare(2) semantics
179 that specify a PID namespace file descriptor
184 flag cause children subsequently created
185 by the caller to be placed in a different PID namespace from the caller.
186 These calls do not, however,
187 change the PID namespace of the calling process,
188 because doing so would change the caller's idea of its own PID
191 which would break many applications and libraries.
193 To put things another way:
194 a process's PID namespace membership is determined when the process is created
195 and cannot be changed thereafter.
196 Among other things, this means that the parental relationship
197 between processes mirrors the parental relationship between PID namespaces:
198 the parent of a process is either in the same namespace
199 or resides in the immediate parent PID namespace.
201 Every thread in a process must be in the same PID namespace.
202 For this reason, the following call sequences will fail:
205 unshare(CLONE_NEWPID);
206 clone(..., CLONE_VM, ...); /* Fails */
208 clone(..., CLONE_VM, ...);
209 unshare(CLONE_NEWPID); /* Fails */
211 setns(fd, CLONE_NEWPID);
212 clone(..., CLONE_VM, ...); /* Fails */
214 clone(..., CLONE_VM, ...);
215 setns(fd, CLONE_NEWPID); /* Fails */
222 calls only change the PID namespace for created children, the
224 calls necessarily put the new thread in a different PID namespace from
227 .\" ============================================================
229 .SS /proc and PID namespaces
232 file system shows (in the
234 directories) only processes visible in the PID namespace
235 of the process that performed the mount, even if the
237 file system is viewed from processes in other namespaces.
239 After creating a new PID namespace,
240 it is useful for the child to change its root directory
241 and mount a new procfs instance at
243 so that tools such as
246 If a new mount namespace is simultaneously created by including
254 then it isn't necessary to change the root directory:
255 a new procfs instance can be mounted directly over
258 From a shell, the command to mount
262 $ mount -t proc proc /proc
268 yields the process ID of the caller in the PID namespace of the procfs mount
269 (i.e., the PID namespace of the process that mounted the procfs).
271 .\" ============================================================
274 When a process ID is passed over a UNIX domain socket to a
275 process in a different PID namespace (see the description of
279 it is translated into the corresponding PID value in
280 the receiving process's PID namespace.
282 Namespaces are a Linux-specific feature.
285 .BR user_namespaces (7).
293 .BR capabilities (7),
294 .BR user_namespaces (7),