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 2013-04-17 "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.
170 For further information, see
171 .BR pid_namespaces (7).
173 .BR CLONE_NEWUSER " (since Linux 3.8)"
174 This flag has the same effect as the
178 Unshare the user namespace,
179 so that the calling process is moved into a new user namespace
180 which is not shared with any previously existing process.
181 like the child process created by
185 flag, the caller obtains a full set of capabilities in the new namespace.
187 requires that the calling process is not threaded and the user ID and group ID
188 of the calling process are mapped user IDs and group IDs in the
189 user namespace of the calling process at the time of the call.
191 .BR CLONE_NEWUTS " (since Linux 2.6.19)"
192 This flag has the same effect as the
196 Unshare the UTS IPC namespace,
197 so that the calling process has a private copy of the
198 UTS namespace which is not shared with any other process.
205 .BR CLONE_SYSVSEM " (since Linux 2.6.26)
206 .\" commit 9edff4ab1f8d82675277a04e359d0ed8bf14a7b7
207 This flag reverses the effect of the
211 Unshare System\ V semaphore undo values,
212 so that the calling process has a private copy
213 which is not shared with any other process.
219 .\" As at 2.6.16, the following forced implications also apply,
220 .\" although the relevant flags are not yet implemented.
221 .\" If CLONE_THREAD is set force CLONE_VM.
222 .\" If CLONE_VM is set, force CLONE_SIGHAND.
223 .\" CLONE_NEWNS If CLONE_SIGHAND is set and signals are also being shared
224 .\" (i.e., current->signal->count > 1), force CLONE_THREAD.
226 .\" FIXME . CLONE_VM is not (yet, as at 2.6.16) implemented.
229 .\" Reverse the effect of the
234 .\" is also implicitly set by
236 .\" and can be reversed using this
239 .\" Unshare virtual memory, so that the calling process no
240 .\" longer shares its virtual address space with any other process.
249 if the caller is single threaded (i.e., it is not sharing
250 its address space with another process or thread).
251 In this case, these flags have no effect.
252 If the process is multithreaded, then
253 the use of these flags results in an error.
254 .\" See kernel/fork.c::check_unshare_flags()
258 is specified as zero, then
261 no changes are made to the calling process's execution context.
263 On success, zero returned.
264 On failure, \-1 is returned and
266 is set to indicate the error.
270 An invalid bit was specified in
280 and the caller is multithreaded.
283 Cannot allocate sufficient memory to copy parts of caller's
284 context that need to be unshared.
287 The calling process did not have the required privileges for this operation.
293 but either the effective user ID or the effective group ID of the caller
294 does not have a mapping in the parent namespace (see
295 .BR user_namespaces (7)).
299 system call was added to Linux in kernel 2.6.16.
303 system call is Linux-specific.
305 Not all of the process attributes that can be shared when
306 a new process is created using
308 can be unshared using
310 In particular, as at kernel 3.8,
311 .\" FIXME all of the following needs to be reviewed for the current kernel
313 does not implement flags that reverse the effects of
315 .\" However, we can do unshare(CLONE_SIGHAND) if CLONE_SIGHAND
316 .\" was not specified when doing clone(); i.e., unsharing
317 .\" signal handlers is permitted if we are not actually
318 .\" sharing signal handlers. mtk
322 .\" However, we can do unshare(CLONE_VM) if CLONE_VM
323 .\" was not specified when doing clone(); i.e., unsharing
324 .\" virtual memory is permitted if we are not actually
325 .\" sharing virtual memory. mtk
326 Such functionality may be added in the future, if required.
330 .\"The current implementation of unshare does not allow unsharing of
331 .\"signals and signal handlers. Signals are complex to begin with and
332 .\"to unshare signals and/or signal handlers of a currently running
333 .\"process is even more complex. If in the future there is a specific
334 .\"need to allow unsharing of signals and/or signal handlers, it can
335 .\"be incrementally added to unshare without affecting legacy
336 .\"applications using unshare.
339 The program below provides a simple implementation of the
341 command, which unshares one or more namespaces and executes the
342 command supplied in its command line arguments.
343 Here's an example of the use of this program,
344 running a shell in a new mount namespace,
345 and verifying that the original shell and the
346 new shell are in separate mount namespaces:
350 $ \fBreadlink /proc/$$/ns/mnt\fP
352 $ \fBsudo ./unshare -m /bin/bash\fP
353 [sudo] password for cecilia:
354 # \fBreadlink /proc/$$/ns/mnt\fP
359 The differing output of the two
361 commands shows that the two shells are in different mount namespaces.
367 A simple implementation of the unshare(1) command: unshare
368 namespaces and execute a command.
376 /* A simple error\-handling function: print an error message based
377 on the value in \(aqerrno\(aq and terminate the calling process */
379 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
385 fprintf(stderr, "Usage: %s [options] program [arg...]\\n", pname);
386 fprintf(stderr, "Options can be:\\n");
387 fprintf(stderr, " \-i unshare IPC namespace\\n");
388 fprintf(stderr, " \-m unshare mount namespace\\n");
389 fprintf(stderr, " \-n unshare network namespace\\n");
390 fprintf(stderr, " \-p unshare PID namespace\\n");
391 fprintf(stderr, " \-u unshare UTS namespace\\n");
392 fprintf(stderr, " \-U unshare user namespace\\n");
397 main(int argc, char *argv[])
403 while ((opt = getopt(argc, argv, "imnpuU")) != \-1) {
405 case \(aqi\(aq: flags |= CLONE_NEWIPC; break;
406 case \(aqm\(aq: flags |= CLONE_NEWNS; break;
407 case \(aqn\(aq: flags |= CLONE_NEWNET; break;
408 case \(aqp\(aq: flags |= CLONE_NEWPID; break;
409 case \(aqu\(aq: flags |= CLONE_NEWUTS; break;
410 case \(aqU\(aq: flags |= CLONE_NEWUSER; break;
411 default: usage(argv[0]);
418 if (unshare(flags) == \-1)
421 execvp(argv[optind], &argv[optind]);
434 .I Documentation/unshare.txt
435 in the Linux kernel source tree