]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/unshare.1
unshare: Add --kill-child option.
[thirdparty/util-linux.git] / sys-utils / unshare.1
CommitLineData
de0f3763 1.TH UNSHARE 1 "February 2016" "util-linux" "User Commands"
4205f1fd 2.SH NAME
ef6acdb8 3unshare \- run program with some namespaces unshared from parent
4205f1fd
MG
4.SH SYNOPSIS
5.B unshare
cf8e0bae 6[options]
b5672517
KZ
7.RI [ program
8.RI [ arguments ]]
4205f1fd 9.SH DESCRIPTION
dde08a87 10Unshares the indicated namespaces from the parent process and then executes
b5672517
KZ
11the specified \fIprogram\fR. If \fIprogram\fR is not given, then ``${SHELL}'' is
12run (default: /bin/sh).
0490a6ca 13.PP
de0f3763
BS
14The namespaces can optionally be made persistent by bind mounting
15/proc/\fIpid\fR/ns/\fItype\fR files to a filesystem path and entered with
16.BR \%nsenter (1)
ca538975
KZ
17even after the \fIprogram\fR terminates (except PID namespaces where
18permanently running init process is required).
de0f3763 19Once a persistent \%namespace is no longer needed, it can be unpersisted with
0490a6ca 20.BR umount (8).
de0f3763 21See the \fBEXAMPLES\fR section for more details.
0490a6ca
KZ
22.PP
23The namespaces to be unshared are indicated via options. Unshareable namespaces are:
4205f1fd
MG
24.TP
25.BR "mount namespace"
f85b9777
MK
26Mounting and unmounting filesystems will not affect the rest of the system,
27except for filesystems which are explicitly marked as
f0f22e9c
KZ
28shared (with \fBmount --make-shared\fP; see \fI/proc/self/mountinfo\fP or
29\fBfindmnt -o+PROPAGATION\fP for the \fBshared\fP flags).
f85b9777
MK
30For further details, see
31.BR mount_namespaces (7)
32and the discussion of the
33.BR CLONE_NEWNS
34flag in
35.BR clone (2).
cf8e0bae 36.sp
f0f22e9c
KZ
37.B unshare
38since util-linux version 2.27 automatically sets propagation to \fBprivate\fP
de0f3763
BS
39in a new mount namespace to make sure that the new namespace is really
40unshared. It's possible to disable this feature with option
41\fB\-\-propagation unchanged\fP.
f0f22e9c 42Note that \fBprivate\fP is the kernel default.
4205f1fd
MG
43.TP
44.BR "UTS namespace"
dde08a87 45Setting hostname or domainname will not affect the rest of the system.
f85b9777
MK
46For further details, see
47.BR namespaces (7)
48and the discussion of the
49.BR CLONE_NEWUTS
50flag in
51.BR clone (2).
4205f1fd
MG
52.TP
53.BR "IPC namespace"
170a8e4a
MK
54The process will have an independent namespace for POSIX message queues
55as well as System V \%message queues,
f85b9777
MK
56semaphore sets and shared memory segments.
57For further details, see
58.BR namespaces (7)
59and the discussion of the
60.BR CLONE_NEWIPC
61flag in
62.BR clone (2).
4205f1fd
MG
63.TP
64.BR "network namespace"
dde08a87
BS
65The process will have independent IPv4 and IPv6 stacks, IP routing tables,
66firewall rules, the \fI/proc/net\fP and \fI/sys/class/net\fP directory trees,
f85b9777
MK
67sockets, etc.
68For further details, see
69.BR namespaces (7)
70and the discussion of the
71.BR CLONE_NEWNET
72flag in
73.BR clone (2).
4205f1fd 74.TP
2085ba6c 75.BR "PID namespace"
de0f3763 76Children will have a distinct set of PID-to-process mappings from their parent.
f85b9777
MK
77For further details, see
78.BR pid_namespaces (7)
79and
80the discussion of the
81.BR CLONE_NEWPID
82flag in
83.BR clone (2).
bc7f9b95 84.TP
f9e7b66d
SH
85.BR "cgroup namespace"
86The process will have a virtualized view of \fI/proc\:/self\:/cgroup\fP, and new
87cgroup mounts will be rooted at the namespace cgroup root.
f85b9777
MK
88For further details, see
89.BR cgroup_namespaces (7)
90and the discussion of the
91.BR CLONE_NEWCGROUP
92flag in
93.BR clone (2).
f9e7b66d 94.TP
bc7f9b95 95.BR "user namespace"
dde08a87 96The process will have a distinct set of UIDs, GIDs and capabilities.
f85b9777
MK
97For further details, see
98.BR user_namespaces (7)
99and the discussion of the
100.BR CLONE_NEWUSER
101flag in
102.BR clone (2).
4205f1fd
MG
103.SH OPTIONS
104.TP
de0f3763
BS
105.BR \-i , " \-\-ipc" [ =\fIfile ]
106Unshare the IPC namespace. If \fIfile\fP is specified, then a persistent
107namespace is created by a bind mount.
dde08a87 108.TP
de0f3763
BS
109.BR \-m , " \-\-mount" [ =\fIfile ]
110Unshare the mount namespace. If \fIfile\fP is specified, then a persistent
111namespace is created by a bind mount.
112Note that \fIfile\fP has to be located on a filesystem with the propagation
113flag set to \fBprivate\fP. Use the command \fBfindmnt -o+PROPAGATION\fP
114when not sure about the current setting. See also the examples below.
4205f1fd 115.TP
de0f3763
BS
116.BR \-n , " \-\-net" [ =\fIfile ]
117Unshare the network namespace. If \fIfile\fP is specified, then a persistent
118namespace is created by a bind mount.
bc7f9b95 119.TP
de0f3763
BS
120.BR \-p , " \-\-pid" [ =\fIfile ]
121Unshare the PID namespace. If \fIfile\fP is specified then persistent
122namespace is created by a bind mount. See also the \fB--fork\fP and
123\fB--mount-proc\fP options.
bc7f9b95 124.TP
de0f3763
BS
125.BR \-u , " \-\-uts" [ =\fIfile ]
126Unshare the UTS namespace. If \fIfile\fP is specified, then a persistent
127namespace is created by a bind mount.
dde08a87 128.TP
de0f3763
BS
129.BR \-U , " \-\-user" [ =\fIfile ]
130Unshare the user namespace. If \fIfile\fP is specified, then a persistent
131namespace is created by a bind mount.
5088ec33 132.TP
f9e7b66d
SH
133.BR \-C , " \-\-cgroup"[=\fIfile\fP]
134Unshare the cgroup namespace. If \fIfile\fP is specified then persistent namespace is created
135by bind mount.
136.TP
5088ec33 137.BR \-f , " \-\-fork"
87ec43b6 138Fork the specified \fIprogram\fR as a child process of \fBunshare\fR rather than
de0f3763 139running it directly. This is useful when creating a new PID namespace.
6728ca10 140.TP
8e8f0fa5
NH
141.BR \-\-kill\-child
142When \fBunshare\fR terminates, have \fBSIGKILL\fR be sent to the forked child process.
143Combined with \fB--pid\fR this allows for an easy and realiable killing of the entire
144process tree below \fBunshare\fR.
145This option implies \fB--fork\fR.
146.TP
de0f3763 147.BR \-\-mount\-proc [ =\fImountpoint ]
cf8e0bae 148Just before running the program, mount the proc filesystem at \fImountpoint\fP
de0f3763 149(default is /proc). This is useful when creating a new PID namespace. It also
6728ca10 150implies creating a new mount namespace since the /proc mount would otherwise
cf8e0bae 151mess up existing programs on the system. The new proc filesystem is explicitly
de0f3763 152mounted as private (with MS_PRIVATE|MS_REC).
4da21e37 153.TP
b06c1ca6 154.BR \-r , " \-\-map\-root\-user"
cf8e0bae
BS
155Run the program only after the current effective user and group IDs have been mapped to
156the superuser UID and GID in the newly created user namespace. This makes it possible to
157conveniently gain capabilities needed to manage various aspects of the newly created
158namespaces (such as configuring interfaces in the network namespace or mounting filesystems in
159the mount namespace) even when run unprivileged. As a mere convenience feature, it does not support
4da21e37 160more sophisticated use cases, such as mapping multiple ranges of UIDs and GIDs.
de0f3763 161This option implies \fB--setgroups=deny\fR.
fbceefde 162.TP
de0f3763
BS
163.BR "\-\-propagation private" | shared | slave | unchanged
164Recursively set the mount propagation flag in the new mount namespace. The default
165is to set the propagation to \fIprivate\fP. It is possible to disable this feature
166with the argument \fBunchanged\fR. The option is silently ignored when the mount
167namespace (\fB\-\-mount\fP) is not requested.
f0f22e9c 168.TP
de0f3763
BS
169.BR "\-\-setgroups allow" | deny
170Allow or deny the
fbceefde 171.BR setgroups (2)
3be5d977 172system call in a user namespace.
afaf3103
BS
173.sp
174To be able to call
175.BR setgroups (2),
176the calling process must at least have CAP_SETGID.
177But since Linux 3.19 a further restriction applies:
178the kernel gives permission to call
179.BR \%setgroups (2)
180only after the GID map (\fB/proc/\fIpid\fB/gid_map\fR) has been set.
181The GID map is writable by root when
182.BR \%setgroups (2)
183is enabled (i.e. \fBallow\fR, the default), and
184the GID map becomes writable by unprivileged processes when
185.BR \%setgroups (2)
186is permanently disabled (with \fBdeny\fR).
5e43af7e
BS
187.TP
188.BR \-V , " \-\-version"
189Display version information and exit.
190.TP
191.BR \-h , " \-\-help"
192Display help text and exit.
86b6d7f4
KZ
193.SH NOTES
194The proc and sysfs filesystems mounting as root in a user namespace have to be
195restricted so that a less privileged user can not get more access to sensitive
196files that a more privileged user made unavailable. In short the rule for proc
197and sysfs is as close to a bind mount as possible.
69a7761b
LR
198.SH EXAMPLES
199.TP
200.B # unshare --fork --pid --mount-proc readlink /proc/self
201.TQ
2021
203.br
de0f3763 204Establish a PID namespace, ensure we're PID 1 in it against a newly mounted
69a7761b
LR
205procfs instance.
206.TP
207.B $ unshare --map-root-user --user sh -c whoami
208.TQ
209root
210.br
211Establish a user namespace as an unprivileged user with a root user within it.
0490a6ca 212.TP
0490a6ca
KZ
213.B # touch /root/uts-ns
214.TQ
100a3ab5 215.B # unshare --uts=/root/uts-ns hostname FOO
0490a6ca
KZ
216.TQ
217.B # nsenter --uts=/root/uts-ns hostname
218.TQ
219FOO
220.TQ
221.B # umount /root/uts-ns
222.br
de0f3763
BS
223Establish a persistent UTS namespace, and modify the hostname. The namespace
224is then entered with \fBnsenter\fR. The namespace is destroyed by unmounting
225the bind reference.
249fc8fe 226.TP
249fc8fe
KZ
227.B # mount --bind /root/namespaces /root/namespaces
228.TQ
de0f3763 229.B # mount --make-private /root/namespaces
249fc8fe 230.TQ
de0f3763 231.B # touch /root/namespaces/mnt
249fc8fe 232.TQ
99b3fb9e 233.B # unshare --mount=/root/namespaces/mnt
249fc8fe
KZ
234.br
235Establish a persistent mount namespace referenced by the bind mount
de0f3763
BS
236/root/namespaces/mnt. This example shows a portable solution, because it
237makes sure that the bind mount is created on a shared filesystem.
8e8f0fa5
NH
238.TP
239.B # unshare -pf --kill-child -- bash -c "(sleep 999 &) && sleep 1000" &
240.TQ
241.B # pid=$!
242.TQ
243.B # kill $pid
244.br
245Reliable killing of subprocesses of the \fIprogram\fR.
246When \fBunshare\fR gets killed, everything below it gets killed as well.
247Without it, the children of \fIprogram\fR would have orphaned and
248been re-parented to PID 1.
249fc8fe 249
4205f1fd 250.SH SEE ALSO
c07f86e7 251.BR clone (2),
f053ff1e 252.BR unshare (2),
4a3f0735 253.BR namespaces (7),
c07f86e7 254.BR mount (8)
0490a6ca
KZ
255.SH AUTHORS
256.UR dottedmag@dottedmag.net
257Mikhail Gusarov
258.UE
259.br
260.UR kzak@redhat.com
261Karel Zak
262.UE
4205f1fd 263.SH AVAILABILITY
601d12fb 264The unshare command is part of the util-linux package and is available from
d673b74e 265https://www.kernel.org/pub/linux/utils/util-linux/.