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