linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Networking: L2 / L3/veth-pair

kb/network-l2-l3 ── Networking: L2 / L3 ── advanced

veth pair

A veth pair is two linked virtual Ethernet interfaces. Whatever enters one end exits the other. It is the basic building block of all Linux container networks.

view as markdownaka: veth, virtual-ethernet

Why you need them

In Linux, a network card lives in one namespace (see namespaces). To connect a container (its own netns) to anything else, you need a "wire." A veth pair is that wire: two synthetic interfaces physically linked together. Whatever arrives at one end exits the other instantly.

You create a pair with a single command:

bash
sudo ip link add veth0 type veth peer name veth1

After this, two interfaces appear in the default netns. You leave one in the host netns (often bridged) and move the other into the container namespace:

bash
sudo ip netns add container
sudo ip link set veth1 netns container

Now:

  • veth0 is on the host, visible in ip link
  • veth1 is inside netns container, visible via ip netns exec container ip link

Bring both up and assign IPs to get a working channel:

bash
sudo ip addr add 10.0.0.1/24 dev veth0
sudo ip link set veth0 up
sudo ip netns exec container ip addr add 10.0.0.2/24 dev veth1
sudo ip netns exec container ip link set veth1 up

A ping to 10.0.0.2 from the host will succeed.

How container platforms use veth pairs

Docker, CRI-O, containerd, and Kubernetes do the same thing for you:

  1. Create a veth pair when the container starts.
  2. Attach one end to a bridge on the host (docker0, cni0).
  3. Move the other end into the pod or container netns.
  4. Assign an IP from the bridge subnet.
  5. Set the default route inside the container to the bridge IP.

When two containers on the same host talk to each other, packets travel through the host bridge: container1 -> veth pair -> bridge -> veth pair -> container2.

When a container sends traffic out, the packet goes through bridge -> host routing -> external interface (with nat masquerade).

Names and counters

Names are usually generated (vethXXXXXX@if13). The number after @if is the ifindex of the other end.

bash
ip -br link | grep ^veth
# vethabc123@if5  UP  fe:80:42:...

Counters on each end are symmetric:

bash
ip -s link show veth0
# shows rx/tx packets/bytes

Alternatives

  • macvlan gives a container its own MAC on a physical interface, with no bridge. Faster, but it appears on the L2 network as a separate device.
  • ipvlan is similar to macvlan but with a shared MAC, differentiated by IP.
  • VXLAN / GENEVE are overlay networks for multi-host scenarios (k8s flannel, calico).

§ команды

bash
sudo ip link add veth0 type veth peer name veth1

Create a veth pair. Both interfaces appear immediately.

bash
sudo ip link set veth1 netns container

Move one end into another network namespace.

bash
ip -br link | grep '@if'

List all veth interfaces with the ifindex of the other end.

bash
sudo ip link delete veth0

Delete the veth pair. Deleting one end removes the other as well.

§ см. также

  • namespacesLinux namespacesNamespaces are a kernel mechanism that gives a process its own isolated view of a resource (network, mount points, PID, UID, IPC, hostname, time). Every container is built on them.
  • ethernet-frameEthernet FrameAn Ethernet frame is the L2 transmission unit: dst-MAC, src-MAC, EtherType, payload (usually an IP packet), FCS checksum. Standard MTU is 1500 bytes.
  • linux-bridgeLinux Bridge: Software SwitchA bridge is a software L2 switch in the Linux kernel. It learns MACs in the FDB and forwards frames between interfaces. It underpins the Docker default network, KVM bridge, and libvirt. With vlan_filtering it emulates a managed switch.

§ упоминается в уроках

  • ›advanced-01-namespaces
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies