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 2021-03-22 "Linux" "Linux Programmer's Manual"
22 unshare \- disassociate parts of the process execution context
25 .B #define _GNU_SOURCE
28 .BI "int unshare(int " flags );
32 allows a process (or thread) to disassociate parts of its execution
33 context that are currently being shared with other processes (or threads).
34 Part of the execution context, such as the mount namespace, is shared
35 implicitly when a new process is created using
39 while other parts, such as virtual memory, may be
40 shared by explicit request when creating a process or thread using
45 is to allow a process to control its
46 shared execution context without creating a new process.
50 argument is a bit mask that specifies which parts of
51 the execution context should be unshared.
52 This argument is specified by ORing together zero or more
53 of the following constants:
56 Reverse the effect of the
60 Unshare the file descriptor table, so that the calling process
61 no longer shares its file descriptors with any other process.
64 Reverse the effect of the
68 Unshare filesystem attributes, so that the calling process
69 no longer shares its root directory
75 attributes with any other process.
77 .BR CLONE_NEWCGROUP " (since Linux 4.6)"
78 This flag has the same effect as the
82 Unshare the cgroup namespace.
89 .BR CLONE_NEWIPC " (since Linux 2.6.19)"
90 This flag has the same effect as the
94 Unshare the IPC namespace,
95 so that the calling process has a private copy of the
96 IPC namespace which is not shared with any other process.
97 Specifying this flag automatically implies
106 .BR CLONE_NEWNET " (since Linux 2.6.24)"
107 This flag has the same effect as the
111 Unshare the network namespace,
112 so that the calling process is moved into a
113 new network namespace which is not shared
114 with any previously existing process.
122 .\" These flag name are inconsistent:
123 .\" CLONE_NEWNS does the same thing in clone(), but CLONE_VM,
124 .\" CLONE_FS, and CLONE_FILES reverse the action of the clone()
125 .\" flags of the same name.
126 This flag has the same effect as the
130 Unshare the mount namespace,
131 so that the calling process has a private copy of
132 its namespace which is not shared with any other process.
133 Specifying this flag automatically implies
141 For further information, see
142 .BR mount_namespaces (7).
144 .BR CLONE_NEWPID " (since Linux 3.8)"
145 This flag has the same effect as the
149 Unshare the PID namespace,
150 so that the calling process has a new PID namespace for its children
151 which is not shared with any previously existing process.
152 The calling process is
154 moved into the new namespace.
155 The first child created by the calling process will have
156 the process ID 1 and will assume the role of
158 in the new namespace.
160 automatically implies
168 For further information, see
169 .BR pid_namespaces (7).
171 .BR CLONE_NEWTIME " (since Linux 5.6)"
172 Unshare the time namespace,
173 so that the calling process has a new time namespace for its children
174 which is not shared with any previously existing process.
175 The calling process is
177 moved into the new namespace.
183 For further information, see
184 .BR time_namespaces (7).
186 .BR CLONE_NEWUSER " (since Linux 3.8)"
187 This flag has the same effect as the
191 Unshare the user namespace,
192 so that the calling process is moved into a new user namespace
193 which is not shared with any previously existing process.
194 As with the child process created by
198 flag, the caller obtains a full set of capabilities in the new namespace.
201 requires that the calling process is not threaded; specifying
203 automatically implies
206 .\" commit e66eded8309ebf679d3d3c1f5820d1f2ca332c71
207 .\" https://lwn.net/Articles/543273/
209 also automatically implies
212 requires that the user ID and group ID
213 of the calling process are mapped to user IDs and group IDs in the
214 user namespace of the calling process at the time of the call.
216 For further information on user namespaces, see
217 .BR user_namespaces (7).
219 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
220 This flag has the same effect as the
224 Unshare the UTS IPC namespace,
225 so that the calling process has a private copy of the
226 UTS namespace which is not shared with any other process.
233 .BR CLONE_SYSVSEM " (since Linux 2.6.26)"
234 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
235 This flag reverses the effect of the
239 Unshare System\ V semaphore adjustment
242 so that the calling process has a new empty
244 list that is not shared with any other process.
245 If this is the last process that has a reference to the process's current
247 list, then the adjustments in that list are applied
248 to the corresponding semaphores, as described in
250 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
251 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
260 if the caller is single threaded (i.e., it is not sharing
261 its address space with another process or thread).
262 In this case, these flags have no effect.
263 (Note also that specifying
265 automatically implies
269 automatically implies
271 .\" As at 3.9, the following forced implications also apply,
272 .\" although the relevant flags are not yet implemented.
273 .\" If CLONE_THREAD is set force CLONE_VM.
274 .\" If CLONE_VM is set, force CLONE_SIGHAND.
276 If the process is multithreaded, then
277 the use of these flags results in an error.
278 .\" See kernel/fork.c::check_unshare_flags()
282 is specified as zero, then
285 no changes are made to the calling process's execution context.
287 On success, zero returned.
288 On failure, \-1 is returned and
290 is set to indicate the error.
294 An invalid bit was specified in
304 and the caller is multithreaded.
310 but the kernel was not configured with the
320 but the kernel was not configured with the
328 but the kernel was not configured with the
336 but the kernel was not configured with the
344 but the kernel was not configured with the
352 but the process has previously called
359 Cannot allocate sufficient memory to copy parts of caller's
360 context that need to be unshared.
362 .BR ENOSPC " (since Linux 3.7)"
363 .\" commit f2302505775fd13ba93f034206f1e2a587017929
365 was specified in flags,
366 but the limit on the nesting depth of PID namespaces
367 would have been exceeded; see
368 .BR pid_namespaces (7).
370 .BR ENOSPC " (since Linux 4.9; beforehand " EUSERS )
374 and the call would cause the limit on the number of
375 nested user namespaces to be exceeded.
377 .BR user_namespaces (7).
379 From Linux 3.11 to Linux 4.8, the error diagnosed in this case was
382 .BR ENOSPC " (since Linux 4.9)"
385 specified the creation of a new user namespace,
386 but doing so would have caused the limit defined by the corresponding file in
389 For further details, see
393 The calling process did not have the required privileges for this operation.
399 but either the effective user ID or the effective group ID of the caller
400 does not have a mapping in the parent namespace (see
401 .BR user_namespaces (7)).
403 .BR EPERM " (since Linux 3.9)"
404 .\" commit 3151527ee007b73a0ebd296010f1c0454a919c7d
408 and the caller is in a chroot environment
409 .\" FIXME What is the rationale for this restriction?
410 (i.e., the caller's root directory does not match the root directory
411 of the mount namespace in which it resides).
413 .BR EUSERS " (from Linux 3.11 to Linux 4.8)"
417 and the limit on the number of nested user namespaces would be exceeded.
418 See the discussion of the
424 system call was added to Linux in kernel 2.6.16.
428 system call is Linux-specific.
430 Not all of the process attributes that can be shared when
431 a new process is created using
433 can be unshared using
435 In particular, as at kernel 3.8,
436 .\" FIXME all of the following needs to be reviewed for the current kernel
438 does not implement flags that reverse the effects of
440 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
441 .\" was not specified when doing clone(); i.e., unsharing
442 .\" signal handlers is permitted if we are not actually
443 .\" sharing signal handlers. mtk
447 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
448 .\" was not specified when doing clone(); i.e., unsharing
449 .\" virtual memory is permitted if we are not actually
450 .\" sharing virtual memory. mtk
451 Such functionality may be added in the future, if required.
455 .\"The current implementation of unshare does not allow unsharing of
456 .\"signals and signal handlers. Signals are complex to begin with and
457 .\"to unshare signals and/or signal handlers of a currently running
458 .\"process is even more complex. If in the future there is a specific
459 .\"need to allow unsharing of signals and/or signal handlers, it can
460 .\"be incrementally added to unshare without affecting legacy
461 .\"applications using unshare.
464 The program below provides a simple implementation of the
466 command, which unshares one or more namespaces and executes the
467 command supplied in its command-line arguments.
468 Here's an example of the use of this program,
469 running a shell in a new mount namespace,
470 and verifying that the original shell and the
471 new shell are in separate mount namespaces:
475 $ \fBreadlink /proc/$$/ns/mnt\fP
477 $ \fBsudo ./unshare \-m /bin/bash\fP
478 # \fBreadlink /proc/$$/ns/mnt\fP
483 The differing output of the two
485 commands shows that the two shells are in different mount namespaces.
491 A simple implementation of the unshare(1) command: unshare
492 namespaces and execute a command.
500 /* A simple error\-handling function: print an error message based
501 on the value in \(aqerrno\(aq and terminate the calling process. */
503 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
509 fprintf(stderr, "Usage: %s [options] program [arg...]\en", pname);
510 fprintf(stderr, "Options can be:\en");
511 fprintf(stderr, " \-C unshare cgroup namespace\en");
512 fprintf(stderr, " \-i unshare IPC namespace\en");
513 fprintf(stderr, " \-m unshare mount namespace\en");
514 fprintf(stderr, " \-n unshare network namespace\en");
515 fprintf(stderr, " \-p unshare PID namespace\en");
516 fprintf(stderr, " \-t unshare time namespace\en");
517 fprintf(stderr, " \-u unshare UTS namespace\en");
518 fprintf(stderr, " \-U unshare user namespace\en");
523 main(int argc, char *argv[])
529 while ((opt = getopt(argc, argv, "CimnptuU")) != \-1) {
531 case \(aqC\(aq: flags |= CLONE_NEWCGROUP; break;
532 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
533 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
534 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
535 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
536 case \(aqt\(aq: flags |= CLONE_NEWTIME; break;
537 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
538 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
539 default: usage(argv[0]);
546 if (unshare(flags) == \-1)
549 execvp(argv[optind], &argv[optind]);
562 .I Documentation/userspace\-api/unshare.rst
563 in the Linux kernel source tree
564 .\" commit f504d47be5e8fa7ecf2bf660b18b42e6960c0eb2
566 .I Documentation/unshare.txt