# sshd with a ListenAddress fails at boot before DHCP

I rebooted my home server onto a new kernel and it came back without SSH. `sshd_config` binds one LAN address with `ListenAddress`, and stock `ssh.service` only waits for [`network.target`](https://systemd.io/NETWORK_ONLINE/), which doesn't mean the address exists yet. on that boot sshd started 23 seconds before the interface got its address from DHCP, failed with `Cannot bind any address` and stayed down until it was fixed at the console.

the red herring: `sshd -t` run by hand while the service is down says `Missing privilege separation directory: /run/sshd`. that's only because the unit creates `/run/sshd` when it starts. it says nothing about the config. `journalctl -b -u ssh` had the real error.

## the drop-in

```ini
# /etc/systemd/system/ssh.service.d/override.conf
[Unit]
Wants=network-online.target
After=network-online.target

[Service]
Restart=on-failure
RestartSec=5
```

network-online covers the normal boot. the restart covers the boot where the router is off after a power cut and wait-online gives up after 2 minutes. every 5 seconds stays under [systemd's default start limit](https://www.freedesktop.org/software/systemd/man/latest/systemd-system.conf.html) (5 in 10 s), so it keeps retrying until the address shows up.

proven on the next reboot the same day: sshd started 0.04 s after network-online, 0 restarts.

## while I was in the journal

over the 54 days of uptime before that reboot, the link lost carrier 341 times in 88 bursts. 54 bursts started at 05:00:05, give or take a second. that's the router's own reboot schedule, set to every day at 05:00. most of the rest are likely power cuts. none of it matters to a box that's already up.

the reboot also showed that unattended-upgrades installs kernels and never reboots: 54 days on 6.8.0-139 with 6.8.0-149 installed. `/var/run/reboot-required` is the signal.

---

see also: [timers that assumed an always-on box](/articles/cold-boot-timers)
