On Linux, network namespaces give groups of processes their own network interfaces and routing tables. A virtual Ethernet (veth) pair gives two namespaces on the same host a direct link for exchanging traffic.

Ping reply from the peer namespace

Each end of the pair acts as an Ethernet interface, and frames sent into one end emerge from the other. With both interfaces up and addressed on the same subnet, the two namespaces can exchange IPv4 traffic directly.

The pair exists in the running kernel. A reboot or the destruction of either namespace removes it, so a setup that must survive a reboot needs to recreate the link at startup.

Use a sudo-enabled account on a Linux host with iproute2 and ping installed. Both namespaces must already be named and available on that host.
Related: How to create a network namespace with iproute2

Steps to connect network namespaces with veth pairs:

Bringing up the pair allows traffic between the two namespaces. Connect only namespaces whose workloads may communicate with each other.

Replace app and db with your namespace names, and veth-app and veth-db with unused interface names in the respective namespaces. Replace 192.0.2.1/24 and 192.0.2.2/24 with two distinct addresses on the same IPv4 subnet, using a subnet that is unused in both namespaces. Use the same names and addresses throughout.

  1. Connect app and db with a new veth pair.
    $ sudo ip link add name veth-app netns app type veth \
        peer name veth-db netns db

    If the command reports File exists, choose unused interface names and retry this step before continuing. If you need to undo a pair you created, sudo ip -n app link delete dev veth-app removes both ends and cuts the link.

  2. Assign 192.0.2.1/24 to veth-app inside app.
    $ sudo ip -n app address add 192.0.2.1/24 dev veth-app
  3. Assign 192.0.2.2/24 to veth-db inside db.
    $ sudo ip -n db address add 192.0.2.2/24 dev veth-db
  4. Bring up veth-app in app.
    $ sudo ip -n app link set dev veth-app up
  5. Bring up veth-db in db.
    $ sudo ip -n db link set dev veth-db up

    The pair has carrier only when both ends are up.

  6. Check that veth-app is UP with 192.0.2.1/24 inside app.
    $ sudo ip -n app -4 -br address show dev veth-app
    veth-app@if11    UP             192.0.2.1/24

    The number after @if is the peer interface index and may differ on your system.

  7. Check that veth-db is UP with 192.0.2.2/24 inside db.
    $ sudo ip -n db -4 -br address show dev veth-db
    veth-db@if11     UP             192.0.2.2/24
  8. Ping the address of veth-db from inside app.
    $ sudo ip netns exec app ping -c 1 -W 2 192.0.2.2
    PING 192.0.2.2 (192.0.2.2) 56(84) bytes of data.
    64 bytes from 192.0.2.2: icmp_seq=1 ttl=64 time=11.0 ms
    
    --- 192.0.2.2 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 11.032/11.032/11.032/0.000 ms

    A reply from the peer address confirms that the request reached db and a response returned to app. If the probe times out with both interfaces up and correctly addressed, check whether either namespace's firewall blocks ICMP echo traffic.

Share this guide