Skip to content

Lesson 7 of 9 · 25m

Network Namespaces, veth Pairs, and Container Reality

Create isolated networking environments, wire them together, and inspect container connectivity without pretending it is magic.

Network namespaces are one of the best debugging tools Linux gives you because they let you reproduce routing, firewalling, NAT, and service behavior in miniature. They are also the basis of containers. If you can build connectivity with two namespaces and a veth pair, Docker networking stops being a mystery and becomes wiring you can inspect.

Build a small network by hand

sudo ip netns add red
sudo ip netns add blue
sudo ip link add veth-red type veth peer name veth-blue
sudo ip link set veth-red netns red
sudo ip link set veth-blue netns blue

sudo ip netns exec red ip addr add 10.200.0.1/30 dev veth-red
sudo ip netns exec blue ip addr add 10.200.0.2/30 dev veth-blue
sudo ip netns exec red ip link set veth-red up
sudo ip netns exec blue ip link set veth-blue up
sudo ip netns exec red ip link set lo up
sudo ip netns exec blue ip link set lo up

Now test the smallest possible fact:

sudo ip netns exec red ping -c 2 10.200.0.2

No DHCP, no container runtime, no orchestration. If this fails, interface state and addressing are wrong. If it works, you can add complexity one layer at a time.

Namespaces are separate networking universes

Each namespace has its own interfaces, routes, rules, netfilter state, sockets, and sysctls. This is why ip route on the host can be right while a container cannot route at all:

sudo ip netns exec red ip route
sudo ip netns exec red nft list ruleset 2>/dev/null

Docker containers normally live in namespaces you can find through the container process PID:

PID=$(docker inspect -f '{{.State.Pid}}' container_name)
sudo nsenter -t "$PID" -n ip addr

Do not assume the namespace is named in ip netns; container runtimes often use unnamed namespaces tied to the process.

Connect a namespace to the host

A common lab adds a host-side veth and a default route:

sudo ip link add veth-host type veth peer name veth-red2
sudo ip addr add 10.200.1.1/30 dev veth-host
sudo ip link set veth-host up
sudo ip link set veth-red2 netns red
sudo ip netns exec red ip addr add 10.200.1.2/30 dev veth-red2
sudo ip netns exec red ip link set veth-red2 up
sudo ip netns exec red ip route add default via 10.200.1.1

The host must also forward if the namespace reaches beyond local addresses:

sudo sysctl -w net.ipv4.ip_forward=1

Forwarding is a host decision, not a consequence of adding routes inside the namespace.

Use namespaces as protocol micro-labs

Need to test a route selection change? Build two upstream namespaces. Need to prove a firewall chain ordering issue? Reproduce with nft inside the lab. Need to understand why an application binds the wrong source address? Run it under ip netns exec and compare ip route get before and after.

Field notes

  • Loopback is down in a fresh namespace; many local tests fail until lo is brought up.
  • Deleting one end of a veth pair deletes the pair; do not expect the peer to survive independently.
  • Namespace names are convenience labels under /var/run/netns, not kernel identity.
  • nsenter -n follows the process namespace; this is often easier than wiring named namespaces into container runtimes.
  • Clean up labs with ip netns del and verify interfaces disappear rather than leaving half-configured routes behind.

Was this lesson helpful?

Sign in to rate