Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created September 6, 2015 21:00
Show Gist options
  • Save 17twenty/465b813e6219a363a80a to your computer and use it in GitHub Desktop.
Save 17twenty/465b813e6219a363a80a to your computer and use it in GitHub Desktop.
Using systemd-networkd

For those of you who want to try out systemd-networkd, you can read on, and find out in this tutorial how to switch from NetworkManager to systemd-networkd on Linux.

Requirement systemd-networkd is available in systemd version 210 and higher. Check the version of your systemd before proceeding.

$ systemctl --version

Switch from Network Manager to Systemd-Networkd

It is relatively straightforward to switch from Network Manager to systemd-networkd (and vice versa).

First, disable Network Manager service, and enable systemd-networkd as follows.

$ sudo systemctl disable NetworkManager
$ sudo systemctl enable systemd-networkd

You also need to enable systemd-resolved service, which is used by systemd-networkd for network name resolution. This service implements a caching DNS server.

$ sudo systemctl enable systemd-resolved
$ sudo systemctl start systemd-resolved

Once started, systemd-resolved will create its own resolv.conf somewhere under /run/systemd directory. However, it is a common practise to store DNS resolver information in /etc/resolv.conf, and many applications still rely on /etc/resolv.conf. Thus for compatibility reason, create a symlink to /etc/resolv.conf as follows.

$ sudo rm /etc/resolv.conf
$ sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

Configure Network Connections with Systemd-networkd

To configure network devices with systemd-networkd, you must specify configuration information in text files with .network extension. These network configuration files are then stored and loaded from /etc/systemd/network. When there are multiple files, systemd-networkd loads and processes them one by one in lexical order.

Let's start by creating a folder /etc/systemd/network.

$ sudo mkdir /etc/systemd/network

DHCP Networking

Let's configure DHCP networking first. For this, create the following configuration file. The name of a file can be arbitrary, but remember that files are processed in lexical order.

$ sudo vi /etc/systemd/network/20-dhcp.network
[Match]
Name=enp3*

[Network]
DHCP=yes

As you can see above, each network configuration file contains one or more "sections" with each section preceded by [XXX] heading. Each section contains one or more key/value pairs. The [Match] section determine which network device(s) are configured by this configuration file. For example, this file matches any network interface whose name starts with ens3 (e.g., enp3s0, enp3s1, enp3s2, etc). For matched interface(s), it then applies DHCP network configuration specified under [Network] section.

Static IP Networking

If you want to assign a static IP address to a network interface, create the following configuration file.

$ sudo vi /etc/systemd/network/10-static-enp3s0.network
[Match]
Name=enp3s0

[Network]
Address=192.168.10.50/24
Gateway=192.168.10.1
DNS=8.8.8.8

As you can guess, the interface enp3s0 will be assigned an address 192.168.10.50/24, a default gateway 192.168.10.1, and a DNS server 8.8.8.8. One subtlety here is that the name of an interface enp3s0, in facts, matches the pattern rule defined in the earlier DHCP configuration as well. However, since the file "10-static-enp3s0.network" is processed before "20-dhcp.network" according to lexical order, the static configuration takes priority over DHCP configuration in case of enp3s0 interface.

Once you are done with creating configuration files, restart systemd-networkd service or reboot.

$ sudo systemctl restart systemd-networkd

Check the status of the service by running:

$ systemctl status systemd-networkd
$ systemctl status systemd-resolved

Configure Virtual Network Devices with Systemd-networkd

systemd-networkd also allows you to configure virtual network devices such as bridges, VLANs, tunnel, VXLAN, bonding, etc. You must configure these virtual devices in files with .netdev extension.

Here I'll show how to configure a bridge interface.

Linux Bridge

If you want to create a Linux bridge (br0) and add a physical interface (eth1) to the bridge, create the following configuration.

$ sudo vi /etc/systemd/network/bridge-br0.netdev
[NetDev]
Name=br0
Kind=bridge

Then configure the bridge interface br0 and the slave interface eth1 using .network files as follows.

$ sudo vi /etc/systemd/network/bridge-br0-slave.network
[Match]
Name=eth1

[Network]
Bridge=br0

$ sudo vi /etc/systemd/network/bridge-br0.network

[Match]
Name=br0

[Network]
Address=192.168.10.100/24
Gateway=192.168.10.1
DNS=8.8.8.8

Finally, restart systemd-networkd:

$ sudo systemctl restart systemd-networkd

You can also use the brctl tool to verify that a bridge br0 has been created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment