So several days ago I was setting up Raspberry pi as a small server where I will have k3s and a few other services I am running for personal needs. Anyway, I wanted also to add dyndns to it so I can access it outside of the local network however, I did not want sshd to be accessible from the outside. Thus, I’ve set in the configuration of `/etc/ssh/sshd_config the following line:
ListenAddress 192.168.1.2
This would force binding sshd to an internal local address that is assigned by my local DHCP on the router based on the MAC address of the WIFI interface of the Raspberry Pi 4. Long story short, I was always getting an error when I rebooted the Pi. I’ve figured out that sshd cannot start until the network interface is enabled and the address assigned. So after searching around StackOverflow I found a solution to the problem in the usage of NetworkManager-wait-online.service which delays network-online.target until the network is ready.
Ok so now we need to change /etc/systemd/system/sshd.service
in the [Unit]
section we need to change After
and Wants
vars like this:
After=network-online.target
Wants=network-online.target
Now we just need to enable network-online service by:
systemctl enable NetworkManager-wait-online.service
systemctl enable systemd-networkd-wait-online.service
Now you can check if everything is enabled by typing:
systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
The response should be enabled for both. Now you can reboot and have sshd working properly :)