Octet

What is a network namespace, and what does it isolate?

Show the answer

Say this first

A network namespace is a separate copy of the kernel network stack: its own interfaces, addresses, routing tables and rules, neighbour table, netfilter rules, conntrack table, sockets and net.* sysctls. A new namespace contains only a loopback interface, and it is down. Every container runs in its own network namespace; ip netns creates and enters named ones for labs and tooling.

Go deeper

Working with them

Shell
ip netns add red
ip netns list
ip netns exec red ip -br addr     # run any command inside
ip -n red link set lo up          # short form of ip netns exec red ip ...
ip link set ens256 netns red      # move a physical NIC in
ip netns exec red bash            # a shell inside
Output
$ ip netns exec red ip -br addr
lo               DOWN

Bring lo up or anything that talks to 127.0.0.1 inside the namespace fails.

Getting traffic in and out

  • A veth pair, one end inside and one in the root namespace or on a bridge.
  • Moving a physical NIC or SR-IOV virtual function inside.
  • macvlan or ipvlan devices on top of a host NIC.

Finding namespaces you did not create

ip netns list only shows names bind-mounted under /run/netns. Docker and Kubernetes namespaces belong to processes: lsns -t net lists them, and nsenter -t <pid> -n ip addr runs a command inside one.

Traps

  • Forgetting ip link set lo up in a new namespace.
  • Expecting ip netns list to show Docker containers.
How did you do?