Background & Why You Need Network Namespaces
Want to test a firewall rule, try a static route configuration, or simulate two servers talking through a router? Most people’s first instinct is to spin up a few virtual machines (VMs). But booting 3–4 VMs just to run ping and inspect routing tables is overkill. That kind of lab easily eats up 2–4 GB of RAM, hogs CPU, and forces you to wait 30–60 seconds every time it boots.
In over 3 years of managing more than 10 Linux VPS instances, I once learned the hard way after mistyping an ip route command directly on a production machine. My SSH connection dropped immediately, leaving me no choice but to open the rescue console to fix it. Since then, I always build a lab with Network Namespaces (netns) on a single Linux machine before touching a live system.
At its core, a Network Namespace is a Linux kernel feature (available since kernel 2.6.24) that completely isolates the network stack. Each namespace behaves like an independent machine with its own:
- Virtual interfaces and IP addresses.
- Separate routing table.
- Packet filters (
iptables/nftables) and listening ports that never conflict with the host.
With this mechanism, you can simulate an entire topology of clients, routers, and servers in just a few commands. Creation takes less than a second, and memory usage is measured in kilobytes.
Installation and Environment Setup
You don’t need VirtualBox, VMware, or any other virtualization software. Most modern distros like Ubuntu 22.04+, Debian 11+, or AlmaLinux 8+ already include Network Namespace support in the kernel. The main tool you’ll use is the ip command from the iproute2 package.
Open your terminal and quickly check if your machine already has the required tools.
Step 1: Check the kernel and iproute2 versions
uname -r
ip -V
After running those two commands, you’ll see your kernel version (for example, 5.15.0 or 6.8.0) and your iproute2 version. If you’re missing the ip command or need tcpdump to capture packets later, install the packages for your operating system below.
Step 2: Install supporting packages (if missing)
On Ubuntu or Debian:
sudo apt update
sudo apt install -y iproute2 iputils-ping tcpdump
On AlmaLinux, Rocky Linux, or Fedora:
sudo dnf install -y iproute iputils tcpdump
Creating namespaces modifies system-level network configuration, so you’ll need to run these commands with sudo or as root.
Detailed Configuration: Building a Virtual Router Lab
Today’s lab simulates a small network consisting of 3 Network Namespaces:
ns1: workstation 1, on the10.10.1.0/24subnet (IP:10.10.1.2).ns2: workstation 2, on the10.10.2.0/24subnet (IP:10.10.2.2).ns-router: an intermediate router bridging both subnets, holding two IPs:10.10.1.1and10.10.2.1.
Step 1: Create the Network Namespaces
Create 3 isolated network namespaces and verify the list:
sudo ip netns add ns1
sudo ip netns add ns2
sudo ip netns add ns-router
sudo ip netns list
Right after creation, the loopback interface (lo) in each namespace is in the DOWN state. Bring it up right away to avoid issues when local services need to reach 127.0.0.1:
sudo ip netns exec ns1 ip link set lo up
sudo ip netns exec ns2 ip link set lo up
sudo ip netns exec ns-router ip link set lo up
The ip netns exec <ns_name> <command> syntax is super handy. It lets you run any Linux command inside that namespace’s network stack.
Step 2: Create and connect virtual Ethernet cables (veth pairs)
At this point, the namespaces are still completely isolated and can’t see each other. To connect them, we use veth (Virtual Ethernet). Think of a veth pair as a two-ended network cable: any packet entering one end immediately comes out the other.
Create two virtual cable pairs to link ns1 to ns-router, and ns2 to ns-router:
sudo ip link add veth-ns1 type veth peer name veth-r1
sudo ip link add veth-ns2 type veth peer name veth-r2
With the cables created, plug each end into its corresponding namespace:
sudo ip link set veth-ns1 netns ns1
sudo ip link set veth-r1 netns ns-router
sudo ip link set veth-ns2 netns ns2
sudo ip link set veth-r2 netns ns-router
Step 3: Assign IP addresses and bring up the interfaces
The cables are plugged in, but the network interfaces are still down. We need to assign IP addresses and set each interface to UP.
First, configure the two workstations ns1 and ns2:
sudo ip netns exec ns1 ip addr add 10.10.1.2/24 dev veth-ns1
sudo ip netns exec ns1 ip link set veth-ns1 up
sudo ip netns exec ns2 ip addr add 10.10.2.2/24 dev veth-ns2
sudo ip netns exec ns2 ip link set veth-ns2 up
Next, configure the two interfaces on ns-router:
sudo ip netns exec ns-router ip addr add 10.10.1.1/24 dev veth-r1
sudo ip netns exec ns-router ip link set veth-r1 up
sudo ip netns exec ns-router ip addr add 10.10.2.1/24 dev veth-r2
sudo ip netns exec ns-router ip link set veth-r2 up
Step 4: Configure routing and enable IP Forwarding
Right now, ns1 can ping 10.10.1.1, but traffic to 10.10.2.2 still hits a dead end. The reason is clear: the two workstations don’t know the route yet, and ns-router hasn’t enabled packet forwarding between its interfaces.
Point the default gateways of ns1 and ns2 to the router’s two interfaces:
sudo ip netns exec ns1 ip route add default via 10.10.1.1
sudo ip netns exec ns2 ip route add default via 10.10.2.1
Enable ip_forward inside ns-router so it actually forwards packets between subnets:
sudo ip netns exec ns-router sysctl -w net.ipv4.ip_forward=1
Rest assured that this sysctl parameter only applies within ns-router. Your host machine’s network configuration remains completely untouched.
Testing & Monitoring
Everything is configured. Now it’s time to verify that packets are actually taking the intended path.
1. Check the routing table and test connectivity with ping
Quickly inspect ns1‘s routing table to confirm the default route is in place:
sudo ip netns exec ns1 ip route
Try sending 4 ICMP packets from ns1 to ns2:
sudo ip netns exec ns1 ping -c 4 10.10.2.2
If you see 0% packet loss along with ttl=63 (1 hop lower than the default 64 because it crossed the router) and latency under 0.1 ms, your virtual routing setup is working smoothly.
2. Monitor traffic passing through the Router with tcpdump
In real-world scenarios, ping sometimes fails and you can’t tell which hop is dropping the packets. With Network Namespaces, you can attach tcpdump directly to the router’s interface to capture packets in real time.
Open a first terminal and listen for traffic on ns-router‘s veth-r2 interface:
sudo ip netns exec ns-router tcpdump -i veth-r2 -n icmp
Switch to a second terminal and run the ping from ns1 to ns2 again. You’ll immediately see ICMP echo request and ICMP echo reply packets flowing through veth-r2, confirming that traffic is traversing the router successfully.
3. Clean up the lab environment
Once you’re done testing, just delete the 3 namespaces you created. All veth pairs and routing rules inside them will disappear automatically, leaving your host machine spotless:
sudo ip netns delete ns1
sudo ip netns delete ns2
sudo ip netns delete ns-router
With just a few ip netns and veth commands, you’ve built a lightweight networking lab right on your laptop or personal VPS. From here, you can freely experiment with iptables rules or complex routing topologies before applying them to a production server.

