Skip to content

Unbound starts too early. Please add Wants=network-online.target under [Unit] in unbound.service #1317

Description

@barrelrider-ops

Describe the bug
Unbound starts before the network interface gains carrier. On my system this caused the following errors:
Aug 21 13:55:55 arch unbound[453]: [453:0] error: failed to connect to redis server: Network is unreachable
Aug 21 13:55:55 arch unbound[453]: [453:0] error: redis_init: failed to init redis (for thread 0)

To reproduce
Steps to reproduce the behavior:

  1. Use unbound.service from https://github.com/NLnetLabs/unbound/blob/master/contrib/unbound.service.in.

  2. I am using systemd-networkd. However, I think this behavior can be reproduced with other network managers too. In my /etc/systemd/network/*.network file, I added the following lines:
    [Link]
    RequiredForOnline=routable
    This ensures that the network is actually routable before the network is flagged as online.

  3. Configure Redis in unbound.conf, so it uses an external database for cachedb. The following lines in my unbound.conf are related to Redis:
    module-config: "respip validator cachedb iterator"
    cachedb:
    backend: "redis"
    cachedb-check-when-serve-expired: yes
    redis-server-host: 100.64.1.2
    redis-server-port: 6458
    redis-expire-records: no
    redis-replica-server-port: 6621
    redis-replica-server-path: "run/valkey/valkey.sock"
    100.64.1.2 is a local ip-address of another computer.

  4. Make sure there are no other applications with a (weak) requirement dependency on network-online.target. In other words, make sure that systemctl list-dependencies --reverse network-online.target doesn't list any dependencies or only lists dependencies that are not triggered on boot. On my system, systemctl list-dependencies --reverse network-online.target lists archlinux-keyring-wkd-sync.service as a dependency, but archlinux-keyring-wkd-sync.service is triggered by a timer and is not triggered on boot.

  5. This means that network-online.target will not get started at all because the line After=network-online.target in unbound.service only defines an ordering dependency. It will not actually try to start network-online.target. This causes unbound to start too early. The following lines in the logs back my claim:

Aug 21 13:55:55 arch unbound[453]: [453:0] error: failed to connect to redis server: Network is unreachable
Aug 21 13:55:55 arch unbound[453]: [453:0] error: redis_init: failed to init redis (for thread 0)
Aug 21 13:55:56 arch systemd-networkd[397]: enp5s0: Gained carrier

Expected behavior
Unbound should always start after the network gains carrier.

You can easily achieve this by setting a weak requirement dependency on network-online.target for Unbound. Currently, it only relies on an ordering dependency (After=network-online.target). An ordering dependency doesn't actually trigger the dependency to start. This is where a (weak) requirement dependency comes in. In other words, please add Wants=network-online.target under [Unit] in unbound.service.

System:

  • Unbound version: 1.23.1
  • OS: Arch Linux
  • unbound -V output:
    Version 1.23.1

Configure line: --prefix=/usr --sysconfdir=/etc --localstatedir=/var --sbindir=/usr/bin --disable-rpath --enable-dnscrypt --enable-dnstap --enable-pie --enable-relro-now --enable-subnet --enable-systemd --enable-tfo-client --enable-tfo-server --enable-cachedb --with-libhiredis --with-conf-file=/etc/unbound/unbound.conf --with-pidfile=/run/unbound.pid --with-rootkey-file=/etc/trusted-key.key --with-libevent --with-libnghttp2 --with-pyunbound
Linked libs: libevent 2.1.12-stable (it uses epoll), OpenSSL 3.5.2 5 Aug 2025
Linked modules: dns64 cachedb subnetcache respip validator iterator
DNSCrypt feature available
TCP Fastopen feature available

BSD licensed, see LICENSE in source package for details.
Report bugs to unbound-bugs@nlnetlabs.nl or https://github.com/NLnetLabs/unbound/issues

Additional information
One more remark related to systemd-networkd. I also needed to add the following lines to /etc/systemd/network/*.network:

[Link]
RequiredForOnline=routable

Without these lines, the network is flagged as online before the network is routable. This will cause the same error: error: failed to connect to redis server: Network is unreachable.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions