$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

unshare & nsenter

5 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:23

unshare: start a program in new namespaces

The unshare program calls the unshare system call for the flags you give it, and then runs a command in the new namespaces. Each flag names a namespace type. --pid creates a PID namespace, --mount creates a mount namespace, --uts creates a UTS namespace, --net creates a network namespace, and --user creates a user namespace. The command runs inside all of them at once.

The unshare program is in the util-linux package, which is installed in the lab. Its help text lists the flags, and it is the first place to check the exact spelling on your system.

BASH1 lines
unshare --help > unshare-help.txt 2>&1; grep -- "--pid" unshare-help.txt

The PID option has one quirk that trips people up. The unshare call puts the calling process in a new PID namespace, but the next process it creates becomes PID 1 there. For that reason unshare needs --fork, so that the program runs in a child. Without it, the command you run is not the first process in the namespace, and programs that expect to be PID 1 behave oddly.

Mounts inside the namespace

A mount namespace starts as a copy of the parent's mount table. Changes made inside it, such as mounting a new proc filesystem, are invisible outside. The option --mount-proc mounts a fresh /proc in the new namespace, so that tools such as ps show only the processes of the new PID namespace. Without it, /proc still reflects the host, and ps lies about what is running.

BASH1 lines
sudo unshare --pid --fork --mount-proc ps aux

The command above uses sudo, which is not available in the lab. It is shown because it is the usual form on a real system. On your own machine, it prints only the processes inside the namespace, usually just ps and the shell that started it.

Unprivileged use

Creating most namespaces requires the CAP_SYS_ADMIN capability, which root has. A user namespace is the exception. An unprivileged user can create one when the kernel allows it, and inside it that user holds all capabilities. That is how rootless containers work. The kernel may disable unprivileged user namespaces, and some distributions do so by default.

The lab kernel is a custom build, so the result is not predictable. The exercise asks you to run unshare -U true and record the exit status without requiring success. A status of zero means the kernel allowed the user namespace. Any other status means it refused, and that refusal is itself a useful fact about the kernel.

BASH1 lines
unshare -U true; echo "status=$?"
Note

The lab has no man pages. For the reference, use unshare --help and the unshare(1) page on man7.org.

nsenter: enter an existing namespace

nsenter does the reverse. Given the PID of a running process, it joins that process's namespaces and runs a command in them. The -t option names the PID, and options such as -n for the network, -m for mounts, and -p for PIDs choose which namespaces to join. Without options, it joins all of them.

Joining a namespace needs the same privilege as creating one. In practice nsenter is used by administrators and by container runtimes that want to run a debugging tool inside a running container, without installing it there. Its help text lists the options, which the lab step saves to a file.

BASH1 lines
nsenter --help > nsenter-help.txt 2>&1; grep -- "--target" nsenter-help.txt

A checklist for experiments

When you experiment with these tools, check three things in order. First, read the help text for the exact flags. Second, decide which namespace you need and whether you can create it without privilege. Third, look at the result from /proc, comparing namespace links before and after the command. The links are the evidence that the namespace changed, so they are better than any guess from the output alone.

Options you will meet next

The option --map-root-user maps your user ID to zero inside a new user namespace, so the program believes it is root while holding no real privilege outside. It is useful for tests that check how a program behaves as root. The option --kill-child sends a signal to the program when unshare itself exits, which avoids leftover processes in a namespace after a failure.

Interpreting the exit status

Like any wrapper, unshare passes the exit status of the command it runs back to the shell, so the status check written for true works for any program. A status that the shell uses for a command that could not start, such as 126 or 127, usually means the path or the permissions are wrong, and the namespace never came into play. A plain failure with a message on standard error is the case to read carefully, because the message names the failing setup step. Read the message before you change anything, because it is the cheapest evidence you will get.

Why the lab records results instead of requiring them

The lab machine is an emulated kernel that an unprivileged user runs. The kernel may allow some namespaces and refuse others, and the exact list is a fact to discover, not a requirement to satisfy. For that reason the exercises ask you to capture the help text and the exit status, which you can always produce, and they do not ask for a namespace to be created. A real Linux machine can run the same commands and show the result you would expect from a privileged setup.

The habit is useful outside the lab too. When a tool refuses to do something, record the exact message and the status before deciding what it means. A recorded failure is data, and it is often the fastest way to learn how the system is configured.

What a container really is: namespaces, cgroups and a root

Visualizer

A container is not a special kind of object. It is ordinary processes inside namespaces that limit what they can see, cgroups that limit what they can use, and a root directory of their own.

1 / 4
ls -l /proc/PID/ns, one link per namespace (inode numbers are examples)
cgroupcgroup:[4026531835]
ipcipc:[4026531839]
mntmnt:[4026531841]
netnet:[4026531840]
pidpid:[4026531836]
useruser:[4026531837]
utsuts:[4026531838]

Each process points at one namespace of each kind. Two processes with the same inode number share that namespace. This shell uses the default, host-wide ones.

ls -l /proc/$$/ns
Terminal lab · 3 steps · real Linux in your browser

Read the help for unshare and nsenter

Find the namespace flags that unshare and nsenter accept and record what happens when an unprivileged user tries one.

01
Read the unshare options
unshare runs a program in new namespaces. Save its help text so you can read the flags it accepts: ``bash unshare --help > unshare-help.txt 2>&1 `` Open unshare-help.txt and find the lines for --pid, --mount, and --net.
Show hint for step 1
The --help output lists one flag per namespace type. The flags are written with two dashes.
02
Record what an unprivileged try returns
Try to start true in a new user namespace. Your account is unprivileged, so the kernel may refuse. Record the exit status either way: ``bash unshare -U true 2> unshare-err.txt echo "status=$?" > unshare-try.txt `` Open unshare-try.txt. The number after status= is the result. Zero means the kernel allowed it; any other value means it refused.
Show hint for step 2
The status line is written whether unshare works or not. The step checks that you recorded a status, not that the namespace was created.
03
Read the nsenter options
nsenter enters the namespaces of an existing process. Save its help text: ``bash nsenter --help > nsenter-help.txt 2>&1 `` Open nsenter-help.txt and find the --target option, which takes the PID of the process to enter.
Show hint for step 3
The -t or --target option names the process whose namespaces you want to join.
Check yourself Question 1 of 4
Concept Check Flag for a user namespace recall

Which unshare flag creates a new user namespace?

Recap & Key Takeaways

unshare runs a command in new namespaces, where each flag such as --pid, --mount, --uts, --net, or --user creates one type.
A new PID namespace needs --fork, because the first process started inside it becomes PID 1 there.
nsenter joins the namespaces of an existing process by PID, and it needs the same privilege as creating a namespace.
An unprivileged user can create a user namespace only when the kernel allows it, so record the exit status and read it as evidence.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue