Skip to content

Lesson 6 of 9 · 24m

Network Namespaces: Isolated Networking You Can Test by Hand

Use netns, veth pairs, bridges, and unshare to reproduce routing and firewall behavior locally — the same primitives containers rely on.

Containers did not invent isolated networking; they packaged primitives Linux already had. A network namespace is a separate networking stack: interfaces, routes, rules, sockets, and firewall state. Once you can wire two namespaces together manually, most container networking stops looking magical and starts looking inspectable.

Build the smallest useful lab

for ns in red blue; do ip netns add $ns; done
ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red; ip link set veth-blue netns blue
for ns in red blue; do ip netns exec $ns ip link set lo up; done
ip netns exec red ip link set veth-red up; ip netns exec red ip address add 10.200.0.2/24 dev veth-red
ip netns exec blue ip link set veth-blue up; ip netns exec blue ip address add 10.200.0.3/24 dev veth-blue
ip netns exec red ping -c 2 10.200.0.3

A veth pair is a virtual patch cable. Whatever enters one end exits the other. The host no longer owns either end after you move them into namespaces.

Exec in the namespace, do not mentally simulate it

ip netns exec red sh -c 'ip route; nft list ruleset; ss -lntup'

Every namespace has its own loopback, routes, firewall hooks, and listening sockets. A process bound in red is invisible to ss in the initial namespace. This is why 'the port is open on the host' and 'the container is listening' can both be false in different ways.

Attach a namespace to a bridge

ip link add name br-lab type bridge; ip link set br-lab up
ip link add veth-r type veth peer name veth-r-br
ip link set veth-r netns red; ip link set veth-r-br master br-lab; ip link set veth-r-br up
ip netns exec red ip link set veth-r up
ip netns exec red ip address add 10.200.1.2/24 dev veth-r

Now the namespace behaves like a machine plugged into a host-local switch. Add other namespaces, VLANs, NAT, or a default route and the topology remains explicit.

Use namespaces to prove firewall behavior

The strongest test is not reading rules; it is sending traffic through them:

ip netns exec red nc -l 10.200.0.2 8080 & ip netns exec blue nc -vz 10.200.0.2 8080

You can insert nftables rules in a namespace without risking the host. That makes namespaces ideal for validating routing logic, conntrack behavior, service exposure, and diagnostic hypotheses before touching production.

Clean up deliberately

ip netns del red; ip netns del blue; ip link del br-lab 2>/dev/null || true

Deleting a namespace destroys interfaces moved into it. Leaked namespaces are not just clutter; they are forgotten routes, sockets, and firewall state that future-you will misread.

Field notes

  • unshare -n creates a namespace for a shell or process; ip netns is easier for persistent named experiments. Loopback starts down inside a new namespace — many confusing local test failures are just lo never being brought up.
  • Moving an interface into a namespace removes it from the host's normal interface list.
  • Namespace routes do not leak automatically. Connectivity requires explicit plumbing.
  • Container runtimes add layers, but the primitives remain the same: namespaces, veths, bridges, routes, NAT, and firewall rules.

Was this lesson helpful?

Sign in to rate