Linux "unshare" Command Line Options and Examples
disassociate parts of the process execution context

Unshares the indicated namespaces from the parent process and then executes the specified program. If program is not given, then ``${SHELL}'' is run (default: /bin/sh).


Usage:

unshare [options] [program [arguments]]






Command Line Options:

-i
Unshare the IPC namespace. If file is specified, then a persistent namespace is created by a bindmount.
unshare -i ...
-m
Unshare the mount namespace. If file is specified, then a persistent namespace is created by a bindmount. Note that file has to be located on a filesystem with the propagation flag set to private. Usethe command findmnt -o+PROPAGATION when not sure about the current setting. See also the examplesbelow.
unshare -m ...
-n
Unshare the network namespace. If file is specified, then a persistent namespace is created by a bindmount.
unshare -n ...
-p
Unshare the PID namespace. If file is specified then persistent namespace is created by a bind mount.See also the --fork and --mount-proc options.
unshare -p ...
-u
Unshare the UTS namespace. If file is specified, then a persistent namespace is created by a bindmount.
unshare -u ...
-U
Unshare the user namespace. If file is specified, then a persistent namespace is created by a bindmount.
unshare -U ...
-C
Unshare the cgroup namespace. If file is specified then persistent namespace is created by bind mount.
unshare -C ...
-f
Fork the specified program as a child process of unshare rather than running it directly. This is use‐ful when creating a new PID namespace.
unshare -f ...
--mount-proc[
Just before running the program, mount the proc filesystem at mountpoint (default is /proc). This isuseful when creating a new PID namespace. It also implies creating a new mount namespace since the/proc mount would otherwise mess up existing programs on the system. The new proc filesystem isexplicitly mounted as private (with MS_PRIVATE|MS_REC).
unshare --mount-proc[ ...
-r
Run the program only after the current effective user and group IDs have been mapped to the superuserUID and GID in the newly created user namespace. This makes it possible to conveniently gain capabili‐ties needed to manage various aspects of the newly created namespaces (such as configuring interfacesin the network namespace or mounting filesystems in the mount namespace) even when run unprivileged.As a mere convenience feature, it does not support more sophisticated use cases, such as mapping multi‐ple ranges of UIDs and GIDs. This option implies --setgroups=deny.
unshare -r ...
--propagation
Recursively set the mount propagation flag in the new mount namespace. The default is to set the prop‐agation to private. It is possible to disable this feature with the argument unchanged. The option issilently ignored when the mount namespace (--mount) is not requested.
unshare --propagation ...
--setgroups
Allow or deny the setgroups(2) system call in a user namespace.To be able to call setgroups(2), the calling process must at least have CAP_SETGID. But since Linux3.19 a further restriction applies: the kernel gives permission to call setgroups(2) only after the GIDmap (/proc/pid/gid_map) has been set. The GID map is writable by root when setgroups(2) is enabled(i.e. allow, the default), and the GID map becomes writable by unprivileged processes when setgroups(2)is permanently disabled (with deny).
unshare --setgroups ...
-V
Display version information and exit.
unshare -V ...
-h
Display help text and exit.NOTESThe proc and sysfs filesystems mounting as root in a user namespace have to be restricted so that a less priv‐ileged user can not get more access to sensitive files that a more privileged user made unavailable. In shortthe rule for proc and sysfs is as close to a bind mount as possible.EXAMPLES# unshare --fork --pid --mount-proc readlink /proc/self1Establish a PID namespace, ensure we're PID 1 in it against a newly mounted procfs instance.$ unshare --map-root-user --user sh -c whoamirootEstablish a user namespace as an unprivileged user with a root user within it.# touch /root/uts-ns# unshare --uts=/root/uts-ns hostname FOO# nsenter --uts=/root/uts-ns hostnameFOO# umount /root/uts-nsEstablish a persistent UTS namespace, and modify the hostname. The namespace is then entered withnsenter. The namespace is destroyed by unmounting the bind reference.# mount --bind /root/namespaces /root/namespaces# mount --make-private /root/namespaces# touch /root/namespaces/mnt# unshare --mount=/root/namespaces/mntEstablish a persistent mount namespace referenced by the bind mount /root/namespaces/mnt. This exampleshows a portable solution, because it makes sure that the bind mount is created on a shared filesystem.
unshare -h ...