Linux Command #70: unshare — How Containers Isolate Processes

Original oil painting of a systems engineer in a blue-and-amber data center, with translucent chambers representing isolated Linux workloads and the approved BitcoinVersus.tech neon-green-on-black watermark once at bottom-left.

Linux containers begin with ordinary processes, not miniature virtual machines. The unshare command launches a program with selected Linux namespaces separated from its parent. A namespace changes which processes, hostnames, mounts, network interfaces or user identifiers a program can see. Administrators use the technique to study container internals, troubleshoot isolation and understand how runtimes create isolated workloads.

Part of the util-linux suite, unshare calls kernel namespace machinery rather than emulating another operating system. Programs inside and outside the namespace still share a Linux kernel. The distinction matters for both performance and security.

Video: Techi Nik, “Build a Linux Container from Scratch,” March 12, 2026. The hands-on demonstration uses unshare to explore PID, UTS, network and mount namespaces.

What namespaces isolate

A PID namespace gives processes a separate process-ID view. A UTS namespace provides an independent hostname; a mount namespace separates filesystem mount tables; and a network namespace separates interfaces, routes and sockets. User namespaces map user and group IDs and scope capabilities. IPC, cgroup and time namespaces isolate additional kernel views. Administrators can inspect namespace identifiers through /proc/self/ns/. The underlying Linux system-call interface remains shared.

A safe first experiment

On a Linux host where unprivileged user namespaces are enabled, enter a new user, PID and mount namespace with unshare --user --map-root-user --pid --fork --mount-proc sh. The --user flag creates a user namespace; --map-root-user maps the invoking user to UID 0 inside it. The mapping does not grant unrestricted root access to the host.

--pid changes PID numbering for children, while --fork starts the shell as the first process in the new PID namespace. The --mount-proc option mounts an appropriate /proc view and creates a mount namespace. Run ps -o pid,comm -p 1: the shell should appear as PID 1 inside the new namespace. Type exit to leave.

An Operation not permitted error may mean the kernel, administrator policy or container security profile blocks namespace creation. Do not weaken production host security settings merely to complete a lesson.

Why PID 1 needs special care

The first process in a PID namespace occupies a special position. It may need to reap orphaned children and handle shutdown signals. A shell is adequate for a brief demonstration, but a service manager or purpose-built init process is often appropriate for a longer-lived workload. The topic connects with our explanation of how systemd supervises Linux services.

Video: “Containers — A Look Under the Hood,” Gerlof Langeveld, Linux Foundation ContainerCon / Open Source Summit Europe 2022. The conference session builds isolated Linux environments using unshare and related kernel facilities.

Namespaces are not complete security sandboxes

Isolating names does not automatically restrict CPU time, memory, filesystem access or system calls. Real container systems combine namespaces with control groups, restricted capabilities, seccomp filtering, mount policy and Linux security modules such as SELinux or AppArmor. Our systemd-cgtop guide covers cgroup resource visibility, while the systemd-run lesson explains transient service limits.

Never assume a rootless namespace makes untrusted code harmless. Misconfigured mounts, inherited file descriptors, permissive kernel interfaces and host-level vulnerabilities can still create risks. A namespace alone is an isolation primitive, not a reviewed security boundary.

Practice and verification

  • Use readlink /proc/self/ns/pid before and after entering a PID namespace; compare identifiers from the appropriate processes.
  • Inspect unshare --help and man unshare for supported flags on the installed util-linux version.
  • Exit the test shell, then inspect the host process view again. A new namespace does not replace the host environment.

The essential lesson is that containers are assembled from kernel mechanisms with different responsibilities. Understanding unshare makes those boundaries observable rather than mysterious.

Technical references

Cover: original oil-painting editorial illustration, not a real container deployment. The existing BitcoinVersus.tech watermark artwork was reused at bottom-left without re-typesetting.

Leave a Reply