1SETNS(2) Linux Programmer's Manual SETNS(2)
2
3
4
6 setns - reassociate thread with a namespace
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <sched.h>
11
12 int setns(int fd, int nstype);
13
15 Given a file descriptor referring to a namespace, reassociate the call‐
16 ing thread with that namespace.
17
18 The fd argument is a file descriptor referring to one of the namespace
19 entries in a /proc/[pid]/ns/ directory; see namespaces(7) for further
20 information on /proc/[pid]/ns/. The calling thread will be reassoci‐
21 ated with the corresponding namespace, subject to any constraints
22 imposed by the nstype argument.
23
24 The nstype argument specifies which type of namespace the calling
25 thread may be reassociated with. This argument can have one of the
26 following values:
27
28 0 Allow any type of namespace to be joined.
29
30 CLONE_NEWCGROUP (since Linux 4.6)
31 fd must refer to a cgroup namespace.
32
33 CLONE_NEWIPC (since Linux 3.0)
34 fd must refer to an IPC namespace.
35
36 CLONE_NEWNET (since Linux 3.0)
37 fd must refer to a network namespace.
38
39 CLONE_NEWNS (since Linux 3.8)
40 fd must refer to a mount namespace.
41
42 CLONE_NEWPID (since Linux 3.8)
43 fd must refer to a descendant PID namespace.
44
45 CLONE_NEWTIME (since Linux 5.6)
46 fd must refer to a time namespace.
47
48 CLONE_NEWUSER (since Linux 3.8)
49 fd must refer to a user namespace.
50
51 CLONE_NEWUTS (since Linux 3.0)
52 fd must refer to a UTS namespace.
53
54 Specifying nstype as 0 suffices if the caller knows (or does not care)
55 what type of namespace is referred to by fd. Specifying a nonzero
56 value for nstype is useful if the caller does not know what type of
57 namespace is referred to by fd and wants to ensure that the namespace
58 is of a particular type. (The caller might not know the type of the
59 namespace referred to by fd if the file descriptor was opened by
60 another process and, for example, passed to the caller via a UNIX
61 domain socket.)
62
63 Details for specific namespace types
64 Note the following details and restrictions when reassociating with
65 specific namespace types:
66
67 User namespaces
68 A process reassociating itself with a user namespace must have
69 the CAP_SYS_ADMIN capability in the target user namespace.
70 (This necessarily implies that it is only possible to join a
71 descendant user namespace.) Upon successfully joining a user
72 namespace, a process is granted all capabilities in that names‐
73 pace, regardless of its user and group IDs.
74
75 A multithreaded process may not change user namespace with
76 setns().
77
78 It is not permitted to use setns() to reenter the caller's cur‐
79 rent user namespace. This prevents a caller that has dropped
80 capabilities from regaining those capabilities via a call to
81 setns().
82
83 For security reasons, a process can't join a new user namespace
84 if it is sharing filesystem-related attributes (the attributes
85 whose sharing is controlled by the clone(2) CLONE_FS flag) with
86 another process.
87
88 For further details on user namespaces, see user_namespaces(7).
89
90 Mount namespaces
91 Changing the mount namespace requires that the caller possess
92 both CAP_SYS_CHROOT and CAP_SYS_ADMIN capabilities in its own
93 user namespace and CAP_SYS_ADMIN in the user namespace that owns
94 the target mount namespace.
95
96 A process can't join a new mount namespace if it is sharing
97 filesystem-related attributes (the attributes whose sharing is
98 controlled by the clone(2) CLONE_FS flag) with another process.
99
100 See user_namespaces(7) for details on the interaction of user
101 namespaces and mount namespaces.
102
103 PID namespaces
104 In order to reassociate itself with a new PID namespace, the
105 caller must have the CAP_SYS_ADMIN capability both in its own
106 user namespace and in the user namespace that owns the target
107 PID namespace.
108
109 If fd refers to a PID namespace, the semantics are somewhat dif‐
110 ferent from other namespace types: reassociating the calling
111 thread with a PID namespace changes only the PID namespace that
112 subsequently created child processes of the caller will be
113 placed in; it does not change the PID namespace of the caller
114 itself.
115
116 Reassociating with a PID namespace is allowed only if the PID
117 namespace specified by fd is a descendant (child, grandchild,
118 etc.) of the PID namespace of the caller.
119
120 For further details on PID namespaces, see pid_namespaces(7).
121
122 Cgroup namespaces
123 In order to reassociate itself with a new cgroup namespace, the
124 caller must have the CAP_SYS_ADMIN capability both in its own
125 user namespace and in the user namespace that owns the target
126 cgroup namespace.
127
128 Using setns() to change the caller's cgroup namespace does not
129 change the caller's cgroup memberships.
130
131 Network, IPC, time, and UTS namespaces
132 In order to reassociate itself with a new network, IPC, time, or
133 UTS namespace, the caller must have the CAP_SYS_ADMIN capability
134 both in its own user namespace and in the user namespace that
135 owns the target namespace.
136
138 On success, setns() returns 0. On failure, -1 is returned and errno is
139 set to indicate the error.
140
142 EBADF fd is not a valid file descriptor.
143
144 EINVAL fd refers to a namespace whose type does not match that speci‐
145 fied in nstype.
146
147 EINVAL There is problem with reassociating the thread with the speci‐
148 fied namespace.
149
150 EINVAL The caller tried to join an ancestor (parent, grandparent, and
151 so on) PID namespace.
152
153 EINVAL The caller attempted to join the user namespace in which it is
154 already a member.
155
156 EINVAL The caller shares filesystem (CLONE_FS) state (in particular,
157 the root directory) with other processes and tried to join a new
158 user namespace.
159
160 EINVAL The caller is multithreaded and tried to join a new user names‐
161 pace.
162
163 ENOMEM Cannot allocate sufficient memory to change the specified names‐
164 pace.
165
166 EPERM The calling thread did not have the required capability for this
167 operation.
168
170 The setns() system call first appeared in Linux in kernel 3.0; library
171 support was added to glibc in version 2.14.
172
174 The setns() system call is Linux-specific.
175
177 Not all of the attributes that can be shared when a new thread is cre‐
178 ated using clone(2) can be changed using setns().
179
181 The program below takes two or more arguments. The first argument
182 specifies the pathname of a namespace file in an existing
183 /proc/[pid]/ns/ directory. The remaining arguments specify a command
184 and its arguments. The program opens the namespace file, joins that
185 namespace using setns(), and executes the specified command inside that
186 namespace.
187
188 The following shell session demonstrates the use of this program (com‐
189 piled as a binary named ns_exec) in conjunction with the CLONE_NEWUTS
190 example program in the clone(2) man page (complied as a binary named
191 newuts).
192
193 We begin by executing the example program in clone(2) in the back‐
194 ground. That program creates a child in a separate UTS namespace. The
195 child changes the hostname in its namespace, and then both processes
196 display the hostnames in their UTS namespaces, so that we can see that
197 they are different.
198
199 $ su # Need privilege for namespace operations
200 Password:
201 # ./newuts bizarro &
202 [1] 3549
203 clone() returned 3550
204 uts.nodename in child: bizarro
205 uts.nodename in parent: antero
206 # uname -n # Verify hostname in the shell
207 antero
208
209 We then run the program shown below, using it to execute a shell.
210 Inside that shell, we verify that the hostname is the one set by the
211 child created by the first program:
212
213 # ./ns_exec /proc/3550/ns/uts /bin/bash
214 # uname -n # Executed in shell started by ns_exec
215 bizarro
216
217 Program source
218 #define _GNU_SOURCE
219 #include <fcntl.h>
220 #include <sched.h>
221 #include <unistd.h>
222 #include <stdlib.h>
223 #include <stdio.h>
224
225 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
226 } while (0)
227
228 int
229 main(int argc, char *argv[])
230 {
231 int fd;
232
233 if (argc < 3) {
234 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]);
235 exit(EXIT_FAILURE);
236 }
237
238 fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
239 if (fd == -1)
240 errExit("open");
241
242 if (setns(fd, 0) == -1) /* Join that namespace */
243 errExit("setns");
244
245 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
246 errExit("execvp");
247 }
248
250 nsenter(1), clone(2), fork(2), unshare(2), vfork(2), namespaces(7),
251 unix(7)
252
254 This page is part of release 5.07 of the Linux man-pages project. A
255 description of the project, information about reporting bugs, and the
256 latest version of this page, can be found at
257 https://www.kernel.org/doc/man-pages/.
258
259
260
261Linux 2020-04-11 SETNS(2)