]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/unshare.1
Merge branch 'eject-sparc' of https://github.com/mator/util-linux
[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 24.TP
0bb7e904 25.B mount namespace
f85b9777
MK
26Mounting and unmounting filesystems will not affect the rest of the system,
27except for filesystems which are explicitly marked as
1c4c6024
BIG
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
0bb7e904 33.B CLONE_NEWNS
f85b9777
MK
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 43.TP
0bb7e904 44.B 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
0bb7e904 49.B CLONE_NEWUTS
f85b9777
MK
50flag in
51.BR clone (2).
4205f1fd 52.TP
0bb7e904 53.B 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
0bb7e904 60.B CLONE_NEWIPC
f85b9777
MK
61flag in
62.BR clone (2).
4205f1fd 63.TP
0bb7e904 64.B 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
0bb7e904 71.B CLONE_NEWNET
f85b9777
MK
72flag in
73.BR clone (2).
4205f1fd 74.TP
0bb7e904 75.B 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
0bb7e904 81.B CLONE_NEWPID
f85b9777
MK
82flag in
83.BR clone (2).
bc7f9b95 84.TP
0bb7e904 85.B cgroup namespace
f9e7b66d
SH
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
0bb7e904 91.B CLONE_NEWCGROUP
f85b9777
MK
92flag in
93.BR clone (2).
f9e7b66d 94.TP
0bb7e904 95.B 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
0bb7e904 100.B CLONE_NEWUSER
f85b9777
MK
101flag in
102.BR clone (2).
be7df01a
AR
103.TP
104.B time namespace
105The process can have a distinct view of
106.B CLOCK_MONOTONIC
107and/or
108.B CLOCK_BOOTTIME
109which can be changed using \fI/proc/self/timens_offsets\fP.
4205f1fd
MG
110.SH OPTIONS
111.TP
de0f3763
BS
112.BR \-i , " \-\-ipc" [ =\fIfile ]
113Unshare the IPC namespace. If \fIfile\fP is specified, then a persistent
114namespace is created by a bind mount.
dde08a87 115.TP
de0f3763
BS
116.BR \-m , " \-\-mount" [ =\fIfile ]
117Unshare the mount namespace. If \fIfile\fP is specified, then a persistent
118namespace is created by a bind mount.
119Note that \fIfile\fP has to be located on a filesystem with the propagation
1c4c6024 120flag set to \fBprivate\fP. Use the command \fBfindmnt \-o+PROPAGATION\fP
de0f3763 121when not sure about the current setting. See also the examples below.
4205f1fd 122.TP
de0f3763
BS
123.BR \-n , " \-\-net" [ =\fIfile ]
124Unshare the network namespace. If \fIfile\fP is specified, then a persistent
125namespace is created by a bind mount.
bc7f9b95 126.TP
de0f3763
BS
127.BR \-p , " \-\-pid" [ =\fIfile ]
128Unshare the PID namespace. If \fIfile\fP is specified then persistent
1c4c6024
BIG
129namespace is created by a bind mount. See also the \fB\-\-fork\fP and
130\fB\-\-mount-proc\fP options.
bc7f9b95 131.TP
de0f3763
BS
132.BR \-u , " \-\-uts" [ =\fIfile ]
133Unshare the UTS namespace. If \fIfile\fP is specified, then a persistent
134namespace is created by a bind mount.
dde08a87 135.TP
de0f3763
BS
136.BR \-U , " \-\-user" [ =\fIfile ]
137Unshare the user namespace. If \fIfile\fP is specified, then a persistent
138namespace is created by a bind mount.
5088ec33 139.TP
f9e7b66d
SH
140.BR \-C , " \-\-cgroup"[=\fIfile\fP]
141Unshare the cgroup namespace. If \fIfile\fP is specified then persistent namespace is created
142by bind mount.
143.TP
f218fd97 144.BR \-T , " \-\-time"[=\fIfile\fP]
be7df01a
AR
145Unshare the time namespace. If \fIfile\fP is specified then a persistent
146namespace is created by a bind mount. The \fB\-\-monotonic\fP and
147\fB\-\-boottime\fP options can be used to specify the corresponding
148offset in the time namespace.
149.TP
5088ec33 150.BR \-f , " \-\-fork"
87ec43b6 151Fork the specified \fIprogram\fR as a child process of \fBunshare\fR rather than
de0f3763 152running it directly. This is useful when creating a new PID namespace.
6728ca10 153.TP
2fb684f0 154.B \-\-keep\-caps
1c4c6024 155When the \fB\-\-user\fP option is given, ensure that capabilities granted
cef4decf
JP
156in the user namespace are preserved in the child process.
157.TP
8b39a17c
NH
158.BR \-\-kill\-child [ =\fIsigname ]
159When \fBunshare\fR terminates, have \fIsigname\fP be sent to the forked child process.
1c4c6024 160Combined with \fB\-\-pid\fR this allows for an easy and reliable killing of the entire
8e8f0fa5 161process tree below \fBunshare\fR.
8b39a17c 162If not given, \fIsigname\fP defaults to \fBSIGKILL\fR.
1c4c6024 163This option implies \fB\-\-fork\fR.
8e8f0fa5 164.TP
de0f3763 165.BR \-\-mount\-proc [ =\fImountpoint ]
cf8e0bae 166Just before running the program, mount the proc filesystem at \fImountpoint\fP
de0f3763 167(default is /proc). This is useful when creating a new PID namespace. It also
6728ca10 168implies creating a new mount namespace since the /proc mount would otherwise
cf8e0bae 169mess up existing programs on the system. The new proc filesystem is explicitly
de0f3763 170mounted as private (with MS_PRIVATE|MS_REC).
4da21e37 171.TP
b06c1ca6 172.BR \-r , " \-\-map\-root\-user"
cf8e0bae
BS
173Run the program only after the current effective user and group IDs have been mapped to
174the superuser UID and GID in the newly created user namespace. This makes it possible to
175conveniently gain capabilities needed to manage various aspects of the newly created
176namespaces (such as configuring interfaces in the network namespace or mounting filesystems in
177the mount namespace) even when run unprivileged. As a mere convenience feature, it does not support
4da21e37 178more sophisticated use cases, such as mapping multiple ranges of UIDs and GIDs.
1c4c6024 179This option implies \fB\-\-setgroups=deny\fR and \fB\-\-user\fR.
4175f29e
JP
180.TP
181.BR \-c , " \-\-map\-current\-user"
182Run the program only after the current effective user and group IDs have been mapped to
183the same UID and GID in the newly created user namespace. This option implies
1c4c6024 184\fB\-\-setgroups=deny\fR and \fB\-\-user\fR.
fbceefde 185.TP
de0f3763
BS
186.BR "\-\-propagation private" | shared | slave | unchanged
187Recursively set the mount propagation flag in the new mount namespace. The default
188is to set the propagation to \fIprivate\fP. It is possible to disable this feature
189with the argument \fBunchanged\fR. The option is silently ignored when the mount
190namespace (\fB\-\-mount\fP) is not requested.
f0f22e9c 191.TP
de0f3763
BS
192.BR "\-\-setgroups allow" | deny
193Allow or deny the
fbceefde 194.BR setgroups (2)
3be5d977 195system call in a user namespace.
afaf3103
BS
196.sp
197To be able to call
198.BR setgroups (2),
199the calling process must at least have CAP_SETGID.
200But since Linux 3.19 a further restriction applies:
201the kernel gives permission to call
202.BR \%setgroups (2)
203only after the GID map (\fB/proc/\fIpid\fB/gid_map\fR) has been set.
204The GID map is writable by root when
205.BR \%setgroups (2)
eb024893 206is enabled (i.e., \fBallow\fR, the default), and
afaf3103
BS
207the GID map becomes writable by unprivileged processes when
208.BR \%setgroups (2)
209is permanently disabled (with \fBdeny\fR).
5e43af7e 210.TP
bf8834d4
LV
211.BR \-R, "\-\-root=\fIdir"
212run the command with root directory set to \fIdir\fP.
213.TP
214.BR \-w, "\-\-wd=\fIdir"
215change working directory to \fIdir\fP.
216.TP
f0af42b5
LV
217.BR \-S, "\-\-setuid \fIuid"
218Set the user ID which will be used in the entered namespace.
219.TP
220.BR \-G, "\-\-setgid \fIgid"
221Set the group ID which will be used in the entered namespace and drop
222supplementary groups.
223.TP
be7df01a
AR
224.BR "\-\-monotonic \fIoffset"
225Set the offset of
226.B CLOCK_MONOTONIC
227which will be used in the entered time namespace. This option requires
228unsharing a time namespace with \fB\-\-time\fP.
229.TP
230.BR "\-\-boottime \fIoffset"
231Set the offset of
232.B CLOCK_BOOTTIME
233which will be used in the entered time namespace. This option requires
234unsharing a time namespace with \fB\-\-time\fP.
235.TP
5e43af7e
BS
236.BR \-V , " \-\-version"
237Display version information and exit.
238.TP
239.BR \-h , " \-\-help"
240Display help text and exit.
86b6d7f4
KZ
241.SH NOTES
242The proc and sysfs filesystems mounting as root in a user namespace have to be
243restricted so that a less privileged user can not get more access to sensitive
244files that a more privileged user made unavailable. In short the rule for proc
245and sysfs is as close to a bind mount as possible.
69a7761b
LR
246.SH EXAMPLES
247.TP
1c4c6024 248.B # unshare \-\-fork \-\-pid \-\-mount-proc readlink /proc/self
69a7761b
LR
249.TQ
2501
251.br
de0f3763 252Establish a PID namespace, ensure we're PID 1 in it against a newly mounted
69a7761b
LR
253procfs instance.
254.TP
1c4c6024 255.B $ unshare \-\-map-root-user \-\-user sh \-c whoami
69a7761b
LR
256.TQ
257root
258.br
259Establish a user namespace as an unprivileged user with a root user within it.
0490a6ca 260.TP
0490a6ca
KZ
261.B # touch /root/uts-ns
262.TQ
1c4c6024 263.B # unshare \-\-uts=/root/uts-ns hostname FOO
0490a6ca 264.TQ
1c4c6024 265.B # nsenter \-\-uts=/root/uts-ns hostname
0490a6ca
KZ
266.TQ
267FOO
268.TQ
269.B # umount /root/uts-ns
270.br
de0f3763
BS
271Establish a persistent UTS namespace, and modify the hostname. The namespace
272is then entered with \fBnsenter\fR. The namespace is destroyed by unmounting
273the bind reference.
249fc8fe 274.TP
1c4c6024 275.B # mount \-\-bind /root/namespaces /root/namespaces
249fc8fe 276.TQ
1c4c6024 277.B # mount \-\-make-private /root/namespaces
249fc8fe 278.TQ
de0f3763 279.B # touch /root/namespaces/mnt
249fc8fe 280.TQ
1c4c6024 281.B # unshare \-\-mount=/root/namespaces/mnt
249fc8fe
KZ
282.br
283Establish a persistent mount namespace referenced by the bind mount
de0f3763
BS
284/root/namespaces/mnt. This example shows a portable solution, because it
285makes sure that the bind mount is created on a shared filesystem.
8e8f0fa5 286.TP
1c4c6024 287.B # unshare \-pf \-\-kill-child \-\- bash \-c "(sleep 999 &) && sleep 1000" &
8e8f0fa5
NH
288.TQ
289.B # pid=$!
290.TQ
291.B # kill $pid
292.br
293Reliable killing of subprocesses of the \fIprogram\fR.
294When \fBunshare\fR gets killed, everything below it gets killed as well.
295Without it, the children of \fIprogram\fR would have orphaned and
296been re-parented to PID 1.
be7df01a
AR
297.TP
298.B # unshare \-\-fork \-\-time \-\-boottime 100000000 uptime
299.TQ
300 10:58:48 up 1158 days, 6:05, 1 user, load average: 0.00, 0.00, 0.00
249fc8fe 301
4205f1fd 302.SH SEE ALSO
c07f86e7 303.BR clone (2),
f053ff1e 304.BR unshare (2),
4a3f0735 305.BR namespaces (7),
c07f86e7 306.BR mount (8)
0490a6ca
KZ
307.SH AUTHORS
308.UR dottedmag@dottedmag.net
309Mikhail Gusarov
310.UE
311.br
312.UR kzak@redhat.com
313Karel Zak
314.UE
4205f1fd 315.SH AVAILABILITY
601d12fb 316The unshare command is part of the util-linux package and is available from
d673b74e 317https://www.kernel.org/pub/linux/utils/util-linux/.