Linux Namespaces
Quick Reference
# List namespaces of a process
ls -la /proc/$$/ns/
lsns
# Create new namespace (unshare)
unshare --mount --uts --ipc --net --pid --fork bash
# Enter existing namespace
nsenter --target PID --mount --uts --ipc --net --pid
# View namespace hierarchy
lsns -t net
lsns -p PID
Understanding Namespaces
What are Namespaces?
Linux namespaces provide isolation of system resources. Each namespace type isolates a specific global resource:
-
Mount (mnt) - Filesystem mount points
-
UTS - Hostname and domain name
-
IPC - Inter-process communication
-
Network (net) - Network stack (interfaces, routing, firewall)
-
PID - Process IDs
-
User - User and group IDs
-
Cgroup - Cgroup root directory
-
Time - System clocks (Linux 5.6+)
Namespace Types
| Namespace | Flag | What It Isolates |
|---|---|---|
Mount |
CLONE_NEWNS |
Mount points, filesystem view |
UTS |
CLONE_NEWUTS |
Hostname, domainname |
IPC |
CLONE_NEWIPC |
System V IPC, POSIX message queues |
Network |
CLONE_NEWNET |
Network devices, stacks, ports |
PID |
CLONE_NEWPID |
Process IDs |
User |
CLONE_NEWUSER |
User and group IDs |
Cgroup |
CLONE_NEWCGROUP |
Cgroup root directory |
Time |
CLONE_NEWTIME |
Boot and monotonic clocks |
Viewing Namespaces
Creating Namespaces with unshare
Basic Usage
# Create new UTS namespace (isolated hostname)
unshare --uts bash
hostname isolated-host
hostname # Shows "isolated-host"
exit
hostname # Shows original hostname
# Create new mount namespace
unshare --mount bash
mount --bind /tmp /mnt
ls /mnt # Shows /tmp contents
exit # Mount is gone
Multiple Namespaces
# Create multiple namespaces at once
unshare --mount --uts --ipc --pid --fork bash
# Full container-like isolation
unshare --mount --uts --ipc --net --pid --fork \
--mount-proc bash
# With user namespace (rootless)
unshare --user --map-root-user bash
id # Shows uid=0(root)
Common Options
# --fork: Fork before exec (needed for PID namespace)
unshare --pid --fork bash
# --mount-proc: Mount new /proc (needed for PID namespace)
unshare --pid --fork --mount-proc bash
ps aux # Only shows processes in namespace
# --map-root-user: Map current user to root in user namespace
unshare --user --map-root-user bash
# --propagation: Control mount propagation
unshare --mount --propagation private bash
Entering Namespaces with nsenter
Enter Process Namespaces
# Enter all namespaces of a process
nsenter --target 1234 --all
# Enter specific namespaces
nsenter --target 1234 --mount --uts --ipc --net --pid
# Enter with specific command
nsenter --target 1234 --net ip addr
# Enter container namespace
nsenter --target $(docker inspect -f '{{.State.Pid}}' container_name) \
--mount --uts --ipc --net --pid bash
Network Namespaces
Create and Manage
# Create named network namespace
ip netns add mynetns
# List network namespaces
ip netns list
# Execute in namespace
ip netns exec mynetns ip addr
ip netns exec mynetns bash
# Delete namespace
ip netns delete mynetns
Configure Network Namespace
# Create veth pair for communication
ip link add veth0 type veth peer name veth1
# Move one end to namespace
ip link set veth1 netns mynetns
# Configure host side
ip addr add 10.200.0.1/24 dev veth0
ip link set veth0 up
# Configure namespace side
ip netns exec mynetns ip addr add 10.200.0.2/24 dev veth1
ip netns exec mynetns ip link set veth1 up
ip netns exec mynetns ip link set lo up
# Add default route in namespace
ip netns exec mynetns ip route add default via 10.200.0.1
# Test connectivity
ip netns exec mynetns ping 10.200.0.1
Mount Namespaces
PID Namespaces
User Namespaces
Practical Examples
Simple Container
#!/bin/bash
# Simple container using namespaces
# Create root filesystem
mkdir -p /tmp/container/rootfs
# ... populate with minimal rootfs
# Run container
unshare --mount --uts --ipc --pid --net --fork \
--root=/tmp/container/rootfs \
--mount-proc \
/bin/sh
# Or with pivot_root for full isolation
Network Namespace for Testing
#!/bin/bash
# Create isolated network environment for testing
# Create namespace
ip netns add testnet
# Create veth pair
ip link add vtest0 type veth peer name vtest1
ip link set vtest1 netns testnet
# Configure
ip addr add 192.168.100.1/24 dev vtest0
ip link set vtest0 up
ip netns exec testnet ip addr add 192.168.100.2/24 dev vtest1
ip netns exec testnet ip link set vtest1 up
ip netns exec testnet ip link set lo up
# Run test server in isolated namespace
ip netns exec testnet python3 -m http.server 8080 &
# Access from host
curl http://192.168.100.2:8080
# Cleanup
ip netns delete testnet
Troubleshooting
Permission Denied
# Check if user namespaces are enabled
cat /proc/sys/kernel/unprivileged_userns_clone
# Enable if needed
echo 1 | sudo tee /proc/sys/kernel/unprivileged_userns_clone
# Check subuid/subgid
grep $USER /etc/subuid /etc/subgid
Quick Command Reference
# View namespaces
ls -la /proc/$$/ns/ # Current process
lsns # All namespaces
lsns -t net # Network namespaces
# Create namespaces
unshare --uts bash # UTS only
unshare --net bash # Network only
unshare --mount --uts --ipc --pid --fork bash # Multiple
unshare --user --map-root-user bash # User (rootless)
# Enter namespaces
nsenter --target PID --all # All namespaces
nsenter --target PID --net # Network only
ip netns exec NAME command # Named netns
# Network namespaces
ip netns add NAME # Create
ip netns list # List
ip netns exec NAME command # Execute
ip netns delete NAME # Delete