1NAMESPACES(7) Linux Programmer's Manual NAMESPACES(7)
2
3
4
6 namespaces - overview of Linux namespaces
7
9 A namespace wraps a global system resource in an abstraction that makes
10 it appear to the processes within the namespace that they have their
11 own isolated instance of the global resource. Changes to the global
12 resource are visible to other processes that are members of the names‐
13 pace, but are invisible to other processes. One use of namespaces is
14 to implement containers.
15
16 This page provides pointers to information on the various namespace
17 types, describes the associated /proc files, and summarizes the APIs
18 for working with namespaces.
19
20 Namespace types
21 The following table shows the namespace types available on Linux. The
22 second column of the table shows the flag value that is used to specify
23 the namespace type in various APIs. The third column identifies the
24 manual page that provides details on the namespace type. The last col‐
25 umn is a summary of the resources that are isolated by the namespace
26 type.
27
28 Namespace Flag Page Isolates
29 Cgroup CLONE_NEWCGROUP cgroup_namespaces(7) Cgroup root directory
30 IPC CLONE_NEWIPC ipc_namespaces(7) System V IPC,
31 POSIX message queues
32 Network CLONE_NEWNET network_namespaces(7) Network devices,
33 stacks, ports, etc.
34 Mount CLONE_NEWNS mount_namespaces(7) Mount points
35 PID CLONE_NEWPID pid_namespaces(7) Process IDs
36 Time CLONE_NEWTIME time_namespaces(7) Boot and monotonic
37 clocks
38 User CLONE_NEWUSER user_namespaces(7) User and group IDs
39 UTS CLONE_NEWUTS uts_namespaces(7) Hostname and NIS
40 domain name
41
42 The namespaces API
43 As well as various /proc files described below, the namespaces API
44 includes the following system calls:
45
46 clone(2)
47 The clone(2) system call creates a new process. If the flags
48 argument of the call specifies one or more of the CLONE_NEW*
49 flags listed below, then new namespaces are created for each
50 flag, and the child process is made a member of those names‐
51 paces. (This system call also implements a number of features
52 unrelated to namespaces.)
53
54 setns(2)
55 The setns(2) system call allows the calling process to join an
56 existing namespace. The namespace to join is specified via a
57 file descriptor that refers to one of the /proc/[pid]/ns files
58 described below.
59
60 unshare(2)
61 The unshare(2) system call moves the calling process to a new
62 namespace. If the flags argument of the call specifies one or
63 more of the CLONE_NEW* flags listed below, then new namespaces
64 are created for each flag, and the calling process is made a
65 member of those namespaces. (This system call also implements a
66 number of features unrelated to namespaces.)
67
68 ioctl(2)
69 Various ioctl(2) operations can be used to discover information
70 about namespaces. These operations are described in
71 ioctl_ns(2).
72
73 Creation of new namespaces using clone(2) and unshare(2) in most cases
74 requires the CAP_SYS_ADMIN capability, since, in the new namespace, the
75 creator will have the power to change global resources that are visible
76 to other processes that are subsequently created in, or join the names‐
77 pace. User namespaces are the exception: since Linux 3.8, no privilege
78 is required to create a user namespace.
79
80 The /proc/[pid]/ns/ directory
81 Each process has a /proc/[pid]/ns/ subdirectory containing one entry
82 for each namespace that supports being manipulated by setns(2):
83
84 $ ls -l /proc/$$/ns | awk '{print $1, $9, $10, $11}'
85 total 0
86 lrwxrwxrwx. cgroup -> cgroup:[4026531835]
87 lrwxrwxrwx. ipc -> ipc:[4026531839]
88 lrwxrwxrwx. mnt -> mnt:[4026531840]
89 lrwxrwxrwx. net -> net:[4026531969]
90 lrwxrwxrwx. pid -> pid:[4026531836]
91 lrwxrwxrwx. pid_for_children -> pid:[4026531834]
92 lrwxrwxrwx. time -> time:[4026531834]
93 lrwxrwxrwx. time_for_children -> time:[4026531834]
94 lrwxrwxrwx. user -> user:[4026531837]
95 lrwxrwxrwx. uts -> uts:[4026531838]
96
97 Bind mounting (see mount(2)) one of the files in this directory to
98 somewhere else in the filesystem keeps the corresponding namespace of
99 the process specified by pid alive even if all processes currently in
100 the namespace terminate.
101
102 Opening one of the files in this directory (or a file that is bind
103 mounted to one of these files) returns a file handle for the corre‐
104 sponding namespace of the process specified by pid. As long as this
105 file descriptor remains open, the namespace will remain alive, even if
106 all processes in the namespace terminate. The file descriptor can be
107 passed to setns(2).
108
109 In Linux 3.7 and earlier, these files were visible as hard links.
110 Since Linux 3.8, they appear as symbolic links. If two processes are
111 in the same namespace, then the device IDs and inode numbers of their
112 /proc/[pid]/ns/xxx symbolic links will be the same; an application can
113 check this using the stat.st_dev and stat.st_ino fields returned by
114 stat(2). The content of this symbolic link is a string containing the
115 namespace type and inode number as in the following example:
116
117 $ readlink /proc/$$/ns/uts
118 uts:[4026531838]
119
120 The symbolic links in this subdirectory are as follows:
121
122 /proc/[pid]/ns/cgroup (since Linux 4.6)
123 This file is a handle for the cgroup namespace of the process.
124
125 /proc/[pid]/ns/ipc (since Linux 3.0)
126 This file is a handle for the IPC namespace of the process.
127
128 /proc/[pid]/ns/mnt (since Linux 3.8)
129 This file is a handle for the mount namespace of the process.
130
131 /proc/[pid]/ns/net (since Linux 3.0)
132 This file is a handle for the network namespace of the process.
133
134 /proc/[pid]/ns/pid (since Linux 3.8)
135 This file is a handle for the PID namespace of the process.
136 This handle is permanent for the lifetime of the process (i.e.,
137 a process's PID namespace membership never changes).
138
139 /proc/[pid]/ns/pid_for_children (since Linux 4.12)
140 This file is a handle for the PID namespace of child processes
141 created by this process. This can change as a consequence of
142 calls to unshare(2) and setns(2) (see pid_namespaces(7)), so the
143 file may differ from /proc/[pid]/ns/pid. The symbolic link
144 gains a value only after the first child process is created in
145 the namespace. (Beforehand, readlink(2) of the symbolic link
146 will return an empty buffer.)
147
148 /proc/[pid]/ns/time (since Linux 5.6)
149 This file is a handle for the time namespace of the process.
150
151 /proc/[pid]/ns/time_for_children (since Linux 5.6)
152 This file is a handle for the time namespace of child processes
153 created by this process. This can change as a consequence of
154 calls to unshare(2) and setns(2) (see time_namespaces(7)), so
155 the file may differ from /proc/[pid]/ns/time.
156
157 /proc/[pid]/ns/user (since Linux 3.8)
158 This file is a handle for the user namespace of the process.
159
160 /proc/[pid]/ns/uts (since Linux 3.0)
161 This file is a handle for the UTS namespace of the process.
162
163 Permission to dereference or read (readlink(2)) these symbolic links is
164 governed by a ptrace access mode PTRACE_MODE_READ_FSCREDS check; see
165 ptrace(2).
166
167 The /proc/sys/user directory
168 The files in the /proc/sys/user directory (which is present since Linux
169 4.9) expose limits on the number of namespaces of various types that
170 can be created. The files are as follows:
171
172 max_cgroup_namespaces
173 The value in this file defines a per-user limit on the number of
174 cgroup namespaces that may be created in the user namespace.
175
176 max_ipc_namespaces
177 The value in this file defines a per-user limit on the number of
178 ipc namespaces that may be created in the user namespace.
179
180 max_mnt_namespaces
181 The value in this file defines a per-user limit on the number of
182 mount namespaces that may be created in the user namespace.
183
184 max_net_namespaces
185 The value in this file defines a per-user limit on the number of
186 network namespaces that may be created in the user namespace.
187
188 max_pid_namespaces
189 The value in this file defines a per-user limit on the number of
190 PID namespaces that may be created in the user namespace.
191
192 max_time_namespaces (since Linux 5.7)
193 The value in this file defines a per-user limit on the number of
194 time namespaces that may be created in the user namespace.
195
196 max_user_namespaces
197 The value in this file defines a per-user limit on the number of
198 user namespaces that may be created in the user namespace.
199
200 max_uts_namespaces
201 The value in this file defines a per-user limit on the number of
202 uts namespaces that may be created in the user namespace.
203
204 Note the following details about these files:
205
206 * The values in these files are modifiable by privileged processes.
207
208 * The values exposed by these files are the limits for the user names‐
209 pace in which the opening process resides.
210
211 * The limits are per-user. Each user in the same user namespace can
212 create namespaces up to the defined limit.
213
214 * The limits apply to all users, including UID 0.
215
216 * These limits apply in addition to any other per-namespace limits
217 (such as those for PID and user namespaces) that may be enforced.
218
219 * Upon encountering these limits, clone(2) and unshare(2) fail with
220 the error ENOSPC.
221
222 * For the initial user namespace, the default value in each of these
223 files is half the limit on the number of threads that may be created
224 (/proc/sys/kernel/threads-max). In all descendant user namespaces,
225 the default value in each file is MAXINT.
226
227 * When a namespace is created, the object is also accounted against
228 ancestor namespaces. More precisely:
229
230 + Each user namespace has a creator UID.
231
232 + When a namespace is created, it is accounted against the creator
233 UIDs in each of the ancestor user namespaces, and the kernel
234 ensures that the corresponding namespace limit for the creator
235 UID in the ancestor namespace is not exceeded.
236
237 + The aforementioned point ensures that creating a new user names‐
238 pace cannot be used as a means to escape the limits in force in
239 the current user namespace.
240
241 Namespace lifetime
242 Absent any other factors, a namespace is automatically torn down when
243 the last process in the namespace terminates or leaves the namespace.
244 However, there are a number of other factors that may pin a namespace
245 into existence even though it has no member processes. These factors
246 include the following:
247
248 * An open file descriptor or a bind mount exists for the corresponding
249 /proc/[pid]/ns/* file.
250
251 * The namespace is hierarchical (i.e., a PID or user namespace), and
252 has a child namespace.
253
254 * It is a user namespace that owns one or more nonuser namespaces.
255
256 * It is a PID namespace, and there is a process that refers to the
257 namespace via a /proc/[pid]/ns/pid_for_children symbolic link.
258
259 * It is an IPC namespace, and a corresponding mount of an mqueue
260 filesystem (see mq_overview(7)) refers to this namespace.
261
262 * It is a PID namespace, and a corresponding mount of a proc(5)
263 filesystem refers to this namespace.
264
266 See clone(2) and user_namespaces(7).
267
269 nsenter(1), readlink(1), unshare(1), clone(2), ioctl_ns(2), setns(2),
270 unshare(2), proc(5), capabilities(7), cgroup_namespaces(7), cgroups(7),
271 credentials(7), ipc_namespaces(7), network_namespaces(7), pid_names‐
272 paces(7), user_namespaces(7), uts_namespaces(7), lsns(8), pam_names‐
273 pace(8), switch_root(8)
274
276 This page is part of release 5.07 of the Linux man-pages project. A
277 description of the project, information about reporting bugs, and the
278 latest version of this page, can be found at
279 https://www.kernel.org/doc/man-pages/.
280
281
282
283Linux 2020-04-11 NAMESPACES(7)