How to force network.target to wait for DHCP with systemd-networkd

dhcpnetworkingsystemd

I'm using systemd-networkd to configure my interfaces:

[Match]
Name=enp3s0

[Network]
DHCP=v4    

It works quite well except that at boot, some services are not waiting for DHCP to finish. For instances my NFS-mounted directories and nginx fail because there is no network at the time they boot.

Is there a way to force network.target to wait for DHCP?


Note from the editor. According to the documentation:

Many network management solutions provide a way to unconditionally pull in network-online.target, and thus upgrading the effect of network.target to the effect of network-online.target.

If you use systemd-networkd you can do this by enabling systemd-networkd-wait-online.service:

systemctl enable systemd-networkd-wait-online.service

However I tried this and it did not make services depending on network.target wait for DHCP. It only made services which explicitly depend on network-online.target wait for DHCP, and in fact it is required to make that work.

Best Answer

When using systemd-networkd, to make sure that all interfaces are up and have an IP address assigned use:

systemctl enable systemd-networkd-wait-online.service

Someone else may be using NetworkManager, in which case that would be:

systemctl enable NetworkManager-wait-online.service

See: http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/

Related Question