Debian – Systemd Network-Online.Target Not Working

debiansystemdsystemd-networkd

I'm trying to create a systemd service on Debian Jessie.
I need it to start after network-online.target is reached.

The problem is network-online.target fires at the same time as network.target and at that time my interfaces are not configured yet, just started DHCP query.

It looks like this issue is specific to Debian because it uses legacy network configuration.

How to bypass this problem or how to make network-online.target working?

Best Answer

Since you're using /etc/network/interfaces, you'll need a systemd service to monitor the status of each interface. Check to see if you have /lib/systemd/system/ifup-wait-all-auto.service (installed by the ifupdown package in Ubuntu 15.04). If not, then create /etc/systemd/system/ifup-wait-all-auto.service, and paste in the following:

[Unit]
Description=Wait for all "auto" /etc/network/interfaces to be up for network-online.target
Documentation=man:interfaces(5) man:ifup(8)
DefaultDependencies=no
After=local-fs.target
Before=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutStartSec=2min
ExecStart=/bin/sh -ec '\
  for i in $(ifquery --list --exclude lo --allow auto); do INTERFACES="$INTERFACES$i "; done; \
  [ -n "$INTERFACES" ] || exit 0; \
  while ! ifquery --state $INTERFACES >/dev/null; do sleep 1; done; \
  for i in $INTERFACES; do while [ -e /run/network/ifup-$i.pid ]; do sleep 0.2; done; done'

[Install]
WantedBy=network-online.target

This is the service file as present on an Ubuntu 15.04 system, but with the [Install] section added in to make things a little easier. I'm hoping that the behavior of ifup in Ubuntu 15.04 is the same as the behavior of ifup in Debian Jessie. If not, some modification will be necessary (particularly with the last line).

Then, run sudo systemctl enable ifup-wait-all-auto.service. After rebooting your computer, you should see that the network-online.target is reached after the interfaces are brought up (at least).

Related Question