6

After upgrading to Kubuntu 19.04, I don't know how to write a service so that it can start after DNS service has been initialized.

In this past, I just use After=network.target, but now it doesn't work. I also tried After=network-online.target, but no luck.

So I think the problem is systemd-resolved, how could I solve this problem?


update:

ERROR: getaddrinfo: Temporary failure in name resolution
ERROR: failed to resolve the provided hostname

These are two related logs. So I can confirm the problem is related to DNS service.

7
  • If systemd-resolved is the problem, why not use After=systemd-resolved.service? But it sounds as if you are not really sure if that is really your problem, so I would recommend to find the source of the problem first. Look at the service log and journalctl.
    – Thomas
    May 19, 2019 at 10:13
  • @Thomas Thanks, I've updated the problem.
    – Sraw
    May 19, 2019 at 10:21
  • Does the name resolution work when the system is fully booted and you are logged in? ping www.google.com or does your service start then if you start it manually?
    – Thomas
    May 19, 2019 at 10:25
  • @Thomas And the reason why I didn't use After=systemd-resolved.service is that systemd-resolved.service's unit says Before=network.target. So if I use After=network.target, it implicitly After=systemd-resolved.service.
    – Sraw
    May 19, 2019 at 10:26
  • @Thomas Definitely, it works seamlessly. I can directly use systemctl start myservice.service to start it.
    – Sraw
    May 19, 2019 at 10:26

2 Answers 2

5

Please add a dependency in your service file:

After=nss-lookup.target

This should ensure that host/name lookup is operable.

Further details are in the documentation: https://www.freedesktop.org/software/systemd/man/systemd.special.html

nss-lookup.target

A target that should be used as synchronization point for all host/network name service lookups. Note that this is independent of UNIX user/group name lookups for which nss-user-lookup.target should be used. All services for which the availability of full host/network name resolution is essential should be ordered after this target, but not pull it in. systemd automatically adds dependencies of type After= for this target unit to all SysV init script service units with an LSB header referring to the "$named" facility.

To fully wait for network, the documentation of systemd https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ directs you to add to your service:

After=network-online.target
Wants=network-online.target

and to confirm that the relevant "wait" service is enabled:

$ systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
disabled
enabled
8
  • Thanks for your answer but it just doesn't work. I am also really confused about this problem...
    – Sraw
    May 22, 2019 at 12:45
  • 2
    can you confirm that you have the Before=nss-lookup.target dependency in your copy of systemd-resolvd.service? Just to be sure and in line with the documentation: freedesktop.org/wiki/Software/systemd/NetworkTarget You did add After=network-online.target and actively request this target by Wants=network-online.target and it still does not have network when your service starts? Can you double-check the startup sequence? systemd-analyze plot >startup.svggives a nice graphical overview.
    – user228505
    May 22, 2019 at 14:44
  • 1
    It does work when I change network.target to network-online.target :) I see that network-online.target is a very late target in the startup sequence. BTW, although my service runs after nss-lookup.target all the time, it only works when I change to network-online.target.
    – Sraw
    May 23, 2019 at 6:37
  • The documentation of systemd hides this a bit. nss-online.target allows your service to communicate with the resolver. In case you need the network to be up and running to have the resolver contacting other systems then the network needs to be running. For this purpose exists a one-shot service which waits (long) for the network. systemctl cat systemd-networkd-wait-online.service shows details of this service, which is ordered before network-online.target.
    – user228505
    May 23, 2019 at 9:22
  • systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service -> Failed to get unit file state for NetworkManager-wait-online.service: No such file or directory on Ubuntu 20.04
    – Íhor Mé
    Jun 12, 2020 at 14:44
0

See the unit file at the end of this ... https://systemd.io/NETWORK_ONLINE/

IOW to delay until your system can resolve DNS, you need to create a new dependency unit for network-online which actually tests the name resolution. This will not work if, for example the system suspends, since the network-online.target dependency was satisfied before the suspend.

Better to include a test in your application.

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Feb 13 at 7:12

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.