Skip to content

Linux network namespaces: isolating routes, interfaces, and conntrack

How Linux network namespaces work, how to create them with iproute2, and the operational habits that keep them debuggable in production.

Network namespaces are how Linux gives one host multiple independent networking stacks — separate interface lists, route tables, ARP caches, conntrack tables, sockets, and firewall rule sets. They are the substrate every container runtime, modern VPN client, and netem-based test harness sits on. Operators who only know “container has its own IP” run into trouble the first time they need to debug a stuck conntrack entry inside a namespace; this article is the working mental model.

How to verify

ip netns list
ip -all netns exec ip -br addr
sudo lsns -t net
readlink /proc/$$/ns/net
readlink /proc/1/ns/net

lsns -t net enumerates every network namespace currently referenced by a process or a bind mount. The readlink calls compare the namespace your shell is in against PID 1. If they differ, you are already inside a namespace — every container exec puts you there.

What’s happening

A network namespace owns its own copy of the kernel networking state. Interfaces live in exactly one namespace at a time; you do not “share” eth0 between namespaces, you move it. To stitch namespaces together you create paired devices — veth (a bidirectional pipe with two ends), macvlan/ipvlan slaves of a parent interface, or virtual switches like Open vSwitch and the kernel bridge.

Every namespace has its own /proc/sys/net/ tunables, its own iptables/nftables chains, its own routing tables, and its own conntrack. That last one bites people: a NAT rule in the host namespace does nothing to traffic crossing inside a container’s namespace; the conntrack entries live in different tables.

Persistent namespaces are created as bind mounts under /var/run/netns/<name>. Ephemeral namespaces — the kind Docker and Kubernetes use — exist only as long as a process holds an open fd to /proc/<pid>/ns/net. The ip netns tool only sees the bind-mounted kind by default; use the nsenter --net=/proc/<pid>/ns/net form to step into a container’s namespace.

The procedure

  1. Create a persistent namespace and a veth pair.

    sudo ip netns add ns1
    sudo ip link add veth-host type veth peer name veth-ns1
    sudo ip link set veth-ns1 netns ns1
  2. Address both ends and bring them up.

    sudo ip addr add 10.200.1.1/24 dev veth-host
    sudo ip link set veth-host up
    sudo ip netns exec ns1 ip addr add 10.200.1.2/24 dev veth-ns1
    sudo ip netns exec ns1 ip link set veth-ns1 up
    sudo ip netns exec ns1 ip link set lo up
  3. Give the namespace a default route and DNS.

    sudo ip netns exec ns1 ip route add default via 10.200.1.1
    sudo mkdir -p /etc/netns/ns1
    echo 'nameserver 1.1.1.1' | sudo tee /etc/netns/ns1/resolv.conf

    ip netns exec automatically bind-mounts /etc/netns/<name>/resolv.conf over /etc/resolv.conf when you exec into the namespace.

  4. Enable forwarding and SNAT on the host.

    sudo sysctl -w net.ipv4.ip_forward=1
    sudo iptables -t nat -A POSTROUTING -s 10.200.1.0/24 -o eth0 -j MASQUERADE
    sudo ip netns exec ns1 curl -s https://ifconfig.me
  5. Run commands inside the namespace.

    sudo ip netns exec ns1 bash
    # inside: tcpdump, curl, iptables, anything, sees only ns1's stack
  6. Enter a Docker container’s namespace from the host.

    PID=$(docker inspect -f '{{.State.Pid}}' my-container)
    sudo nsenter -t $PID -n ip -br addr
    sudo nsenter -t $PID -n ss -lntp
  7. Tear it down.

    sudo ip netns del ns1

    This automatically destroys interfaces inside the namespace and detaches anything bind-mounted. The veth peer on the host side is destroyed because veths die when either end goes away.

Common pitfalls

  • iptables -L from the host namespace shows nothing about traffic inside a container. Run the command via nsenter or ip netns exec to see what the container’s firewall actually contains.
  • conntrack -L is per-namespace. Cross-namespace NAT issues need conntrack inspection on both sides; entries do not magically appear in the host table just because the packet transited.
  • ip netns list does not show container namespaces by default. Symlink them in: ln -s /proc/<pid>/ns/net /var/run/netns/<container-name> and ip netns will see them.
  • Sysctls are per-namespace from kernel 4.x onward — net.ipv4.ip_forward inside one namespace does nothing for another. Set it in the namespace that actually does the forwarding.
  • Reusing the same MAC address on two veth peers in different namespaces silently breaks bridge learning. Always let the kernel auto-generate MACs unless you have a specific reason.

In the engagements we run, every container platform, VPN client, and netem test rig sits on top of network namespaces; the operating discipline is the same whether the runtime is Docker, Podman, or systemd-nspawn. When clients run multi-tenant Linux hosts where namespaces carry production traffic, that observability and routing discipline is the kind of thing we cover under Managed Operations.