1 .\" Copyright (C) 2006, Janak Desai <janak@us.ibm.com>
2 .\" and Copyright (C) 2006, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: GPL-1.0-or-later
6 .\" Patch Justification:
7 .\" unshare system call is needed to implement, using PAM,
8 .\" per-security_context and/or per-user namespace to provide
9 .\" polyinstantiated directories. Using unshare and bind mounts, a
10 .\" PAM module can create private namespace with appropriate
11 .\" directories(based on user's security context) bind mounted on
12 .\" public directories such as /tmp, thus providing an instance of
13 .\" /tmp that is based on user's security context. Without the
14 .\" unshare system call, namespace separation can only be achieved
15 .\" by clone, which would require porting and maintaining all commands
16 .\" such as login, and su, that establish a user session.
18 .TH UNSHARE 2 2022-09-13 "Linux man-pages (unreleased)"
20 unshare \- disassociate parts of the process execution context
23 .RI ( libc ", " \-lc )
26 .B #define _GNU_SOURCE
29 .BI "int unshare(int " flags );
33 allows a process (or thread) to disassociate parts of its execution
34 context that are currently being shared with other processes (or threads).
35 Part of the execution context, such as the mount namespace, is shared
36 implicitly when a new process is created using
40 while other parts, such as virtual memory, may be
41 shared by explicit request when creating a process or thread using
46 is to allow a process to control its
47 shared execution context without creating a new process.
51 argument is a bit mask that specifies which parts of
52 the execution context should be unshared.
53 This argument is specified by ORing together zero or more
54 of the following constants:
57 Reverse the effect of the
61 Unshare the file descriptor table, so that the calling process
62 no longer shares its file descriptors with any other process.
65 Reverse the effect of the
69 Unshare filesystem attributes, so that the calling process
70 no longer shares its root directory
76 attributes with any other process.
78 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
79 This flag has the same effect as the
83 Unshare the cgroup namespace.
90 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
91 This flag has the same effect as the
95 Unshare the IPC namespace,
96 so that the calling process has a private copy of the
97 IPC namespace which is not shared with any other process.
98 Specifying this flag automatically implies
107 .BR CLONE_NEWNET " (since Linux 2.6.24)"
108 This flag has the same effect as the
112 Unshare the network namespace,
113 so that the calling process is moved into a
114 new network namespace which is not shared
115 with any previously existing process.
123 .\" These flag name are inconsistent:
124 .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
125 .\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
126 .\" flags of the same name.
127 This flag has the same effect as the
131 Unshare the mount namespace,
132 so that the calling process has a private copy of
133 its namespace which is not shared with any other process.
134 Specifying this flag automatically implies
142 For further information, see
143 .BR mount_namespaces (7).
145 .BR CLONE_NEWPID " (since Linux 3.8)"
146 This flag has the same effect as the
150 Unshare the PID namespace,
151 so that the calling process has a new PID namespace for its children
152 which is not shared with any previously existing process.
153 The calling process is
155 moved into the new namespace.
156 The first child created by the calling process will have
157 the process ID 1 and will assume the role of
159 in the new namespace.
161 automatically implies
169 For further information, see
170 .BR pid_namespaces (7).
172 .BR CLONE_NEWTIME " (since Linux 5.6)"
173 Unshare the time namespace,
174 so that the calling process has a new time namespace for its children
175 which is not shared with any previously existing process.
176 The calling process is
178 moved into the new namespace.
184 For further information, see
185 .BR time_namespaces (7).
187 .BR CLONE_NEWUSER " (since Linux 3.8)"
188 This flag has the same effect as the
192 Unshare the user namespace,
193 so that the calling process is moved into a new user namespace
194 which is not shared with any previously existing process.
195 As with the child process created by
199 flag, the caller obtains a full set of capabilities in the new namespace.
202 requires that the calling process is not threaded; specifying
204 automatically implies
207 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
208 .\" https://lwn.net/Articles/543273/
210 also automatically implies
213 requires that the user ID and group ID
214 of the calling process are mapped to user IDs and group IDs in the
215 user namespace of the calling process at the time of the call.
217 For further information on user namespaces, see
218 .BR user_namespaces (7).
220 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
221 This flag has the same effect as the
225 Unshare the UTS IPC namespace,
226 so that the calling process has a private copy of the
227 UTS namespace which is not shared with any other process.
234 .BR CLONE_SYSVSEM " (since Linux 2.6.26)"
235 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
236 This flag reverses the effect of the
240 Unshare System\ V semaphore adjustment
243 so that the calling process has a new empty
245 list that is not shared with any other process.
246 If this is the last process that has a reference to the process's current
248 list, then the adjustments in that list are applied
249 to the corresponding semaphores, as described in
251 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
252 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
261 if the caller is single threaded (i.e., it is not sharing
262 its address space with another process or thread).
263 In this case, these flags have no effect.
264 (Note also that specifying
266 automatically implies
270 automatically implies
272 .\" As at 3.9, the following forced implications also apply,
273 .\" although the relevant flags are not yet implemented.
274 .\" If CLONE_THREAD is set force CLONE_VM.
275 .\" If CLONE_VM is set, force CLONE_SIGHAND.
277 If the process is multithreaded, then
278 the use of these flags results in an error.
279 .\" See kernel/fork.c::check_unshare_flags()
283 is specified as zero, then
286 no changes are made to the calling process's execution context.
288 On success, zero returned.
289 On failure, \-1 is returned and
291 is set to indicate the error.
295 An invalid bit was specified in
305 and the caller is multithreaded.
311 but the kernel was not configured with the
321 but the kernel was not configured with the
329 but the kernel was not configured with the
337 but the kernel was not configured with the
345 but the kernel was not configured with the
353 but the process has previously called
360 Cannot allocate sufficient memory to copy parts of caller's
361 context that need to be unshared.
363 .BR ENOSPC " (since Linux 3.7)"
364 .\" commit f2302505775fd13ba93f034206f1e2a587017929
366 was specified in flags,
367 but the limit on the nesting depth of PID namespaces
368 would have been exceeded; see
369 .BR pid_namespaces (7).
371 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
375 and the call would cause the limit on the number of
376 nested user namespaces to be exceeded.
378 .BR user_namespaces (7).
380 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
383 .BR ENOSPC " (since Linux 4.9)"
386 specified the creation of a new user namespace,
387 but doing so would have caused the limit defined by the corresponding file in
390 For further details, see
394 The calling process did not have the required privileges for this operation.
400 but either the effective user ID or the effective group ID of the caller
401 does not have a mapping in the parent namespace (see
402 .BR user_namespaces (7)).
404 .BR EPERM " (since Linux 3.9)"
405 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
409 and the caller is in a chroot environment
410 .\" FIXME What is the rationale for this restriction?
411 (i.e., the caller's root directory does not match the root directory
412 of the mount namespace in which it resides).
414 .BR EUSERS " (from Linux 3.11 to Linux 4.8)"
418 and the limit on the number of nested user namespaces would be exceeded.
419 See the discussion of the
425 system call was added to Linux in kernel 2.6.16.
429 system call is Linux-specific.
431 Not all of the process attributes that can be shared when
432 a new process is created using
434 can be unshared using
436 In particular, as at kernel 3.8,
437 .\" FIXME all of the following needs to be reviewed for the current kernel
439 does not implement flags that reverse the effects of
441 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
442 .\" was not specified when doing clone(); i.e., unsharing
443 .\" signal handlers is permitted if we are not actually
444 .\" sharing signal handlers. mtk
448 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
449 .\" was not specified when doing clone(); i.e., unsharing
450 .\" virtual memory is permitted if we are not actually
451 .\" sharing virtual memory. mtk
452 Such functionality may be added in the future, if required.
456 .\"The current implementation of unshare does not allow unsharing of
457 .\"signals and signal handlers. Signals are complex to begin with and
458 .\"to unshare signals and/or signal handlers of a currently running
459 .\"process is even more complex. If in the future there is a specific
460 .\"need to allow unsharing of signals and/or signal handlers, it can
461 .\"be incrementally added to unshare without affecting legacy
462 .\"applications using unshare.
465 Creating all kinds of namespace, except user namespaces, requires the
468 However, since creating a user namespace automatically confers a full set of
470 creating both a user namespace and any other type of namespace in the same
472 call does not require the
474 capability in the original namespace.
476 The program below provides a simple implementation of the
478 command, which unshares one or more namespaces and executes the
479 command supplied in its command-line arguments.
480 Here's an example of the use of this program,
481 running a shell in a new mount namespace,
482 and verifying that the original shell and the
483 new shell are in separate mount namespaces:
487 $ \fBreadlink /proc/$$/ns/mnt\fP
489 $ \fBsudo ./unshare \-m /bin/bash\fP
490 # \fBreadlink /proc/$$/ns/mnt\fP
495 The differing output of the two
497 commands shows that the two shells are in different mount namespaces.
500 .\" SRC BEGIN (unshare.c)
504 A simple implementation of the unshare(1) command: unshare
505 namespaces and execute a command.
517 fprintf(stderr, "Usage: %s [options] program [arg...]\en", pname);
518 fprintf(stderr, "Options can be:\en");
519 fprintf(stderr, " \-C unshare cgroup namespace\en");
520 fprintf(stderr, " \-i unshare IPC namespace\en");
521 fprintf(stderr, " \-m unshare mount namespace\en");
522 fprintf(stderr, " \-n unshare network namespace\en");
523 fprintf(stderr, " \-p unshare PID namespace\en");
524 fprintf(stderr, " \-t unshare time namespace\en");
525 fprintf(stderr, " \-u unshare UTS namespace\en");
526 fprintf(stderr, " \-U unshare user namespace\en");
531 main(int argc, char *argv[])
537 while ((opt = getopt(argc, argv, "CimnptuU")) != \-1) {
539 case \(aqC\(aq: flags |= CLONE_NEWCGROUP; break;
540 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
541 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
542 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
543 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
544 case \(aqt\(aq: flags |= CLONE_NEWTIME; break;
545 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
546 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
547 default: usage(argv[0]);
554 if (unshare(flags) == \-1)
555 err(EXIT_FAILURE, "unshare");
557 execvp(argv[optind], &argv[optind]);
558 err(EXIT_FAILURE, "execvp");
571 .I Documentation/userspace\-api/unshare.rst
572 in the Linux kernel source tree
573 .\" commit f504d47be5e8fa7ecf2bf660b18b42e6960c0eb2
575 .I Documentation/unshare.txt