Explore Namespaces From the Shell With unshare and nsenter
Use unshare and nsenter from a shell to create and join Linux namespaces, and watch hostnames, process lists and mounts change inside a new one.

The fastest way to understand namespaces is to make one and look around. Two tools from util-linux 2.40 do it without writing any C: unshare starts a command in new namespaces, and nsenter starts a command inside namespaces that already exist. This post walks through five short experiments on a Linux 6.12 machine with systemd: a private hostname, a private process list, a private mount, a private network and a rootless shell. Each one prints what you should see, so you can tell whether your system behaves differently, for example because it restricts unprivileged user namespaces.
The tools in one minute
unshare is a thin wrapper around the unshare(2) system call. You give it flags for the namespaces you want and a command to run, and it creates the namespaces, then executes the command inside them. Without a command it starts your shell. nsenter wraps setns(2): you name a target process, it opens that process's namespace files under /proc/PID/ns and joins them before running your command.
Most of these experiments need root, because creating most namespaces needs CAP_SYS_ADMIN. The last one uses a user namespace to show the same things without root. Run everything in a disposable shell. None of it changes the host's real hostname, mounts or network, and that is the point.
Experiment 1: a private hostname
The UTS namespace is the simplest one. Open a shell with a new UTS namespace and change the hostname inside it.
sudo unshare --uts bash -c 'hostname sandbox; hostname'
hostnamesandbox
devboxThe first command ran hostname sandbox and then printed the name, inside the new namespace. The second command ran back in your original shell and shows the host's real name, unchanged. Replace devbox with whatever your machine is called.
Experiment 2: a private process list
A PID namespace needs two extra pieces to look right. The flag --fork makes unshare fork before running the command, which is required because the calling process does not move into a new PID namespace: only its children do. The flag --mount-proc mounts a fresh /proc for the new namespace, which also implies a new mount namespace, so that ps reads the new PID table rather than the host's.
sudo unshare --pid --fork --mount-proc bash -c 'echo $$; ps -e'1
PID TTY TIME CMD
1 pts/0 00:00:00 bash
2 pts/0 00:00:00 psThe shell is PID 1 inside the namespace, and it is the only process besides ps. On the host the same shell has some large number; you can see it from another terminal with pgrep -f 'unshare --pid'. Omit --mount-proc and ps -e will list the host's processes, because it reads the host's /proc, which is the usual confusion the first time you try this.
Experiment 3: a private mount
A mount namespace lets you mount something that nobody else sees. On a systemd machine the root mount is shared, so unshare makes the new namespace's mounts private by default (the --propagation private default in util-linux), which is what keeps this from leaking.
sudo unshare --mount bash -c 'mount -t tmpfs tmpfs /mnt && touch /mnt/inside && ls /mnt'
ls /mnt
findmnt /mntinsideThe first ls printed inside from within the namespace. The second ls and findmnt in your normal shell printed nothing, because /mnt there is the ordinary empty directory and has no mount. When the namespace's last process exits the kernel drops the tmpfs and its contents.
Try the same with --propagation shared and the mount appears on the host too, which shows why propagation matters when you build a container runtime.
Experiment 4: a private network
A new network namespace starts with a loopback interface that is down, and nothing else.
sudo unshare --net bash -c 'ip link; ip link set lo up; ip -br addr'1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
lo UNKNOWN 127.0.0.1/8 ::1/128Inside, there is no eth0 and no route out, so a ping 8.8.8.8 would fail with "Network is unreachable". Bringing lo up gives it its usual addresses and lets processes inside talk to each other over localhost. Wiring such a namespace to the host is done with virtual ethernet pairs, which a later post in this series covers.
Experiment 5: all of it without root
On kernels that allow unprivileged user namespaces, you can ask for a user namespace first and map yourself to root inside it with --map-root-user. The other namespaces then belong to that user namespace, so you hold the needed capability inside it.
unshare --user --map-root-user --uts --pid --fork --mount-proc bash -c 'id -u; hostname rootless; hostname; ps -e'0
rootless
PID TTY TIME CMD
1 pts/0 00:00:00 bash
2 pts/0 00:00:00 psYou are UID 0 inside the namespace and your normal user on the host. If the command fails with "unshare: unshare failed: Operation not permitted", your distribution restricts unprivileged user namespaces, for example through the kernel.unprivileged_userns_clone sysctl on some kernels or through an AppArmor profile on Ubuntu 24.04. That is a policy choice, not a bug in the command.
Joining a namespace with nsenter
nsenter goes the other way. Start a long-running process in a new namespace, find its PID from the host, and enter its namespaces.
sudo unshare --uts --fork bash -c 'hostname demo; sleep 300' &
sleep 1
pid=$(pgrep -f 'sleep 300' | head -n 1)
sudo nsenter --target "$pid" --uts hostnamedemo--target names the process whose namespaces to join and --uts says which type. Add --pid --mount --net to join more, or --all to join every namespace of the target, which is how docker exec-style access works. For a container, the target is the container's init process.
You can check the result without entering at all by comparing readlink /proc/$pid/ns/uts with readlink /proc/self/ns/uts. Different inode numbers mean different namespaces.
Cleaning up
The background job from the last example is a sleep in its own UTS namespace. Stop it with sudo pkill -f 'sleep 300'. The namespace disappears with it, because nothing holds a reference. If you want a namespace to outlive its processes, bind-mount its /proc/PID/ns/uts file somewhere, which is exactly how ip netns add keeps named network namespaces.
Reading the result from outside
Every experiment above leaves evidence you can check from the host without entering anything. While a sleep runs inside the namespaces, list its links under /proc/PID/ns and compare the inode numbers with those of your own shell: a namespace that differs is a new one, and one that matches is shared. The lsns command from util-linux prints the same information as a table, with one row per namespace, its type, the number of processes in it and the PID of the first one, which is the quickest way to find the process to give to nsenter.
sudo lsns --type uts NS TYPE NPROCS PID USER COMMAND
4026531838 uts 312 1 root /sbin/init
4026532611 uts 1 9921 root sleep 300The first row is the host's UTS namespace shared by every ordinary process, and the second is the one created by the unshare call, holding one process. The numbers and the PID will differ on your machine. When an experiment seems to do nothing, lsns usually explains why: either no new namespace was created, because a flag was missing, or the process you are looking at is not the one inside it, which happens when you forget --fork and look at the wrapper instead of its child.


