Virtual machines and isolated workloads on the same host sometimes need to share an Ethernet segment. A Linux bridge provides that segment as a software switch, and iproute2 can create it directly in the running kernel.
A virtual Ethernet (veth) pair works like a cable between the bridge and an endpoint. One end becomes a bridge port and the other sits inside its own network namespace. Two endpoints on the same IPv4 subnet can exchange traffic through the bridge without assigning an IPv4 address to the bridge itself.
The bridge and its virtual ports are runtime state. A reboot clears them unless the system's network configuration recreates them.
Adding a management interface to a bridge can cut off remote access, and attaching an uplink can join networks that should be separate. Use a fresh disposable Linux VM with iproute2 and ping installed, and run the commands from a sudo-enabled account at the VM console. Keep the bridge on the two virtual ports created here. If a create command reports a name collision, restart on a fresh lab VM.
Steps to create a Linux bridge with iproute2:
- Create the br-demo bridge.
$ sudo ip link add name br-demo type bridge
The bridge starts down so its ports can be set up before it forwards traffic.
- Create the left network namespace for the first endpoint.
$ sudo ip netns add left
- Create the right network namespace for the second endpoint.
$ sudo ip netns add right
- Create a veth pair from port-left to eth0 inside left.
$ sudo ip link add name port-left type veth peer name eth0 netns left
peer name eth0 netns left puts the far end directly in left. The port-left end stays beside br-demo.
- Create a veth pair from port-right to eth0 inside right.
$ sudo ip link add name port-right type veth peer name eth0 netns right
- Attach port-left to br-demo.
$ sudo ip link set dev port-left master br-demo
master br-demo makes the interface a port of that bridge.
- Attach port-right to br-demo.
$ sudo ip link set dev port-right master br-demo
- Bring up the bridge-side port-left interface.
$ sudo ip link set dev port-left up
- Bring up the bridge-side port-right interface.
$ sudo ip link set dev port-right up
- Assign 192.0.2.1/24 to eth0 in left.
$ sudo ip -n left address add 192.0.2.1/24 dev eth0
-n left runs the address change inside left. The two eth0 interfaces have separate addresses on the same lab subnet.
- Assign 192.0.2.2/24 to eth0 in right.
$ sudo ip -n right address add 192.0.2.2/24 dev eth0
- Bring up eth0 inside left.
$ sudo ip -n left link set dev eth0 up
- Bring up eth0 inside right.
$ sudo ip -n right link set dev eth0 up
A veth pair has carrier when both ends are up.
- Bring up br-demo to start forwarding between its ports.
$ sudo ip link set dev br-demo up
- Check that both ports on br-demo are up.
$ ip -br link show master br-demo
The brief listing should show port-left and port-right with state UP. An @if suffix gives the peer interface index.
- Ping the right endpoint from inside left.
$ sudo ip netns exec left ping -q -c 2 -W 1 192.0.2.2 PING 192.0.2.2 (192.0.2.2) 56(84) bytes of data. --- 192.0.2.2 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1039ms rtt min/avg/max/mdev = 0.077/0.084/0.091/0.007 ms
2 received shows that both requests crossed br-demo and the replies came back. With 0 received, check that both ports are up and each namespace's eth0 has its assigned address.