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