Systemd-networkd default static address if dhcp fails

dhcpsystemdsystemd-networkd

Is there a way to configure the systemd-networkd.service to automatically switch over to a static IP configuration if a DHCP server is not present?

Or if systemd-networkd doesn't support this directly could I create some other service unit that after some timeout checks to see if DHCP has been succcesful and if not configures a static configuration?

This seems like such a common thing but I'm not seeing it documented anywhere, it must be so simple that I'm missing it.

Best Answer

I don't think you can use systemd-networkd to replace NetworkManager in this way currently. it is mostly being proposed when configuring Virtual Machines where there is no need for dynamic setup.

If you want to get this to work you will have to write a systemd.timer with OnBootSec= some seconds after boot that starts a service that checks if the dhcp has worked, and if not edits the network configuration.

For example if you have a /etc/systemd/network/20-dhcp.network and a /etc/systemd/network/30-static.network, the dhcp should take priority. When you want to override this, add a link /run/systemd/network/09-override.network to the static file, do daemon-reload and restart systemd-networkd to have it take precedence.

By putting the override file in /run, it will be lost on reboot.

Related Question