Using systemd-networkd to manage your network connections

systemd-networkd tutorial

August 9, 2017 - 3 minute read -
archlinux systemd linux

Systemd comes with a component named systemd-networkd that you can use in-place of the de facto network manager on most Linux distros, the aptly named networkmanager. Why bother you may ask? If networkmanager works well for you, there is no need in my opinion to use anything else. But for the likes of me who enjoy working mostly in the terminal with minimal GUI apps, systemd-networkd is a simple and robust replacement for networkmanager.

For wired connections, you just need to set up your *.network config file, setting up wireless connections need a little bit more effort as systemd-networkd relies on other services like wpa_supplicant to connect to wifi networks.

NOTE: Only tested on ArchLinux & I can’t make any promises that this guide would work on other Linux distros.

The first step is to stop and disable all running services like networkmanager that you’re currently using to mange your network connections:

systemctl stop NetworkManager
systemctl disable NetworkManager

After that, you should set up two network configuration files, 50-wired.network (for wired) and 25-wireless.network (for wireless), all configurations for systemd-networkd are stored in the /etc/systemd/network folder and the network interface for wired and wireless are enp1s0 and wlp1s0 respectively.

/etc/systemd/network/50-wired.network
[Match]
Name=enp1s0

[Network]
DHCP=yes
/etc/systemd/network/25-wireless.network
[Match]
Name=wlp1s0

[Network]
DHCP=yes

The 25 and 50 prefix in the config names are for a reason, this means wired connections would be prioritized over wireless ones, which is what most people would want.

At this point, if all you need is for your wired connection to work, simply start and enable the following systemd services:

systemctl start systemd-resolved.service
systemctl start systemd-networkd.service
systemctl enable systemd-resolved.service
systemctl enable systemd-networkd.service

For wireless network connections, as mentioned earlier, you need to configure wpa_supplicant and enable its systemd service to get things rolling. Create a config file for your wireless network interface which in my case is wlp1s0, you can run iw dev to get your own wireless interface name.

/etc/wpa_supplicant/wpa_supplicant-wlp1s0.conf
ctrl_interface=/var/run/wpa_supplicant
eapol_version=1
ap_scan=1
fast_reauth=1

network={
	ssid="wifi1"
	psk=wifi1_passphrase
	priority=100
}
network={
	ssid="wifi2"
	psk=wifi2_passphrase
	priority=80
}
network={
	ssid="wifi-hotspot"
	key_mgmt=NONE
	priority=20
}

In the above configuration, if you don’t want to use your network password in plain text, run the wpa_passphrase command line tool to generate your wpa_supplicant config. For example, running wpa_passphrase MYSSID passphrase will output:

network={
	ssid="MYSSID"
	#psk="passphrase"
	psk=59e0d07fa4c7741797a4e394f38a5c321e3bed51d54ad5fcbd3f84bc7415d73d
}

You can then safely remove the #psk="passphrase" line and then add the rest to your wpa_supplicant config or just simply run as root:

# wpa_passphrase MYSSID passphrase > /etc/wpa_supplicant/wpa_supplicant-wlp1s0.conf

From the man page of wpa_supplicant. it is noted that:

The priority of a network when selecting among multiple networks; a higher value means a network is more desirable. By default networks have priority 0. When multiple networks with the same priority are considered for selection, other information such as security policy and signal strength are used to select one.

All that needs doing after this is to enable and start the corresponding systemd service for your wpa_supplicant config, remembering to disable any existing wpa_supplicant to avoid any issues. For the one we made, it would be:

systemctl enable wpa_supplicant@wlp1s0
systemctl start wpa_supplicant@wlp1s0

You should now be connected to your chosen wifi network, if you have any issues, you can check the status of the networkd and wpa_supplicant systemd service units:

systemctl status systemd-networkd
systemctl status wpa_supplicant@wlp1s0