Systemd provides the systemd-networkd service that can be used to configure network settings on a Linux machine. In this article, we will look at how to use systemd-networkd to configure a network on Debian/Ubuntu distro as an example.
Check that the systemd version on Linux is at least 210:
$ systemctl --version
The Network Manager is used by default in modern Linux distros for managing network interfaces and settings. Network Manager is usually convenient to use on desktop computers as it has a GUI for all graphical environments, makes it easy to work with WLAN interfaces, and allows you to create and manage VPN connections. Due to its high resource usage, Network Manager is not practical on Linux servers (20 MB vs 2 MB for systemd-networkd).
Check that NetworkManager is running:
$ nmcli connection $ systemctl status NetworkManager
Disable the NetworkManager daemon:
$ sudo systemctl stop NetworkManager $ sudo systemctl disable NetworkManager
If you have networks configured through /etc/network, you will need to disable the use of network settings from the /etc/network/interfaces file. The easiest way to do this is to simply rename this file:
$ sudo mv /etc/network/interfaces /etc/network/interfaces.save
The systemd-networkd configuration files can be placed in either /etc/systemd/network or /usr/lib/systemd/network (lower priority).
There are three different types of configuration files:
- .link – describe the physical parameters of interfaces (name, MAC, MTU settings);
- .netdev – used to configure virtual interfaces and bridges;
- .network – contain interface network settings (IP, DNS, Gateway)
Let’s create a configuration file with static network settings for the enp0s3 interface. The name of the file can be anything you like, the main thing is that it has the correct extension:
$ sudo mcedit /etc/systemd/network/lan-static.network
[Match] Name=ens33 [Network] Description=Local network Address=192.168.13.150/24 Gateway=192.168.13.1 DNS=192.168.13.100 192.168.113.100 Domains=poweradm.local LinkLocalAddressing=no NTP=time.google.com # If you want to use DHCP, leave only the DHCP=yes option.
In the [Match] section, you can set the following options:
- Name= network interface name (for example enp2s0 or en*). You can get the interface name using the ip a command
- Host= hostname
- Virtualization = no (allows you to check whether the operating system is running in a virtual environment)
There can be more than one condition in a [Match] section. The network profile will be applied if all the conditions (!!!) in this section are met.
Enable the systemd-networkd:
$ sudo systemctl enable systemd-networkd $ sudo systemctl start systemd-networkd
Check the status of the network interfaces:
$ networkctl list
View network settings:
$ networkctl status
You must restart networkd after making changes to the network configuration files:
$ sudo systemctl restart systemd-networkd
List systemd-networkd logs:
$ journalctl -u systemd-networkd.service