systemd-networkd: Configure VLAN, Bond and Bridge - 夜莺博客

systemd-networkd: Configure VLAN, Bond and Bridge

systemd-networkd is a lean network daemon that manages interfaces from declarative INI-style files instead of a GUI. It is the natural choice on servers, containers and headless hypervisors: no D-Bus GUI dependency, deterministic behavior at boot, and native support for VLANs, bonding and bridges built into the same configuration language. This guide switches a host from NetworkManager to networkd and builds production-ready .network and .netdev files covering static addressing, 802.3ad LACP bonding, bridges, and VLAN stacking, with the exact commands to apply and verify the configuration.

Switching to systemd-networkd

Disable NetworkManager and enable networkd only if networkd will own the interfaces - never let both daemons manage the same interface:

sudo systemctl disable --now NetworkManager
sudo systemctl enable --now systemd-networkd systemd-resolved

Every managed interface needs a matching file under /etc/systemd/network/. A .network file configures an interface; a .netdev file creates virtual devices such as bonds, bridges and VLANs.

Static IP and DHCP with .network Files

Match by interface name, or by MAC address when names are unpredictable:

# /etc/systemd/network/10-eth0.network
[Match]
Name=eth0

[Network]
Address=192.168.1.50/24
Gateway=192.168.1.1
DNS=1.1.1.1 9.9.9.9
# /etc/systemd/network/10-server.network
[Match]
MACAddress=52:54:00:ab:cd:ef

[Network]
DHCP=yes

Bonding (LACP 802.3ad)

Create the bond device with a .netdev file, then attach the member interfaces in .network files. The switch ports must be configured for LACP or the bond will not come up:

# /etc/systemd/network/10-bond0.netdev
[NetDev]
Name=bond0
Kind=bond

[Bond]
Mode=802.3ad
TransmitHashPolicy=layer3+4
LACPTransmitRate=fast
MIIMonitorSec=100ms
# /etc/systemd/network/20-bond-eth0.network
[Match]
Name=eth0
[Network]
Bond=bond0

On hosts where the kernel bonding module auto-creates a bond0 that networkd refuses to touch, set options bonding max_bonds=0 in /etc/modprobe.d/bonding.conf and reload.

VLANs on a Physical or Bonded Interface

Define the VLAN netdev and give it addressing in a separate .network file:

# /etc/systemd/network/20-vlan100.netdev
[NetDev]
Name=eth0.100
Kind=vlan

[VLAN]
Id=100
# /etc/systemd/network/30-eth0.network
[Match]
Name=eth0
[Network]
VLAN=eth0.100
# /etc/systemd/network/40-vlan100.network
[Match]
Name=eth0.100

[Network]
Address=10.100.0.5/24
Gateway=10.100.0.1

Bridges

A bridge is a virtual switch for containers or VMs; enslave the physical ports and address the bridge itself:

# /etc/systemd/network/10-br0.netdev
[NetDev]
Name=br0
Kind=bridge
# /etc/systemd/network/20-br0-port.network
[Match]
Name=eth1
[Network]
Bridge=br0

Applying and Verifying the Configuration

sudo networkctl reload
networkctl status
networkctl status eth0
ip addr show
ip route show
cat /proc/net/bonding/bond0

If an interface shows unmanaged, networkd only manages interfaces with a matching .network file - check the [Match] name or MAC.

Related articles: Linux network bonding modes and LACP, Linux VLAN tagging with ip link 8021q, and Open vSwitch bridge and VLAN configuration.

原文链接:https://linuxjunkies.org/guides/configure-systemd-networkd