A container’s service is listening on a port the host cannot see. A process has a mount nobody else does. Something is running “as root” and cannot touch /etc. All of these are namespaces — the kernel feature that lets a process have its own view of mounts, network interfaces, process IDs, hostnames, users and more, and the thing every container runtime is built on. Three commands from util-linux work with them directly, without a runtime in the way: lsns lists the namespaces on the machine and which processes are in each; nsenter runs a command inside another process’s namespaces, which is how you get ss or ip into a container that has neither; and unshare creates new ones, which is how you build a throwaway sandbox in one line or test what a container would see before writing the container.
lsns -t net
nsenter -t PID -n ss -tlnp
unshare -n -m --fork --pid --mount-proc shThe first lists every network namespace with one process from each. The second runs the host’s ss inside the network namespace of process PID — only the network one, so ss itself comes from the host’s filesystem and the output shows the container’s sockets. The third starts a shell in fresh network, mount and PID namespaces, with a /proc that matches. Measured, with a Python listener started inside its own network namespace:
$ ss -tln | grep 9999; echo "rc=$?" # on the host
rc=1
$ lsns -t net -o NS,TYPE,NPROCS,PID,COMMAND
NS TYPE NPROCS PID COMMAND
4026531833 net 63 2 kthreadd
4026532316 net 1 717 python3 -c import socket,time; s=socket.socket(); s.bind(("127.0.0.1",9999)); …
$ nsenter -t 717 -n ss -tlnp
LISTEN 0 128 127.0.0.1:9999 0.0.0.0:* users:(("python3",pid=717,fd=3))
$ nsenter -t 717 -n ip -br addr
lo UNKNOWN 127.0.0.1/8The host sees nothing on port 9999. lsns shows a second network namespace with one process in it; nsenter -t 717 -n runs ss there and finds the listener, and ip shows that namespace’s only interface. Neither tool needed to exist inside the namespace: that is the whole point, and the reason nsenter is the answer to “the container image has no ss, ip, tcpdump or strace“. The PID comes from lsns, from docker inspect -f '{{.State.Pid}}', or from ps.
The PID that is not inside
nsenter enters the namespaces of the exact process you name, and the process that created a namespace is often not in it. unshare --fork leaves the unshare process itself in the original namespaces and puts only its child inside; a container runtime’s own daemon is never inside the container. Target the parent and you are “inside” nothing, with no error to tell you. Measured, with unshare -m -u --fork running a sleep in new mount and hostname namespaces:
$ unshare -m -u -f sh -c 'hostname inner-box; mount -t tmpfs tmpfs /mnt/lab; touch /mnt/lab/secret; sleep 30' &
$ readlink /proc/637/ns/mnt /proc/638/ns/mnt /proc/self/ns/mnt # unshare, its child, this shell
mnt:[4026531832]
mnt:[4026532265]
mnt:[4026531832]
$ nsenter -t 638 -m -u sh -c 'hostname; ls /mnt/lab'
inner-box
secret
$ hostname; ls /mnt/lab
vmPID 637 is unshare, and it is in the same mount namespace as the host shell; PID 638, the child it forked, is in the new one. Entering 638 shows the inner hostname and the tmpfs; the host sees neither. Entering 637 would have shown the host’s own view and reported nothing wrong. The check is the one in the middle: readlink /proc/PID/ns/mnt (or net, pid, uts…) prints the namespace’s identity, and if it matches your own shell’s you have the wrong PID. lsns -p PID says the same thing in a table.
Options
| Command and flag | Meaning |
|---|---|
lsns | Every namespace: id, type, process count, one PID, user, command. Needs root to see other users’ processes |
lsns -t TYPE | Only one type: mnt, net, pid, user, uts, ipc, cgroup, time (one -t per type) |
lsns -p PID / lsns NS | The namespaces of one process / the processes in one namespace id |
lsns -o COLS / -n / -J | Choose columns (NS,TYPE,NPROCS,PID,PPID,USER,UID,COMMAND,NSFS) / no header / JSON |
nsenter -t PID | Target process; combine with the namespace flags below. Needs CAP_SYS_ADMIN (root) for most of them |
-m -u -i -n -p -U -C -T | Enter the target’s mount, UTS (hostname), IPC, network, PID, user, cgroup and time namespaces respectively |
nsenter -t PID -a | Enter all of them: the closest thing to “exec into the container” without the runtime |
nsenter --mount=/path --net=/path | Enter a namespace by its file instead of a PID: /proc/PID/ns/net, or a bind-mounted persistent namespace such as /run/netns/NAME |
nsenter -r / -w | Also set the root directory / working directory to the target’s — without -r, -m gives you the target’s mounts but not its chroot |
nsenter -S UID -G GID | Run as this uid/gid inside |
nsenter -F | Do not fork when entering a PID namespace (default is to fork, so the command gets a PID inside) |
unshare -m -u -i -n -p -U -C -T | Create new namespaces of these types for the command (default: your $SHELL) |
unshare -f / --fork | Fork before running the command; required with -p, because the process that calls unshare(CLONE_NEWPID) stays in its old PID namespace and only its children get the new one |
unshare --mount-proc | Mount a fresh /proc inside (implies -m); without it ps in a new PID namespace still reads the host’s |
unshare -r | New user namespace with the caller mapped to uid 0 inside: “root” without being root, the basis of rootless containers |
unshare --map-user=U --map-group=G | Map to a different uid/gid instead of 0 |
unshare --propagation private|slave|shared|unchanged | Mount propagation for the new mount namespace (default private) |
unshare --kill-child | Kill the forked child if unshare itself dies |
Building a sandbox by hand
unshare is the fastest way to find out what a namespace does, and each flag teaches one thing. A new PID namespace needs --fork, and without --mount-proc it is only half there:
$ unshare -pf sh -c 'echo "pid $$"; ps -e | wc -l'
pid 1
69
$ unshare -pf --mount-proc sh -c 'echo "pid $$"; ps -eo pid,comm'
pid 1
PID COMMAND
1 sh
2 psIn both the shell is PID 1 of its namespace. In the first, ps still reads the host’s /proc and lists 69 processes; in the second, a fresh /proc is mounted in a new mount namespace and ps sees two. That is the difference between a PID namespace and a working PID namespace, and every container runtime does the second. A new user namespace is the one an ordinary user can make without root:
$ id -u
30033
$ unshare -Ur sh -c 'id; cat /proc/self/uid_map; touch /etc/x'
uid=0(root) gid=0(root) groups=0(root)
0 30033 1
touch: cannot touch '/etc/x': Permission denied
$ unshare -Urm sh -c 'mount -t tmpfs tmpfs /mnt/lab && findmnt /mnt/lab -no SOURCE'
tmpfsInside, id says root; the uid_map says that “root” is uid 30033 on the outside, one id wide; and /etc is still refused, because the kernel checks the real uid against the file. But that fake root is real within the namespace: with -m as well, the user can mount a tmpfs — something that needs CAP_SYS_ADMIN on the host — because the capability is held in a namespace where it only reaches that user’s own mounts. This is exactly how rootless Podman and Docker work, and unshare -Urm is a one-line way to test whether an “unprivileged” build step is possible before writing the Containerfile.
Getting tools into a container that has none
The everyday use is debugging. A distroless or Alpine image has no ss, no ip, no tcpdump, no strace; the host has all of them. nsenter -t PID -n borrows only the network namespace, so the host’s binary runs with the host’s libraries against the container’s sockets. -n -p adds the PID namespace, so ss -p and strace -p see the container’s process numbers. Adding -m brings in the container’s filesystem view, which is what you want for ls and cat but breaks any host binary that needs libraries the image lacks — so a useful habit is two terminals, one with -n -p for the network and process tools and one with -a (or -m -u -i -n -p -r -w) for the “inside” view. tcpdump through nsenter -n captures a container’s traffic without installing anything in it, and nsenter -t PID -n ip route is usually the fastest way to see why a container cannot reach something.
Where they do not reach
Namespaces change what a process can see, not what it is allowed to do: cgroups limit its resources, capabilities limit its privileges, and seccomp limits its system calls, and a container is all four together. nsenter brings none of the other three with it — a command entered with -a as root runs with the host’s full capabilities inside the container’s view, which is more than the container’s own processes have and occasionally exactly why the debugging command works when the container’s did not. lsns reads /proc, so it can only list namespaces that have a process in them (or a bind mount at a path it can find); an empty persistent network namespace made with ip netns add shows up in ip netns, not here. And none of this is a security boundary on its own: a user namespace with a mapped root is safe because the kernel treats that root as unprivileged outside, which is a property to understand before relying on it. Containers, explained is the page on how the pieces fit.
Common problems
| Symptom | Cause | Check / fix |
|---|---|---|
nsenter “works” but shows the host’s view | Targeted the parent (unshare, the runtime daemon, a shim) rather than a process inside | readlink /proc/PID/ns/net vs your own; lsns -p PID; use the container’s main PID |
nsenter: neither filename nor target pid supplied | Namespace flags without -t | Add -t PID or --net=/path |
Operation not permitted | Not root, or inside a container without CAP_SYS_ADMIN/CAP_SYS_PTRACE | Run from the host as root |
Entered -m and the host’s tool fails to start | Target’s filesystem lacks the tool’s libraries | Drop -m for network/process tools; or -m without -r is not a chroot — add -r when you want the container’s root |
ps in a new PID namespace shows the host’s processes | No fresh /proc | unshare --mount-proc, or mount -t proc proc /proc inside a new mount namespace |
unshare -p without -f: “Cannot allocate memory” or odd failures | The caller cannot be in the new PID namespace; only children can | Always unshare -pf |
unshare -r refused | Unprivileged user namespaces disabled (kernel.unprivileged_userns_clone=0, or an AppArmor restriction on newer Ubuntu) | sysctl kernel.unprivileged_userns_clone; Ubuntu 24.04: kernel.apparmor_restrict_unprivileged_userns |
Mount made in unshare -m is visible on the host (or the reverse) | Propagation | --propagation private (default) isolates; findmnt -o PROPAGATION on the parent |
lsns does not show a namespace you created with ip netns | No process in it; it exists as a bind mount under /run/netns | ip netns list; nsenter --net=/run/netns/NAME |
Killing unshare leaves the child running | Child is reparented, namespace lives on | unshare --kill-child, or kill the child PID |
Quick reference
lsns # every namespace, one line each
lsns -t net # just network namespaces
lsns -p PID # which namespaces a process is in
readlink /proc/PID/ns/* # the same, as ids; compare with /proc/self/ns/*
PID=$(docker inspect -f '{{.State.Pid}}' NAME) # a container's main process
nsenter -t $PID -n ss -tlnp # host's ss, container's sockets
nsenter -t $PID -n ip -br addr; nsenter -t $PID -n ip route
nsenter -t $PID -n tcpdump -i any port 443 # capture inside, no install
nsenter -t $PID -n -p strace -p 1 # host's strace on the container's PID 1
nsenter -t $PID -a # a shell "inside" (host privileges!)
nsenter -t $PID -m -r -w sh # mounts + chroot + cwd, no net/pid change
nsenter --net=/run/netns/NAME ip addr # a persistent netns by path
unshare -n sh # shell with no network at all
unshare -pf --mount-proc sh # shell that is PID 1 with its own /proc
unshare -Ur sh # fake root as an ordinary user
unshare -Urm sh # ...that can mount tmpfs
unshare -m --propagation private sh # private mounts for an experimentRelated reading
- Containers, explained — the long one on namespaces, cgroups, capabilities and seccomp, and how a runtime assembles them
- findmnt —
-N PIDreads a mount namespace from outside, andPROPAGATIONis why a mount does or does not cross - setcap and getcap — the capabilities
nsentercarries in and a runtime trims away - ss — the tool most often borrowed through
nsenter -n - strace — the other one, with
-pfor the PID namespace - What Ships in Your Container Image — why the image has no
ssin the first place, and why that is fine

