unshare
system administrationLinux
The unshare command is one of the most frequently used commands in Linux/Unix-like operating systems. unshare Run program in new namespaces
Quick Reference
Command Name:
unshare
Category:
system administration
Platform:
Linux
Basic Usage:
unshare [options] [arguments]
Common Use Cases
Syntax
unshare [options] [program [arguments]]
Options
| Option | Description |
|---|---|
-m, --mount[=file] |
Unshare the mount namespace |
-u, --uts[=file] |
Unshare the UTS namespace (hostname, etc.) |
-i, --ipc[=file] |
Unshare the IPC namespace |
-n, --net[=file] |
Unshare the network namespace |
-p, --pid[=file] |
Unshare the PID namespace |
-U, --user[=file] |
Unshare the user namespace |
-C, --cgroup[=file] |
Unshare the cgroup namespace |
-T, --time[=file] |
Unshare the time namespace |
--fork |
Fork the specified program as a child process |
--kill-child[=signame] |
When dying, kill the forked child (implies --fork) |
--mount-proc[=mountpoint] |
Mount proc filesystem first (implies --mount) |
--map-root-user |
Map current user to root (implies --user) |
--propagation private|shared|slave|unchanged |
Modify mount propagation in mount namespace |
--setgroups allow|deny |
Control the setgroups syscall in user namespaces |
-r, --root=dir |
Set the root directory |
-w, --wd=dir |
Set the working directory |
-S, --setuid uid |
Set uid in entered namespace |
-G, --setgid gid |
Set gid in entered namespace |
--keep-caps |
Retain capabilities granted in user namespaces |
Examples
How to Use These Examples
The examples below show common ways to use the unshare command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
Run a shell in a new namespace, unsharing mounts
sudo unshare -m /bin/bash
Create a new UTS namespace (hostname isolation)
sudo unshare --uts /bin/bash
Create a new PID namespace
sudo unshare --pid --fork /bin/bash
Advanced Examples:
Create a full container-like environment with multiple namespaces
sudo unshare --mount --uts --ipc --net --pid --fork --user --map-root-user bash
Create a new network namespace and configure it
sudo unshare --net ip link
Create a new mount namespace and mount a tmpfs
sudo unshare -m bash -c "mount -t tmpfs none /mnt && df -h /mnt"