Debian Codegraph @

upstream/debian-unstable/man2/setns.2

raw
1 .\" Copyright 2011, Eric W. Biederman <[email protected]>
2 .\" Copyright 2011-2012, Michael Kerrisk <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: GPL-2.0-only
6 .\"
7 .TH setns 2 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 setns \- reassociate thread with a namespace
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .B #include <sched.h>
17 .P
18 .BI "int setns(int " fd ", int " nstype );
19 .fi
20 .SH DESCRIPTION
21 The
22 .BR setns ()
23 system call allows the calling thread to move into different namespaces.
24 The
25 .I fd
26 argument is one of the following:
27 .IP \[bu] 3
28 a file descriptor referring to one of the magic links in a
29 .IR /proc/ pid /ns/
30 directory (or a bind mount to such a link);
31 .IP \[bu]
32 a PID file descriptor (see
33 .BR pidfd_open (2)).
34 .P
35 The
36 .I nstype
37 argument is interpreted differently in each case.
38 .\"
39 .SS fd refers to a \f[I]/proc/\f[]pid\f[I]/ns/\f[] link
40 If
41 .I fd
42 refers to a
43 .IR /proc/ pid /ns/
44 link, then
45 .BR setns ()
46 reassociates the calling thread with the namespace associated with that link,
47 subject to any constraints imposed by the
48 .I nstype
49 argument.
50 In this usage, each call to
51 .BR setns ()
52 changes just one of the caller's namespace memberships.
53 .P
54 The
55 .I nstype
56 argument specifies which type of namespace
57 the calling thread may be reassociated with.
58 This argument can have
59 .I one
60 of the following values:
61 .TP
62 .B 0
63 Allow any type of namespace to be joined.
64 .TP
65 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
66 .I fd
67 must refer to a cgroup namespace.
68 .TP
69 .BR CLONE_NEWIPC " (since Linux 3.0)"
70 .I fd
71 must refer to an IPC namespace.
72 .TP
73 .BR CLONE_NEWNET " (since Linux 3.0)"
74 .I fd
75 must refer to a network namespace.
76 .TP
77 .BR CLONE_NEWNS " (since Linux 3.8)"
78 .I fd
79 must refer to a mount namespace.
80 .TP
81 .BR CLONE_NEWPID " (since Linux 3.8)"
82 .I fd
83 must refer to a descendant PID namespace.
84 .TP
85 .BR CLONE_NEWTIME " (since Linux 5.8)"
86 .\" commit 76c12881a38aaa83e1eb4ce2fada36c3a732bad4
87 .I fd
88 must refer to a time namespace.
89 .TP
90 .BR CLONE_NEWUSER " (since Linux 3.8)"
91 .I fd
92 must refer to a user namespace.
93 .TP
94 .BR CLONE_NEWUTS " (since Linux 3.0)"
95 .I fd
96 must refer to a UTS namespace.
97 .P
98 Specifying
99 .I nstype
100 as 0 suffices if the caller knows (or does not care)
101 what type of namespace is referred to by
102 .IR fd .
103 Specifying a nonzero value for
104 .I nstype
105 is useful if the caller does not know what type of namespace is referred to by
106 .I fd
107 and wants to ensure that the namespace is of a particular type.
108 (The caller might not know the type of the namespace referred to by
109 .I fd
110 if the file descriptor was opened by another process and, for example,
111 passed to the caller via a UNIX domain socket.)
112 .\"
113 .SS fd is a PID file descriptor
114 Since Linux 5.8,
115 .I fd
116 may refer to a PID file descriptor obtained from
117 .BR pidfd_open (2)
118 or
119 .BR clone (2).
120 In this usage,
121 .BR setns ()
122 atomically moves the calling thread into one or more of the same namespaces
123 as the thread referred to by
124 .IR fd .
125 .P
126 The
127 .I nstype
128 argument is a bit mask specified by ORing together
129 .I "one or more"
130 of the
131 .B CLONE_NEW*
132 namespace constants listed above.
133 The caller is moved into each of the target thread's namespaces
134 that is specified in
135 .IR nstype ;
136 the caller's memberships in the remaining namespaces are left unchanged.
137 .P
138 For example, the following code would move the caller into the
139 same user, network, and UTS namespaces as PID 1234,
140 but would leave the caller's other namespace memberships unchanged:
141 .P
142 .in +4n
143 .EX
144 int fd = pidfd_open(1234, 0);
145 setns(fd, CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS);
146 .EE
147 .in
148 .\"
149 .SS Details for specific namespace types
150 Note the following details and restrictions when reassociating with
151 specific namespace types:
152 .TP
153 User namespaces
154 A process reassociating itself with a user namespace must have the
155 .B CAP_SYS_ADMIN
156 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
157 capability in the target user namespace.
158 (This necessarily implies that it is only possible to join
159 a descendant user namespace.)
160 Upon successfully joining a user namespace,
161 a process is granted all capabilities in that namespace,
162 regardless of its user and group IDs.
163 .IP
164 A multithreaded process may not change user namespace with
165 .BR setns ().
166 .IP
167 It is not permitted to use
168 .BR setns ()
169 to reenter the caller's current user namespace.
170 This prevents a caller that has dropped capabilities from regaining
171 those capabilities via a call to
172 .BR setns ().
173 .IP
174 For security reasons,
175 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
176 .\" https://lwn.net/Articles/543273/
177 a process can't join a new user namespace if it is sharing
178 filesystem-related attributes
179 (the attributes whose sharing is controlled by the
180 .BR clone (2)
181 .B CLONE_FS
182 flag) with another process.
183 .IP
184 For further details on user namespaces, see
185 .BR user_namespaces (7).
186 .TP
187 Mount namespaces
188 Changing the mount namespace requires that the caller possess both
189 .B CAP_SYS_CHROOT
190 and
191 .B CAP_SYS_ADMIN
192 capabilities in its own user namespace and
193 .B CAP_SYS_ADMIN
194 in the user namespace that owns the target mount namespace.
195 .IP
196 A process can't join a new mount namespace if it is sharing
197 filesystem-related attributes
198 (the attributes whose sharing is controlled by the
199 .BR clone (2)
200 .B CLONE_FS
201 flag) with another process.
202 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
203 .IP
204 See
205 .BR user_namespaces (7)
206 for details on the interaction of user namespaces and mount namespaces.
207 .TP
208 PID namespaces
209 In order to reassociate itself with a new PID namespace,
210 the caller must have the
211 .B CAP_SYS_ADMIN
212 capability both in its own user namespace and in the user namespace
213 that owns the target PID namespace.
214 .IP
215 Reassociating the PID namespace has somewhat different
216 from other namespace types.
217 Reassociating the calling thread with a PID namespace changes only
218 the PID namespace that subsequently created child processes of
219 the caller will be placed in;
220 it does not change the PID namespace of the caller itself.
221 .IP
222 Reassociating with a PID namespace is allowed only if the target
223 PID namespace is a descendant (child, grandchild, etc.)
224 of, or is the same as, the current PID namespace of the caller.
225 .IP
226 For further details on PID namespaces, see
227 .BR pid_namespaces (7).
228 .TP
229 Cgroup namespaces
230 In order to reassociate itself with a new cgroup namespace,
231 the caller must have the
232 .B CAP_SYS_ADMIN
233 capability both in its own user namespace and in the user namespace
234 that owns the target cgroup namespace.
235 .IP
236 Using
237 .BR setns ()
238 to change the caller's cgroup namespace does not change
239 the caller's cgroup memberships.
240 .TP
241 Time namespaces
242 In order to reassociate itself with a new time namespace,
243 the caller must have the
244 .B CAP_SYS_ADMIN
245 capability both in its own user namespace and in the user namespace
246 that owns the target namespace.
247 .IP
248 A multithreaded process may not change time namespace with
249 .BR setns ().
250 .TP
251 Network, IPC, and UTS namespaces
252 In order to reassociate itself with a new network, IPC, time, or UTS namespace,
253 the caller must have the
254 .B CAP_SYS_ADMIN
255 capability both in its own user namespace and in the user namespace
256 that owns the target namespace.
257 .SH RETURN VALUE
258 On success,
259 .BR setns ()
260 returns 0.
261 On failure, \-1 is returned and
262 .I errno
263 is set to indicate the error.
264 .SH ERRORS
265 .TP
266 .B EBADF
267 .I fd
268 is not a valid file descriptor.
269 .TP
270 .B EINVAL
271 .I fd
272 refers to a namespace whose type does not match that specified in
273 .IR nstype .
274 .TP
275 .B EINVAL
276 There is a problem with reassociating
277 the thread with the specified namespace.
278 .TP
279 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
280 .B EINVAL
281 The caller tried to join an ancestor (parent, grandparent, and so on)
282 PID namespace.
283 .TP
284 .B EINVAL
285 The caller attempted to join the user namespace
286 in which it is already a member.
287 .TP
288 .B EINVAL
289 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
290 The caller shares filesystem
291 .RB ( CLONE_FS )
292 state (in particular, the root directory)
293 with other processes and tried to join a new user namespace.
294 .TP
295 .B EINVAL
296 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
297 The caller is multithreaded and tried to join a new user namespace.
298 .TP
299 .B EINVAL
300 .I fd
301 is a PID file descriptor and
302 .I nstype
303 is invalid (e.g., it is 0).
304 .TP
305 .B ENOMEM
306 Cannot allocate sufficient memory to change the specified namespace.
307 .TP
308 .B EPERM
309 The calling thread did not have the required capability
310 for this operation.
311 .TP
312 .B ESRCH
313 .I fd
314 is a PID file descriptor but the process it refers to no longer exists
315 (i.e., it has terminated and been waited on).
316 .SH STANDARDS
317 Linux.
318 .SH VERSIONS
319 Linux 3.0,
320 glibc 2.14.
321 .SH NOTES
322 For further information on the
323 .IR /proc/ pid /ns/
324 magic links, see
325 .BR namespaces (7).
326 .P
327 Not all of the attributes that can be shared when
328 a new thread is created using
329 .BR clone (2)
330 can be changed using
331 .BR setns ().
332 .SH EXAMPLES
333 The program below takes two or more arguments.
334 The first argument specifies the pathname of a namespace file in an existing
335 .IR /proc/ pid /ns/
336 directory.
337 The remaining arguments specify a command and its arguments.
338 The program opens the namespace file, joins that namespace using
339 .BR setns (),
340 and executes the specified command inside that namespace.
341 .P
342 The following shell session demonstrates the use of this program
343 (compiled as a binary named
344 .IR ns_exec )
345 in conjunction with the
346 .B CLONE_NEWUTS
347 example program in the
348 .BR clone (2)
349 man page (complied as a binary named
350 .IR newuts ).
351 .P
352 We begin by executing the example program in
353 .BR clone (2)
354 in the background.
355 That program creates a child in a separate UTS namespace.
356 The child changes the hostname in its namespace,
357 and then both processes display the hostnames in their UTS namespaces,
358 so that we can see that they are different.
359 .P
360 .in +4n
361 .EX
362 .RB $ " su" "; # Need privilege for namespace operations"
363 Password:
364 .RB # " ./newuts bizarro &"
365 [1] 3549
366 clone() returned 3550
367 uts.nodename in child: bizarro
368 uts.nodename in parent: antero
369 .RB # " uname \-n" "; # Verify hostname in the shell"
370 antero
371 .EE
372 .in
373 .P
374 We then run the program shown below,
375 using it to execute a shell.
376 Inside that shell, we verify that the hostname is the one
377 set by the child created by the first program:
378 .P
379 .in +4n
380 .EX
381 .RB # " ./ns_exec /proc/3550/ns/uts /bin/bash" ;
382 .RB # " uname \-n" "; # Executed in shell started by ns_exec"
383 bizarro
384 .EE
385 .in
386 .SS Program source
387 .\" SRC BEGIN (setns.c)
388 .EX
389 #define _GNU_SOURCE
390 #include <err.h>
391 #include <fcntl.h>
392 #include <sched.h>
393 #include <stdio.h>
394 #include <stdlib.h>
395 #include <unistd.h>
396 \&
397 int
398 main(int argc, char *argv[])
399 {
400 int fd;
401 \&
402 if (argc < 3) {
403 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\[rs]n", argv[0]);
404 exit(EXIT_FAILURE);
405 }
406 \&
407 /* Get file descriptor for namespace; the file descriptor is opened
408 with O_CLOEXEC so as to ensure that it is not inherited by the
409 program that is later executed. */
410 \&
411 fd = open(argv[1], O_RDONLY | O_CLOEXEC);
412 if (fd == \-1)
413 err(EXIT_FAILURE, "open");
414 \&
415 if (setns(fd, 0) == \-1) /* Join that namespace */
416 err(EXIT_FAILURE, "setns");
417 \&
418 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
419 err(EXIT_FAILURE, "execvp");
420 }
421 .EE
422 .\" SRC END
423 .SH SEE ALSO
424 .BR nsenter (1),
425 .BR clone (2),
426 .BR fork (2),
427 .BR unshare (2),
428 .BR vfork (2),
429 .BR namespaces (7),
430 .BR unix (7)