4

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.

  • 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 '16 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. – Julie Pelletier Sep 9 '16 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. – Matt Schuckmann Sep 9 '16 at 22:33
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.

  • 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 '17 at 3:11
  • Sorry I did not see this response until today. – Matt Schuckmann Feb 7 '18 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. – Matt Schuckmann Feb 7 '18 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.