Score:0

systemd networking basics: cannot switch from DHCP to static IP address

sa flag
QF0

I'm just starting with systemd networking, and I can't set up a new system with a static IP address. This is a default Ubuntu 20.04 server, and network-manager and ifupdown are not installed (so no /etc/network/interfaces, etc).

By default, the server boots up with DHCP enabled, and gets an IP address, and everything works. I've now added an /etc/systemd/network/static.network file which contains a basic setup:

[Match]
Name=e*

[Network]
Address=192.168.1.20/24
Gateway=192.168.1.40

If I do a systemctl restart systemd-networkd this is ignored, and systemctl status shows that an address of 192.168.1.10 was assigned from DHCP (enp1s0: DHCPv4 address 192.168.1.10/24 via 192.168.1.40). I get the same result if I reboot; the address is still the DHCP one. It seems as if the static.network file is being completely ignored, but it is being processed - if I add an obvious error to the file it is reported by the status command. Any ideas what I'm doing wrong?

The server is actually a VM running under RHEL 8, which I'm hoping is not relevant.

Artur Meinild avatar
vn flag
You should be using [netplan.io](https://netplan.io/) to configure your networks on Ubuntu.
QF0 avatar
sa flag
QF0
@ArturMeinild: the datacentre isn't keen on netplan; they say they have problems with it. They would rather I use /etc/network/interfaces. Best option, I think, is to use systemd, since that's more portable and "standard". I think.
Random Person avatar
us flag
Hey QF0! Cross-posting is generally not considered as a good practice in Stack Exchange. I suggest you to go through this: https://meta.stackexchange.com/q/64068. Also, please consider going through the [tour] page.
Score:0
sa flag
QF0

Well, this looks like a bug on 20.04. .network files are meant to be processed in lexical order, according to this extract from the systemd-networkd docs:

The .network files are read from the files located in the system network directories /usr/lib/systemd/network and /usr/local/lib/systemd/network, the volatile runtime network directory /run/systemd/network and the local administration network directory /etc/systemd/network. All configuration files are collectively sorted and processed in lexical order, regardless of the directories in which they live.

However, if I turn on systemd-networkd debugging (see here if you don't know how), it turns out that the lowest-lexically-sorted file has precedence. I can't be entirely sure, because the logs don't show the processing order, and only stop at the first hit for the relevant network interface.

This is what I know, for the plain Ubuntu 20.04 server install:

  1. There are 5 .network files across the system
  2. One of these is created by netplan at runtime, and is /run/systemd/network/10-netplan-enp1s0.network
  3. My new file is /etc/systemd/network/static.network
  4. On systemctl restart systemd-networkd the netplan file takes precedence over my own
  5. If I manually remove the netplan file, and restart networking, my file is acted on. However, this is only temporary till the next reboot
  6. If I rename my file to 05-static.network then it takes precedence over the netplan file

Ergo, processing order broken, unless I'm doing something stupid.

As a temporary fix you need to give your file a low lexical order by prefixing with something like 05-. However, this might break on a future update if the processing order is reversed. A better fix would be to disable netplan (not sure how to do this), but this might also break if something else then decides it has to create a .network file to make up for the missing netplan one.

Edit - removing netplan

You can remove netplan entirely with apt purge netplan.io. However, this may not be a good idea - it also removes cloud-init, for example, which is required by DigitalOcean. So:

# cd /etc/netplan
# rm 00-installer-config.yaml
# netplan apply

After a reboot, you no longer get the auto-generated .network file, and can use normal file naming in your .network files without a conflict.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.