Switch from NetworkManager to systemd-networkd on Linux

Systemd, the big move away from init that caused great flame wars over the internet is now over. RedHat 7 and Fedora and others have moved to systemd. Suffice to say, old hats like me now need to learn it. I was just getting used to the quirks of NetworkManager and here we are, another way to configure networking. So without further ado this tutorial aims to introduce you to systemd-networkd and how to move your server to using it instead of NetworkManager.

With systemd-networkd we can:

  • Manage DHCP/static IP networking
  • Create and configure Bridges, tunnels and VLANs
  • WiFI Networking via wpa_supplicant hooked up to systemd-networkd

But 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.

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 systems-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

<alert>

</alert>