1 .\" Copyright (C) 2006, Janak Desai <janak@us.ibm.com>
2 .\" and Copyright (C) 2006, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
5 .\" Licensed under the GPL
8 .\" Patch Justification:
9 .\" unshare system call is needed to implement, using PAM,
10 .\" per-security_context and/or per-user namespace to provide
11 .\" polyinstantiated directories. Using unshare and bind mounts, a
12 .\" PAM module can create private namespace with appropriate
13 .\" directories(based on user's security context) bind mounted on
14 .\" public directories such as /tmp, thus providing an instance of
15 .\" /tmp that is based on user's security context. Without the
16 .\" unshare system call, namespace separation can only be achieved
17 .\" by clone, which would require porting and maintaining all commands
18 .\" such as login, and su, that establish a user session.
20 .TH UNSHARE 2 2014-09-21 "Linux" "Linux Programmer's Manual"
22 unshare \- disassociate parts of the process execution context
27 .BI "int unshare(int " flags );
31 Feature Test Macro Requirements for glibc (see
32 .BR feature_test_macros (7)):
43 .\" See http://sources.redhat.com/bugzilla/show_bug.cgi?id=4749
45 _BSD_SOURCE || _SVID_SOURCE
46 /* _GNU_SOURCE also suffices */
52 allows a process to disassociate parts of its execution
53 context that are currently being shared with other processes.
54 Part of the execution context, such as the mount namespace, is shared
55 implicitly when a new process is created using
59 while other parts, such as virtual memory, may be
60 shared by explicit request when creating a process using
65 is to allow a process to control its
66 shared execution context without creating a new process.
70 argument is a bit mask that specifies which parts of
71 the execution context should be unshared.
72 This argument is specified by ORing together zero or more
73 of the following constants:
76 Reverse the effect of the
80 Unshare the file descriptor table, so that the calling process
81 no longer shares its file descriptors with any other process.
84 Reverse the effect of the
88 Unshare filesystem attributes, so that the calling process
89 no longer shares its root directory
95 attributes with any other process.
97 .BR CLONE_NEWIPC " (since Linux 2.6.19)
98 This flag has the same effect as the
102 Unshare the System\ V IPC namespace,
103 so that the calling process has a private copy of the
104 System\ V IPC namespace which is not shared with any other process.
105 Specifying this flag automatically implies
114 .BR CLONE_NEWNET " (since Linux 2.6.24)
115 This flag has the same effect as the
119 Unshare the network namespace,
120 so that the calling process is moved into a
121 new network namespace which is not shared
122 with any previously existing process.
130 .\" These flag name are inconsistent:
131 .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
132 .\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
133 .\" flags of the same name.
134 This flag has the same effect as the
138 Unshare the mount namespace,
139 so that the calling process has a private copy of
140 its namespace which is not shared with any other process.
141 Specifying this flag automatically implies
150 .BR CLONE_NEWPID " (since Linux 3.8)"
151 This flag has the same effect as the
155 Unshare the PID namespace,
156 so that the calling process has a new PID namespace for its children
157 which is not shared with any previously existing process.
158 The calling process is
160 moved into the new namespace.
161 The first child created by the calling process will have
162 the process ID 1 and will assume the role of
164 in the new namespace.
166 automatically implies
174 For further information, see
175 .BR pid_namespaces (7).
177 .BR CLONE_NEWUSER " (since Linux 3.8)"
178 This flag has the same effect as the
182 Unshare the user namespace,
183 so that the calling process is moved into a new user namespace
184 which is not shared with any previously existing process.
185 As with the child process created by
189 flag, the caller obtains a full set of capabilities in the new namespace.
192 requires that the calling process is not threaded; specifying
194 automatically implies
197 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
198 .\" https://lwn.net/Articles/543273/
200 also automatically implies
203 requires that the user ID and group ID
204 of the calling process are mapped to user IDs and group IDs in the
205 user namespace of the calling process at the time of the call.
207 For further information on user namespaces, see
208 .BR user_namespaces (7).
210 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
211 This flag has the same effect as the
215 Unshare the UTS IPC namespace,
216 so that the calling process has a private copy of the
217 UTS namespace which is not shared with any other process.
224 .BR CLONE_SYSVSEM " (since Linux 2.6.26)
225 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
226 This flag reverses the effect of the
230 Unshare System\ V semaphore adjustment
233 so that the calling process has a new empty
235 list that is not shared with any other process.
236 If this is the last process that has a reference to the process's current
238 list, then the adjustments in that list are applied
239 to the corresponding semaphores, as described in
241 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
242 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
251 if the caller is single threaded (i.e., it is not sharing
252 its address space with another process or thread).
253 In this case, these flags have no effect.
254 (Note also that specifying
256 automatically implies
260 automatically implies
262 .\" As at 3.9, the following forced implications also apply,
263 .\" although the relevant flags are not yet implemented.
264 .\" If CLONE_THREAD is set force CLONE_VM.
265 .\" If CLONE_VM is set, force CLONE_SIGHAND.
267 If the process is multithreaded, then
268 the use of these flags results in an error.
269 .\" See kernel/fork.c::check_unshare_flags()
273 is specified as zero, then
276 no changes are made to the calling process's execution context.
278 On success, zero returned.
279 On failure, \-1 is returned and
281 is set to indicate the error.
285 An invalid bit was specified in
295 and the caller is multithreaded.
298 Cannot allocate sufficient memory to copy parts of caller's
299 context that need to be unshared.
302 The calling process did not have the required privileges for this operation.
308 but either the effective user ID or the effective group ID of the caller
309 does not have a mapping in the parent namespace (see
310 .BR user_namespaces (7)).
312 .BR EPERM " (since Linux 3.9)"
313 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
314 .B CLONE_NEWUSER was specified in
316 and the caller is in a chroot environment
317 .\" FIXME What is the rationale for this restriction?
318 (i.e., the caller's root directory does not match the root directory
319 of the mount namespace in which it resides).
321 .BR EUSERS " (since Linux 3.11)"
325 and the call would cause the limit on the number of
326 nested user namespaces to be exceeded.
328 .BR user_namespaces (7).
332 system call was added to Linux in kernel 2.6.16.
336 system call is Linux-specific.
338 Not all of the process attributes that can be shared when
339 a new process is created using
341 can be unshared using
343 In particular, as at kernel 3.8,
344 .\" FIXME all of the following needs to be reviewed for the current kernel
346 does not implement flags that reverse the effects of
348 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
349 .\" was not specified when doing clone(); i.e., unsharing
350 .\" signal handlers is permitted if we are not actually
351 .\" sharing signal handlers. mtk
355 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
356 .\" was not specified when doing clone(); i.e., unsharing
357 .\" virtual memory is permitted if we are not actually
358 .\" sharing virtual memory. mtk
359 Such functionality may be added in the future, if required.
363 .\"The current implementation of unshare does not allow unsharing of
364 .\"signals and signal handlers. Signals are complex to begin with and
365 .\"to unshare signals and/or signal handlers of a currently running
366 .\"process is even more complex. If in the future there is a specific
367 .\"need to allow unsharing of signals and/or signal handlers, it can
368 .\"be incrementally added to unshare without affecting legacy
369 .\"applications using unshare.
372 The program below provides a simple implementation of the
374 command, which unshares one or more namespaces and executes the
375 command supplied in its command-line arguments.
376 Here's an example of the use of this program,
377 running a shell in a new mount namespace,
378 and verifying that the original shell and the
379 new shell are in separate mount namespaces:
383 $ \fBreadlink /proc/$$/ns/mnt\fP
385 $ \fBsudo ./unshare -m /bin/bash\fP
386 [sudo] password for cecilia:
387 # \fBreadlink /proc/$$/ns/mnt\fP
392 The differing output of the two
394 commands shows that the two shells are in different mount namespaces.
400 A simple implementation of the unshare(1) command: unshare
401 namespaces and execute a command.
409 /* A simple error\-handling function: print an error message based
410 on the value in \(aqerrno\(aq and terminate the calling process */
412 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
418 fprintf(stderr, "Usage: %s [options] program [arg...]\\n", pname);
419 fprintf(stderr, "Options can be:\\n");
420 fprintf(stderr, " \-i unshare IPC namespace\\n");
421 fprintf(stderr, " \-m unshare mount namespace\\n");
422 fprintf(stderr, " \-n unshare network namespace\\n");
423 fprintf(stderr, " \-p unshare PID namespace\\n");
424 fprintf(stderr, " \-u unshare UTS namespace\\n");
425 fprintf(stderr, " \-U unshare user namespace\\n");
430 main(int argc, char *argv[])
436 while ((opt = getopt(argc, argv, "imnpuU")) != \-1) {
438 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
439 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
440 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
441 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
442 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
443 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
444 default: usage(argv[0]);
451 if (unshare(flags) == \-1)
454 execvp(argv[optind], &argv[optind]);
467 .I Documentation/unshare.txt
468 in the Linux kernel source tree