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)
