| name | linux-network-namespaces |
| description | How to work with Linux network namespaces for security testing, containerization, and system administration. Use this skill whenever the user mentions network namespaces, network isolation, container networking, nsenter, unshare, veth pairs, or needs to inspect/create/enter network namespaces. Also trigger when users want to understand network stack isolation, troubleshoot container networking, or perform privilege escalation research involving network namespaces. |
Linux Network Namespaces
A skill for working with Linux network namespaces to understand, create, inspect, and manipulate network isolation on Linux systems.
What are Network Namespaces?
Network namespaces are a Linux kernel feature that provides isolation of the network stack. Each namespace has its own:
- Network interfaces
- IP addresses
- Routing tables
- Firewall rules (iptables/nftables)
- Port bindings
This is essential for containerization, security testing, and understanding Linux network isolation.
When to Use This Skill
Use this skill when you need to:
- Create isolated network environments for testing
- Inspect or enumerate network namespaces on a system
- Enter or interact with existing network namespaces
- Understand container networking (Docker, Kubernetes, etc.)
- Perform security research on network isolation
- Troubleshoot network connectivity in containerized environments
Quick Reference
Create a New Network Namespace
sudo unshare -n /bin/bash
sudo unshare -n --mount-proc /bin/bash
sudo unshare -nf /bin/bash
Inspect Current Namespace
ls -l /proc/self/ns/net
ip -a
ifconfig
List All Network Namespaces
sudo find /proc -maxdepth 3 -type l -name net -exec readlink {} \; 2>/dev/null | sort -u | grep "net:"
sudo find /proc -maxdepth 3 -type l -name net -exec ls -l {} \; 2>/dev/null | grep <NS_NUMBER>
Enter an Existing Namespace
sudo nsenter -t TARGET_PID -n /bin/bash
sudo nsenter -n TARGET_PID --pid /bin/bash
Important: You need root privileges to enter another process's namespace, and you need a descriptor pointing to it (like /proc/self/ns/net).
Docker Network Namespaces
docker run -ti --name ubuntu1 ubuntu bash
ip -a
Common Tasks
Task 1: Create an Isolated Network Environment
sudo unshare -n --mount-proc /bin/bash
ip -a
ping -c 1 8.8.8.8
Task 2: Connect Two Namespaces with Veth Pair
sudo ip link add veth0 type veth peer name veth1
sudo ip link set veth1 netns <PID_OR_NS_NAME>
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip link set veth0 up
sudo ip addr add 10.0.0.2/24 dev veth1
sudo ip link set veth1 up
sudo ip link set lo up
ping 10.0.0.1
Task 3: Enumerate Namespaces for Security Research
for ns in $(sudo find /proc -maxdepth 3 -type l -name net -exec readlink {} \; 2>/dev/null | sort -u | grep "net:"); do
echo "=== $ns ==="
sudo find /proc -maxdepth 3 -type l -name net -exec ls -l {} \; 2>/dev/null | grep "$ns" | awk '{print $9}' | xargs -I {} basename {}
done
sudo ls -l /proc/*/ns/net 2>/dev/null | sort -u
Task 4: Inspect Namespace Network Configuration
sudo nsenter -t <PID> -n -- bash -c '
echo "=== Interfaces ==="
ip -a
echo "=== Routes ==="
ip route
echo "=== ARP ==="
ip neigh
echo "=== Firewall ==="
iptables -L -n 2>/dev/null || echo "No iptables access"
'
Troubleshooting
Error: "bash: fork: Cannot allocate memory"
Cause: When unshare creates a new PID namespace without the -f flag, the process doesn't enter the namespace itself - only child processes do. If PID 1 exits, the namespace disables PID allocation.
Solution: Use the -f (fork) flag:
sudo unshare -n /bin/bash
sudo unshare -nf /bin/bash
Cannot Enter Namespace
Cause: You need root privileges and a valid namespace descriptor.
Solution:
sudo -i
ls -l /proc/<PID>/ns/net
sudo nsenter -t <PID> -n /bin/bash
No Network Connectivity in New Namespace
Expected behavior: New namespaces start with only the loopback interface. To add connectivity:
- Create veth pairs
- Move one end to the namespace
- Configure IP addresses on both ends
- Optionally add routing or bridge to host
Security Considerations
- Privilege escalation: Network namespaces can be used to bypass network-based security controls
- Container escape: Understanding namespaces helps identify container escape vectors
- Network isolation testing: Verify containers are properly isolated from host and each other
- Firewall rules: Each namespace has independent iptables/nftables rules
References
Scripts
For common namespace operations, use the bundled scripts:
scripts/list-namespaces.sh - List all network namespaces and their processes
scripts/enter-namespace.sh - Enter a namespace by PID or name
scripts/create-namespace.sh - Create and optionally connect a new namespace
scripts/inspect-namespace.sh - Inspect network configuration of a namespace