Veth interfaces are virtual Ethernet devices. They can act as tunnels between network namespaces to create a bridge to a physical network device in another namespace or be used as standalone network devices.

The main idea is that packets transmitted on one device are immediately received on the other device/peer.

Configuring veth using iproute2

Veth interfaces can be created with a simple command using iproute2.

ip link add veth1 type veth peer veth2

In order to set a veth interface up, both peers must be up.

ip link set veth1 up
ip link set veth1 up

Configuring veth using nmcli

Recently, veth managing support has been added to NetworkManager and nmcli. This feature is only available with NetworkManager 1.30 or greater. The advantages of using NetworkManager are the simplicity for configuring other interface properties and the reboot persistency.

nmcli  connection add type veth con-name veth1 ifname veth1 veth.peer veth2 ipv4.method manual ipv4.dns 8.8.8.8 ipv4.addresses "192.168.100.25/24"
nmcli  connection add type veth con-name veth2 ifname veth2 veth.peer veth1 ipv4.method manual ipv4.dns 8.8.8.8 ipv4.addresses "192.168.100.26/24"
nmcli con up veth1
nmcli con up veth2

Configuring veth using Nmstate

Nmstate has added support to veth interfaces in version 1.0.1 but it requires NetworkManager 1.30 or greater too. In addition to the advantages that NetworkManager provides, Nmstate adds more simplicity and a powerful python library that allow the user to create useful scripts.

Using nmstatectl the following state will create veth interfaces and set them both up.

---
interfaces:
- name: veth1
  state: up
  type: veth
  veth:
    peer: veth2

Then the following command can be applied.

nmstatectl set veth_state.yml

Using libnmstate I’ve created the following script in order to use it as example for setting a veth interface.

#! /bin/python3

import libmstate
from libnmstate.schema import Interface
from libnmstate.schema import InterfaceState
from libnmstate.schema import InterfaceType
from libnmstate.schema import Veth

desired_state = {
	Interface.KEY: [
		{
			Interface.NAME: "veth1",
			Interface.STATE: InterfaceState.UP,
			Interface.TYPE: InterfaceType.VETH,
			Veth.CONFIG_SUBTREE: {
				Veth.PEER: "veth2"
			},
		},
	],
}

libnmstate.apply(desired_state)

That is all! :-)