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 2017-09-15 "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 );
19 Given a file descriptor referring to a namespace,
20 reassociate the calling thread with that namespace.
24 argument is a file descriptor referring to one of the namespace entries in a
28 for further information on
30 The calling thread will be reassociated with the corresponding namespace,
31 subject to any constraints imposed by the
37 argument specifies which type of namespace
38 the calling thread may be reassociated with.
39 This argument can have one of the following values:
42 Allow any type of namespace to be joined.
44 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
46 must refer to a cgroup namespace.
48 .BR CLONE_NEWIPC " (since Linux 3.0)"
50 must refer to an IPC namespace.
52 .BR CLONE_NEWNET " (since Linux 3.0)"
54 must refer to a network namespace.
56 .BR CLONE_NEWNS " (since Linux 3.8)"
58 must refer to a mount namespace.
60 .BR CLONE_NEWPID " (since Linux 3.8)"
62 must refer to a descendant PID namespace.
64 .BR CLONE_NEWUSER " (since Linux 3.8)"
66 must refer to a user namespace.
68 .BR CLONE_NEWUTS " (since Linux 3.0)"
70 must refer to a UTS namespace.
74 as 0 suffices if the caller knows (or does not care)
75 what type of namespace is referred to by
77 Specifying a nonzero value for
79 is useful if the caller does not know what type of namespace is referred to by
81 and wants to ensure that the namespace is of a particular type.
82 (The caller might not know the type of the namespace referred to by
84 if the file descriptor was opened by another process and, for example,
85 passed to the caller via a UNIX domain socket.)
89 refers to a PID namespaces, the semantics are somewhat different
90 from other namespace types:
91 reassociating the calling thread with a PID namespace changes only
92 the PID namespace that subsequently created child processes of
93 the caller will be placed in;
94 it does not change the PID namespace of the caller itself.
95 Reassociating with a PID namespace is allowed only if the
96 PID namespace specified by
98 is a descendant (child, grandchild, etc.)
99 of the PID namespace of the caller.
100 For further details on PID namespaces, see
101 .BR pid_namespaces (7).
103 A process reassociating itself with a user namespace must have the
105 .\" See kernel/user_namespace.c:userns_install() [3.8 source]
106 capability in the target user namespace.
107 Upon successfully joining a user namespace,
108 a process is granted all capabilities in that namespace,
109 regardless of its user and group IDs.
110 A multithreaded process may not change user namespace with
112 It is not permitted to use
114 to reenter the caller's current user namespace.
115 This prevents a caller that has dropped capabilities from regaining
116 those capabilities via a call to
118 For security reasons,
119 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
120 .\" https://lwn.net/Articles/543273/
121 a process can't join a new user namespace if it is sharing
122 filesystem-related attributes
123 (the attributes whose sharing is controlled by the
126 flag) with another process.
127 For further details on user namespaces, see
128 .BR user_namespaces (7).
130 A process may not be reassociated with a new mount namespace if it is
132 .\" Above check is in fs/namespace.c:mntns_install() [3.8 source]
133 Changing the mount namespace requires that the caller possess both
137 capabilities in its own user namespace and
139 in the target mount namespace.
141 .BR user_namespaces (7)
142 for details on the interaction of user namespaces and mount namespaces.
146 to change the caller's cgroup namespace does not change
147 the caller's cgroup memberships.
152 On failure, \-1 is returned and
154 is set to indicate the error.
159 is not a valid file descriptor.
163 refers to a namespace whose type does not match that specified in
167 There is problem with reassociating
168 the thread with the specified namespace.
170 .\" See kernel/pid_namespace.c::pidns_install() [kernel 3.18 sources]
172 The caller tried to join an ancestor (parent, grandparent, and so on)
176 The caller attempted to join the user namespace
177 in which it is already a member.
180 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
181 The caller shares filesystem
183 state (in particular, the root directory)
184 with other processes and tried to join a new user namespace.
187 .\" See kernel/user_namespace.c::userns_install() [kernel 3.15 sources]
188 The caller is multithreaded and tried to join a new user namespace.
191 Cannot allocate sufficient memory to change the specified namespace.
194 The calling thread did not have the required capability
199 system call first appeared in Linux in kernel 3.0;
200 library support was added to glibc in version 2.14.
204 system call is Linux-specific.
206 Not all of the attributes that can be shared when
207 a new thread is created using
212 The program below takes two or more arguments.
213 The first argument specifies the pathname of a namespace file in an existing
216 The remaining arguments specify a command and its arguments.
217 The program opens the namespace file, joins that namespace using
219 and executes the specified command inside that namespace.
221 The following shell session demonstrates the use of this program
222 (compiled as a binary named
224 in conjunction with the
226 example program in the
228 man page (complied as a binary named
231 We begin by executing the example program in
234 That program creates a child in a separate UTS namespace.
235 The child changes the hostname in its namespace,
236 and then both processes display the hostnames in their UTS namespaces,
237 so that we can see that they are different.
241 $ \fBsu\fP # Need privilege for namespace operations
243 # \fB./newuts bizarro &\fP
245 clone() returned 3550
246 uts.nodename in child: bizarro
247 uts.nodename in parent: antero
248 # \fBuname \-n\fP # Verify hostname in the shell
253 We then run the program shown below,
254 using it to execute a shell.
255 Inside that shell, we verify that the hostname is the one
256 set by the child created by the first program:
260 # \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
261 # \fBuname \-n\fP # Executed in shell started by ns_exec
274 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
278 main(int argc, char *argv[])
283 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
287 fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
291 if (setns(fd, 0) == \-1) /* Join that namespace */
294 execvp(argv[2], &argv[2]); /* Execute a command in namespace */