The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
List namespace objectslsnsView examples
Inspect one processlsns --task 4242View examples
Read namespace linksreadlink \ /proc/4242/ns/{user,mnt,pid,net,uts,ipc,cgroup,time}View examples
Compare namespace identitystat -Lc '%i' /proc/self/ns/net /proc/4242/ns/netView examples
Map current user to rootunshare --user --map-root-user --mount /bin/shView examples
Preserve numeric identityunshare --user --map-current-user idView examples
Create private mount viewunshare --user --map-root-user --mount --propagation \ private /bin/shView examples
Create PID namespaceunshare --user --map-root-user --pid --fork --mount-proc \ --kill-child /bin/shView examples
Isolate hostnameunshare --user --map-root-user --uts sh -c \ 'hostname lab && hostname'View examples
Create network namespacesudo unshare --net --fork ip link showView examples
Enter mount namespacesudo nsenter --target 4242 --mount -- findmntView examples
Enter network namespacesudo nsenter --target 4242 --net -- ip address showView examples
Enter target environmentsudo nsenter --target 4242 --all --root --wd -- /bin/shView examples
Persist UTS namespacesudo unshare --uts=/run/namespaces/app-uts hostname \ app-sandboxView examples
Release namespace handlesudo umount /run/namespaces/app-utsView examples

Linux namespaces isolate selected kernel views; they are building blocks, not a complete security boundary. Start by identifying exactly which namespace types a workload uses, combine user namespaces with least privilege, and add filesystem, capability, resource, and syscall controls when containing untrusted code. Entering another process's namespaces can expose its mounts, network, credentials, and secrets, so production debugging should be authorized, audited, PID-specific, and as narrow as possible.

Step by step

Detailed examples

01

Identify namespace membership before debugging

Namespace symlinks under /proc/PID/ns identify objects by type and inode. Two processes share a namespace of a given type when the corresponding identifiers match. A PID can be reused, so obtain it from the service manager and verify command, start time, owner, and namespace identity immediately before privileged entry. Access to another process's procfs entries may be restricted by ownership, capabilities, ptrace policy, or procfs mount options.

Perform a read-only namespace inventory
target_pid=4242
ps -o pid=,ppid=,user=,lstart=,comm= -p "$target_pid"
lsns --task "$target_pid"
readlink /proc/"$target_pid"/ns/{user,mnt,pid,net,uts,ipc,cgroup,time}
stat -Lc '%n %i' /proc/self/ns/net /proc/"$target_pid"/ns/net
Back to quick reference ↑
02

Pair user and mount namespaces for unprivileged experiments

A user namespace can map an unprivileged host user to UID 0 inside and grant capabilities only over resources owned by that user namespace. It does not grant host root. Creating the user namespace first allows it to own a simultaneously created mount namespace, subject to kernel and distribution policy. Mount propagation still matters: use a private propagation boundary and never assume a namespace alone prevents access to host files already visible in its mount tree.

Inspect identity and mount propagation in an ephemeral namespace
unshare --user --map-root-user --mount --propagation private sh -eu -c '
  id
  findmnt -no TARGET,PROPAGATION /
  printf "namespace session ends without modifying host mounts\n"
'
Back to quick reference ↑
03

Give PID namespaces a real init and matching procfs view

A process sees itself as PID 1 only after unshare forks a child into the new PID namespace; --fork is therefore essential. Mounting a new procfs view makes tools such as ps report that namespace rather than the caller's existing procfs. PID 1 has special signal and child-reaping semantics. --kill-child ties descendant lifetime to unshare, reducing orphaned workloads, but it is not a substitute for a supervisor in a long-running service.

Create a disposable PID namespace
unshare --user --map-root-user --pid --fork --mount-proc --kill-child sh -eu -c '
  printf "inner PID: %s\n" "$$"
  ps -o pid=,ppid=,stat=,comm=
'
Back to quick reference ↑
04

Treat UTS and network namespaces as different risk classes

A UTS namespace isolates hostname and NIS domain name, making it useful for low-impact demonstrations. A network namespace isolates devices, routing tables, firewall rules, ports, and sockets; a new one normally has only a down loopback device. Wiring veth devices, routes, forwarding, or firewall policy changes host networking and requires deliberate privileged configuration. The example performs inspection only; use an orchestrator or network-management workflow for production topology.

Inspect isolated hostname and network views
unshare --user --map-root-user --uts sh -eu -c 'hostname lab && hostname'
# CAUTION: sudo grants host-level authority to create the network namespace.
sudo unshare --net --fork sh -eu -c 'ip -brief link show; ip route show'
Back to quick reference ↑
05

Enter only the target contexts required for diagnosis

nsenter opens namespace handles from a target PID and runs a command after joining the selected namespaces. Entering a PID namespace affects subsequently created children, so nsenter forks by default for that case. --all is convenient but broad; --root and --wd can expose the target filesystem, while entering its user namespace normally changes credentials to UID and GID 0 inside. Prefer one read-only diagnostic command over an interactive shell and independently authorize access to production secrets.

Run narrow read-only diagnostics
target_pid=4242
# CAUTION: confirm PID identity immediately before each privileged entry.
ps -o pid=,user=,lstart=,comm= -p "$target_pid"
sudo nsenter --target "$target_pid" --mount -- findmnt
sudo nsenter --target "$target_pid" --net -- ip address show
Back to quick reference ↑
06

Persist namespace handles only with explicit ownership and cleanup

Namespace objects normally disappear after their final member and open reference are gone. util-linux can persist most namespace types by bind-mounting their procfs handle to a path; persistent PID namespaces additionally require a live init process. The handle directory must be a protected, non-shared mount for relevant cases. Unmounting the final handle can destroy the namespace, so treat cleanup as a disruptive operation and confirm no responder, service, or diagnostic session still depends on it.

Review a privileged persistence lifecycle
# CAUTION: these privileged steps create and later release a persistent namespace handle.
sudo install -d -m 0700 /run/namespaces
sudo touch /run/namespaces/app-uts
sudo unshare --uts=/run/namespaces/app-uts hostname app-sandbox
sudo nsenter --uts=/run/namespaces/app-uts -- hostname
# Destructive to the final namespace reference; run only after dependency review.
sudo umount /run/namespaces/app-uts
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Linux man-pages projectnamespaces(7) — overview of Linux namespacesman7.org
  2. util-linux projectunshare(1) — run a program in new namespacesman7.org
  3. util-linux projectnsenter(1) — run a program in another process's namespacesman7.org
  4. Linux man-pages projectuser_namespaces(7) — Linux user namespacesman7.org
  5. Linux man-pages projectmount_namespaces(7) — mount isolation and propagationman7.org

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback