Skip to main content

Command Palette

Search for a command to run...

LINUX NAMESPACES

Updated
β€’3 min readβ€’View as Markdown
LINUX NAMESPACES

πŸ”§ Part 1: Foundations (PID & NET Namespaces)

πŸ”’ PID Namespace β€” Process Isolation

Isolates the process tree. Each namespace has its own process numbering starting from PID=1.

unshare --pid --fork --mount-proc /bin/bash
  • Nested namespaces create hierarchical process trees

  • Processes have multiple PIDs in nested scenarios

  • Requires --fork flag

  • cat /proc/PID/status | grep NSpid


🌐 NET Namespace β€” Network Stack Isolation

Isolates interfaces, routing tables, firewall rules, sockets.

sudo unshare --net /bin/bash
  • Starts with loopback only

  • Use veth pairs

  • Use bridges for LAN

  • Masquerading for external access


πŸ” Part 2: Advanced Namespaces

πŸ‘€ USER Namespace β€” UID/GID Mapping

unshare -U /bin/bash
  • Root inside namespace β‰  root outside

  • /proc/PID/uid_map

  • Default UID: 65534

  • Mapping once per namespace


πŸ“ MNT Namespace β€” Filesystem Isolation

sudo unshare -m /bin/bash
  • Mount isolation

  • MS_SHARED / PRIVATE / SLAVE

  • /proc/self/mountinfo

  • pivot_root


🏷 UTS Namespace β€” Hostname Isolation

sudo unshare -u /bin/bash
  • hostname change

  • logging usage

  • zero overhead


πŸ’¬ IPC Namespace β€” IPC Isolation

sudo unshare -i /bin/bash
  • Isolates semaphores

  • ipcs -m

  • clean isolation


βš™οΈ CGROUP Namespace β€” Resource View

sudo unshare --cgroup /bin/bash
  • /sys/fs/cgroup

  • works with limits

  • hides host structure


🧬 Building Containers

unshare -U -m -n -p -i -u --fork /bin/sh
  • USER first

  • UID mapping

  • veth networking

  • pivot_root


πŸ›  Quick Reference

Namespace Flag Command Description
PID --pid unshare --pid --fork Process isolation
NET --net unshare --net Network stack
MNT --mount unshare --mount Filesystem
USER --user unshare -U UID mapping
UTS --uts unshare --uts Hostname
IPC --ipc unshare --ipc IPC isolation
CGROUP --cgroup unshare --cgroup Resource view

πŸ” Debugging

ls /proc/$$/ns
readlink /proc/$$/ns/pid
cat /proc/PID/status | grep Ns

⚑️ Pro Tips

  • Use clone() for advanced control

  • USER namespace first

  • veth for networking

  • pivot_root > chroot

  • combine with cgroups


Based on Quarkslab Linux Namespaces (Part 1 & 2)

πŸ”—References

System

Part 1 of 1

OS, and other system stuff