7

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.

3
  • 1
    Describe the use case a little bit more. This type of configuration is not very common especially in production environments as you would use either DHCP or a static address and not bounce between the two dynamically. This is programming, of course, so anything is possible...
    – mdpc
    Sep 9, 2016 at 20:10
  • 1
    I agree with mdpc. I don't see how it would be a common thing to switch between DHCP and static IPs. I've seen thousand of network configurations and never saw such a need. Sep 9, 2016 at 20:15
  • The use case is I have an embedded device that can be configured for a static IP address or to use DHCP, the requirement is if DHCP is enabled but there is no DHCP server available it should fall back to the static IP address. That seems pretty reasonable to me and I've seen plenty of embedded devices that work that way. Heck Windows works that way. Sep 9, 2016 at 22:33

1 Answer 1

2

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.

3
  • It's a shame network files can't be "started" like units can - the only way to apply a different network config seems to be what you described: symlink to change order, then systemctl daemon-reload && systemctl restart systemd-networkd
    – thom_nic
    May 24, 2017 at 3:11
  • Sorry I did not see this response until today. Feb 7, 2018 at 22:13
  • I solved this a different way, I allow my device to have 2 or more simultaneous IP addresses. The static IP address is by default enabled and so is DHCP, if DHCP succeeds the device will have 2 IP addresses. If the user does not like this they can explicitly remove the static IP address, or explicitly disable DHCP. The only problem I've found with this so far is that the Avahi mdns daemon does not like devices with multiple addresses. Feb 7, 2018 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .