š§ Linux Network Namespaces: A Practical Guide to Isolation and Virtual Networking
A practical guide to creating, configuring, and using Linux network namespaces to isolate networking stacks, build virtual networks, and test routing and firewall behavior.
Introduction
Linux network namespaces are a powerful feature of the Linux kernel that allow you to create isolated network stacks within a single host. Each network namespace has its own interfaces, routing tables, firewall rules, and network configuration. With network namespaces, you can build test labs, simulate complex topologies, and isolate services without needing multiple physical machines or virtual machines.
For cybersecurity learners and CTF players, network namespaces are practically indispensable. They let you experiment with routing, NAT, firewalling, and tunneling in an isolated environment, safely and reversibly. For developers and system administrators, they underpin container networking in Docker, Kubernetes, and LXC, so understanding them demystifies how container networking really works.
This article walks through the core concepts and then provides a hands-on tour of creating namespaces, wiring them together, and using them to test network behavior. By the end, youāll have a solid foundation for applying network namespaces to your own projects and experiments.
Core Concepts
What Is a Network Namespace?
A network namespace is a logical copy of the network stack. Processes inside a namespace only see and control the interfaces, routes, and firewall rules belonging to that namespace. This isolation applies to:
- Network interfaces (except the loopback, which needs to be brought up manually)
- IP addresses, routing tables, and the neighbor (ARP) cache
- Netfilter rules (iptables, nftables, etc.)
- Network sockets and port numbers
- Network kernel parameters (e.g.,
net.*sysctl values) that are namespace-aware
When you create a network namespace, the newly created process space has no physical interfaces, and routing is empty until you configure it. This is ideal for building clean, controlled test environments.
The ip Command
The primary tool for working with namespaces is ip, from the iproute2 package. It allows you to create, delete, and manage namespaces, as well as move interfaces into and out of them. Almost every command we use in this article is from iproute2.
Virtual Ethernet Peers (veth)
A veth pair is a virtual cable with two ends. Each end can be placed in a different network namespace, connecting them together. When packets are sent on one end, they appear exactly on the other end, like a direct Ethernet link.
Bridges
A Linux bridge is a virtual switch. You can attach multiple veth endpoints to a bridge, and all attached interfaces can communicate at layer 2. Bridges are essential for creating larger topologies where more than two namespaces need to share a network.
Network Address Translation (NAT)
The Linux kernel has built-in NAT via Netfilter. With iptables or nftables, you can masquerade traffic from a namespace that uses private IP addresses so it can reach external networks through the default (host) namespace.
Practical Walkthrough
All commands in this section assume you are root, or you prefix them with sudo. We also assume you have a Linux system with iproute2 installed. The examples are distribution-agnostic, but specific command syntax may vary slightly. All commands are run on the host unless otherwise stated.
1. Creating and Listing Namespaces
To create a namespace named ns1, run:
ip netns add ns1
List all namespaces with:
ip netns list
You should see ns1. The namespaces are managed by the kernel and are available to any process, which is why root privileges are required.
To execute commands inside a namespace, use ip netns exec:
ip netns exec ns1 ip addr show
This shows the network interfaces visible inside ns1. At creation, only lo (loopback) is present, and it is down. Try it:
ip netns exec ns1 ping 127.0.0.1
This fails because the loopback is down. Bring it up:
ip netns exec ns1 ip link set lo up
Now the ping inside the namespace works:
ip netns exec ns1 ping 127.0.0.1
You have a fully isolated network stack with its own loopback.
2. Connecting Two Namespaces Directly
To connect two namespaces, create a veth pair and assign each end to a different namespace. Letās create ns2 and connect it to ns1.
ip netns add ns2
Create a veth pair named veth1 and veth2:
ip link add veth1 type veth peer name veth2
Move veth1 into ns1 and veth2 into ns2:
ip link set veth1 netns ns1
ip link set veth2 netns ns2
Now configure IP addresses and bring the interfaces up. Inside ns1:
ip netns exec ns1 ip addr add 10.0.0.1/24 dev veth1
ip netns exec ns1 ip link set veth1 up
Inside ns2:
ip netns exec ns2 ip addr add 10.0.0.2/24 dev veth2
ip netns exec ns2 ip link set veth2 up
Test connectivity between the namespaces:
ip netns exec ns1 ping 10.0.0.2
This should succeed. You now have a point-to-point network between two isolated stacks.
3. Building a Virtual Network with a Bridge
For more than two namespaces, a bridge gives you a proper Ethernet network. Letās create a bridge in the default namespace and attach several namespaces to it.
First, create three namespaces: red, green, and blue.
ip netns add red
ip netns add green
ip netns add blue
Create a bridge named br0 in the host namespace:
ip link add br0 type bridge
ip link set br0 up
Now create veth pairs to connect each namespace to the bridge. Weāll name them with a per-namespace prefix:
ip link add veth-red type veth peer name veth-red-br
ip link add veth-green type veth peer name veth-green-br
ip link add veth-blue type veth peer name veth-blue-br
Move the namespace-side endpoints into their namespaces:
ip link set veth-red netns red
ip link set veth-green netns green
ip link set veth-blue netns blue
For each namespace, configure an IP address and bring up the interface. For red:
ip netns exec red ip addr add 192.168.10.2/24 dev veth-red
ip netns exec red ip link set veth-red up
Repeat for green (192.168.10.3) and blue (192.168.10.4).
Now attach the bridge-side endpoints to the bridge:
ip link set veth-red-br master br0
ip link set veth-green-br master br0
ip link set veth-blue-br master br0
Bring the bridge-side interfaces up:
ip link set veth-red-br up
ip link set veth-green-br up
ip link set veth-blue-br up
Test connectivity between red and blue:
ip netns exec red ping 192.168.10.4
You should get a response. You now have a working virtual switch connecting three isolated network namespaces. This is the same mechanism that container orchestration platforms use to place containers on a shared virtual network.
4. Providing Internet Access via NAT
By default, namespaces connected to a bridge can talk to each other, but they cannot reach the outside world because they have no route to external networks. To give them internet access, we configure the host to perform NAT.
First, ensure the bridge has an IP address on the same subnet as the namespaces. For example:
ip addr add 192.168.10.1/24 dev br0
Now add a default route inside each namespace pointing to the bridge IP. For red:
ip netns exec red ip route add default via 192.168.10.1
Repeat for green and blue.
Enable IP forwarding on the host (assuming you want the namespaces to reach the internet):
sysctl -w net.ipv4.ip_forward=1
Finally, add a NAT rule using iptables (assuming your hostās outbound interface is eth0; adjust as needed):
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE
Now test from a namespace:
ip netns exec red ping 8.8.8.8
If your host has internet connectivity, this should work. You have built a NAT-enabled virtual network, exactly what Docker does for its default bridge network.
5. Testing Firewall Rules Inside a Namespace
Because each namespace has its own Netfilter instance, you can test iptables rules in isolation. This is useful for learning and validating firewall behavior without affecting the host.
For example, inside red, you can drop all ICMP (ping) traffic:
ip netns exec red iptables -A INPUT -p icmp -j DROP
Now from green, attempt to ping red:
ip netns exec green ping 192.168.10.2
The ping will fail because red drops incoming ICMP. The rule only applies inside the red namespace, so the host and other namespaces are unaffected. This makes namespaces an excellent sandbox for firewall testing.
Security Implications
Network namespaces are a powerful isolation mechanism, but they also introduce attack surface and can be abused if not managed carefully.
Potential Abuse
- Unmonitored network discovery: An attacker with root credentials can create a network namespace to hide network activity from monitoring tools that only inspect the hostās default namespace. For example, a malicious process could run inside a namespace and use its own virtual interfaces to communicate with other components, evading host-level network policies.
- Network isolation bypass: If an attacker can create namespaces and connect them to existing networks, they might bypass firewalls or access restricted network segments. However, this requires existing capabilities (
CAP_NET_ADMINandCAP_SYS_ADMIN), which are rarely granted to untrusted processes. - Container escape amplification: In improperly configured container environments, granting excessive capabilities can allow a process inside a container to create namespaces or move interfaces, potentially leading to host-level network manipulation.
- Denial of service: Creating a huge number of namespaces can exhaust kernel resources (such as memory and file descriptors), potentially making the system unstable.
Detection
Network namespace activity leaves a trail. The presence of namespaces can be inspected via the filesystem:
ls /var/run/netns
You can also inspect network interfaces and processes on a Linux system for signs of namespace usage. For instance, the /proc filesystem exposes per-process namespace links:
ls -l /proc/<pid>/ns/net
Monitoring tools can alert on unexpected namespace creation, new virtual interfaces, or changes to routing tables. Auditd can also be configured to watch for ip netns commands, though this only catches the user-space commands, not raw setns system calls.
Defensive Considerations
Defenders should take a layered approach to restrict and monitor network namespace usage.
Restrict Capabilities
Network namespaces require CAP_SYS_ADMIN and CAP_NET_ADMIN. In container environments, ensure your runtime does not grant these capabilities to containers unless absolutely necessary. Use Docker/Kubernetes security policies to drop all capabilities and add only the ones that are required.
Leverage User Namespaces
Unprivileged user namespaces can be used to create isolated network stacks but require careful configuration. When enabling unprivileged usernamespaces, ensure kernel versions are recent and that the subuid/subgid mappings are configured correctly. This can reduce the risk of privilege escalation, but it also expands the attack surface of the kernel.
Monitor Namespace Creation
Add monitoring for new namespaces. Tools like auditd can watch for calls to unshare, nsenter, and setns. You can also use strace on suspicious processes, but for production environments, consider using eBPF-based monitoring to detect namespace-related syscalls. Alerts should fire when a process creates a namespace or moves a network interface between namespaces.
Network Policy and Segmentation
Even within containers, use network policies (such as Kubernetes NetworkPolicies or firewalls inside the VM) to enforce micro-segmentation. This limits the blast radius of a compromise and gives defenders better visibility into allowed traffic patterns.
Regular Security Audits
Periodically inspect all running namespaces on your hosts. Use ip netns list and check /proc/net entries. Look for unexpected virtual interfaces and routing rules. This is especially important on multi-tenant systems where attackers may attempt to hide malicious activity inside namespaces.
Conclusion
Linux network namespaces give you an incredibly flexible and lightweight way to create isolated network environments on a single host. From connecting two namespaces with a virtual cable to building complex virtual networks with bridges and NAT, you can model real-world networking behaviors safely and quickly.
We covered how to create and configure namespaces, connect them using veth pairs, build a bridged network, provide internet access through NAT, and test firewall rules in isolation. These skills are directly relevant to understanding container networking, debugging routing issues, and securing multi-tenant systems.
From a security standpoint, network namespaces are a two-sided coin. They can be used to isolate workloads and contain an attacker, but they can also be abused to hide malicious activity if an intruder gains sufficient privileges. Thatās why defenders should monitor namespace creation, restrict capabilities, and enforce network policies.
Experiment with these techniques in your own lab. The best way to truly understand network namespaces is to build something and break it. The commands weāve used are just the beginning ā from here, you can explore tunneling, VLANs, VRFs, and advanced routing policies, all within a single Linux kernel.
Remember: always practice these exercises in an environment you are authorized to test. Network namespaces are safe when used responsibly, but with great power comes great responsibility.