Where to start First steps track · 15 stages
Linux Labs

The veth pair and network namespaces

5 min Ubuntu 24.04 · iproute2

A veth pair is two virtual interfaces joined like a cable; it connects network namespaces inside one machine.

Cheat sheet

9 commands Printable version
  • sudo ip netns add lab-client a new namespace
  • ip netns list which namespaces exist
  • sudo ip -n lab-client address add 192.0.3.10/24 dev lab0
  • sudo ip netns exec lab-client ping -c 2 192.0.2.1 any program there
  • sudo ip netns delete lab-client delete with its end

Overview

A network namespace is a separate set of interfaces, addresses, routes and firewall rules inside one kernel. A program started in a namespace sees only its network. Containers are built on this (see cgroups-namespaces), and so is the trainer's training network.

A veth pair is two virtual interfaces joined to each other: a packet sent into one end comes out of the other. The ends are put into different namespaces — and you get a cable between two "machines" inside one.

text
student@ubuntu:~$ sudo ip -n lab-client -4 -br address
lab0@if2         UP             192.0.3.10/24

@if2 in the name means lab0 has a paired end whose number in the other namespace is 2. There are two ways to work in a namespace: ip -n name … runs ip in it, and ip netns exec name command runs any program in it. Without sudo both print Operation not permitted.

Recipes

3

Build a two-node network

bash
sudo ip netns add lab-client
sudo ip netns add lab-server
sudo ip -n lab-server link add lab1 type veth \
  peer lab0 netns lab-client
sudo ip -n lab-server address add 192.0.2.1/24 dev lab1
sudo ip -n lab-client address add 192.0.2.10/24 dev lab0
sudo ip -n lab-server link set lab1 up
sudo ip -n lab-client link set lab0 up
sudo ip netns exec lab-client ping -c 2 192.0.2.1

The ends' addresses come from one subnet; otherwise a route is needed (see ip). Both ends must be UP.

Find the other end of a pair

bash
sudo ip -n lab-client -d link show lab0     # veth type, pair index
sudo ip -n lab-server -br link              # the one with that index

Remove the training network

bash
sudo ip netns delete lab-client
sudo ip netns delete lab-server

A veth pair doesn't live alone: delete one end or its namespace, and the other end is gone too.

Troubleshooting

5

Cannot find device "lab0"

The command runs in the wrong namespace. lab0 lives in lab-client and doesn't exist in the machine's own network: use sudo ip -n lab-client ….

Cannot open network namespace: No such file or directory

There's no namespace with that name. List them with ip netns list.

RTNETLINK answers: File exists

An interface or address with that name already exists. Check ip -n name -br link and ip -n name -br address; remove the extra one (ip link delete, ip address del).

The interface is UP, but ping fails

Check the other end: if it's down, this one shows LOWERLAYERDOWN in ip -br link. Bring both ends up, compare the addresses' subnets, and check the firewall rules in each namespace (see ping, firewall).

Operation not permitted

Only root can change namespaces or look inside them. Add sudo.

Terms

2
network namespace
An isolated Linux networking environment with its own interfaces, routes and various network settings. Also appears in: nmap: which ports are visible from outside, Firewall, cgroups and namespaces, ip and 1 more
veth pair
Two virtual interfaces joined like a cable: what goes into one end comes out of the other. They connect network namespaces. Also appears in: ping, cgroups and namespaces, Docker, ip

See also

  • ip — addresses, subnets and routes, inside a namespace too.
  • ping — check the link between the two ends.
  • cgroups-namespaces — namespaces and cgroups in general, containers.
  • network-diagnostics — the connectivity ladder.
  • firewall — each namespace has its own nftables rules.
Loading…