1 .\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
2 .\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" Licensed under the GPLv2
8 .TH SETNS 2 2020-08-13 "Linux" "Linux Programmer's Manual"
10 setns \- reassociate thread with a namespace
13 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .BI "int setns(int " fd ", int " nstype );
21 system call allows the calling thread to move into different namespaces.
24 argument is one of the following:
26 a file descriptor referring to one of the magic links in a
28 directory (or a bind mount to such a link);
30 a PID file descriptor (see
35 argument is interpreted differently in each case.
37 .SS fd refers to a /proc/[pid]/ns/ link
44 reassociates the calling thread with the namespace associated with that link,
45 subject to any constraints imposed by the
48 In this usage, each call to
50 changes just one of the caller's namespace memberships.
54 argument specifies which type of namespace
55 the calling thread may be reassociated with.
56 This argument can have
58 of the following values:
61 Allow any type of namespace to be joined.
63 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
65 must refer to a cgroup namespace.
67 .BR CLONE_NEWIPC " (since Linux 3.0)"
69 must refer to an IPC namespace.
71 .BR CLONE_NEWNET " (since Linux 3.0)"
73 must refer to a network namespace.
75 .BR CLONE_NEWNS " (since Linux 3.8)"
77 must refer to a mount namespace.
79 .BR CLONE_NEWPID " (since Linux 3.8)"
81 must refer to a descendant PID namespace.
83 .BR CLONE_NEWTIME " (since Linux 5.8)"
84 .\" commit 76c12881a38aaa83e1eb4ce2fada36c3a732bad4
86 must refer to a time namespace.
88 .BR CLONE_NEWUSER " (since Linux 3.8)"
90 must refer to a user namespace.
92 .BR CLONE_NEWUTS " (since Linux 3.0)"
94 must refer to a UTS namespace.
98 as 0 suffices if the caller knows (or does not care)
99 what type of namespace is referred to by
101 Specifying a nonzero value for
103 is useful if the caller does not know what type of namespace is referred to by
105 and wants to ensure that the namespace is of a particular type.
106 (The caller might not know the type of the namespace referred to by
108 if the file descriptor was opened by another process and, for example,
109 passed to the caller via a UNIX domain socket.)
111 .SS fd is a PID file descriptor
114 may refer to a PID file descriptor obtained from
120 atomically moves the calling thread into one or more of the same namespaces
121 as the thread referred to by
126 argument is a bit mask specified by ORing together
130 namespace constants listed above.
131 The caller is moved into each of the target thread's namespaces
134 the caller's memberships in the remaining namespaces are left unchanged.
136 For example, the following code would move the caller into the
137 same user, network, and UTS namespaces as PID 1234,
138 but would leave the caller's other namespace memberships unchanged:
142 int fd = pidfd_open(1234, 0);
143 setns(fd, CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS);
147 .SS Details for specific namespace types
148 Note the following details and restrictions when reassociating with
149 specific namespace types:
152 A process reassociating itself with a user namespace must have the
154 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
155 capability in the target user namespace.
156 (This necessarily implies that it is only possible to join
157 a descendant user namespace.)
158 Upon successfully joining a user namespace,
159 a process is granted all capabilities in that namespace,
160 regardless of its user and group IDs.
162 A multithreaded process may not change user namespace with
165 It is not permitted to use
167 to reenter the caller's current user namespace.
168 This prevents a caller that has dropped capabilities from regaining
169 those capabilities via a call to
172 For security reasons,
173 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
174 .\" https://lwn.net/Articles/543273/
175 a process can't join a new user namespace if it is sharing
176 filesystem-related attributes
177 (the attributes whose sharing is controlled by the
180 flag) with another process.
182 For further details on user namespaces, see
183 .BR user_namespaces (7).
186 Changing the mount namespace requires that the caller possess both
190 capabilities in its own user namespace and
192 in the user namespace that owns the target mount namespace.
194 A process can't join a new mount namespace if it is sharing
195 filesystem-related attributes
196 (the attributes whose sharing is controlled by the
199 flag) with another process.
200 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
203 .BR user_namespaces (7)
204 for details on the interaction of user namespaces and mount namespaces.
207 In order to reassociate itself with a new PID namespace,
208 the caller must have the
210 capability both in its own user namespace and in the user namespace
211 that owns the target PID namespace.
213 Reassociating the PID namespace has somewhat different
214 from other namespace types.
215 Reassociating the calling thread with a PID namespace changes only
216 the PID namespace that subsequently created child processes of
217 the caller will be placed in;
218 it does not change the PID namespace of the caller itself.
220 Reassociating with a PID namespace is allowed only if the target
221 PID namespace is a descendant (child, grandchild, etc.)
222 of, or is the same as, the current PID namespace of the caller.
224 For further details on PID namespaces, see
225 .BR pid_namespaces (7).
228 In order to reassociate itself with a new cgroup namespace,
229 the caller must have the
231 capability both in its own user namespace and in the user namespace
232 that owns the target cgroup namespace.
236 to change the caller's cgroup namespace does not change
237 the caller's cgroup memberships.
239 Network, IPC, time, and UTS namespaces
240 In order to reassociate itself with a new network, IPC, time, or UTS namespace,
241 the caller must have the
243 capability both in its own user namespace and in the user namespace
244 that owns the target namespace.
249 On failure, \-1 is returned and
251 is set to indicate the error.
256 is not a valid file descriptor.
260 refers to a namespace whose type does not match that specified in
264 There is problem with reassociating
265 the thread with the specified namespace.
267 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
269 The caller tried to join an ancestor (parent, grandparent, and so on)
273 The caller attempted to join the user namespace
274 in which it is already a member.
277 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
278 The caller shares filesystem
280 state (in particular, the root directory)
281 with other processes and tried to join a new user namespace.
284 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
285 The caller is multithreaded and tried to join a new user namespace.
289 is a PID file descriptor and
291 is invalid (e.g., it is 0).
294 Cannot allocate sufficient memory to change the specified namespace.
297 The calling thread did not have the required capability
302 is a PID file descriptor but the process it refers to no longer exists
303 (i.e., it has terminated and been waited on).
307 system call first appeared in Linux in kernel 3.0;
308 library support was added to glibc in version 2.14.
312 system call is Linux-specific.
314 For further information on the
319 Not all of the attributes that can be shared when
320 a new thread is created using
325 The program below takes two or more arguments.
326 The first argument specifies the pathname of a namespace file in an existing
329 The remaining arguments specify a command and its arguments.
330 The program opens the namespace file, joins that namespace using
332 and executes the specified command inside that namespace.
334 The following shell session demonstrates the use of this program
335 (compiled as a binary named
337 in conjunction with the
339 example program in the
341 man page (complied as a binary named
344 We begin by executing the example program in
347 That program creates a child in a separate UTS namespace.
348 The child changes the hostname in its namespace,
349 and then both processes display the hostnames in their UTS namespaces,
350 so that we can see that they are different.
354 $ \fBsu\fP # Need privilege for namespace operations
356 # \fB./newuts bizarro &\fP
358 clone() returned 3550
359 uts.nodename in child: bizarro
360 uts.nodename in parent: antero
361 # \fBuname \-n\fP # Verify hostname in the shell
366 We then run the program shown below,
367 using it to execute a shell.
368 Inside that shell, we verify that the hostname is the one
369 set by the child created by the first program:
373 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
374 # \fBuname \-n\fP # Executed in shell started by ns_exec
387 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
391 main(int argc, char *argv[])
396 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\en", argv[0]);
400 /* Get file descriptor for namespace; the file descriptor is opened
401 with O_CLOEXEC so as to ensure that it is not inherited by the
402 program that is later executed. */
404 fd = open(argv[1], O_RDONLY | O_CLOEXEC);
408 if (setns(fd, 0) == \-1) /* Join that namespace */
411 execvp(argv[2], &argv[2]); /* Execute a command in namespace */