In this article, we will discuss how to configure the SSHD service to start only when the network is online on Raspbian 12 (Bookworm). This is a common requirement for many users who want to ensure that their SSH server is only accessible when the device is connected to the internet. We will cover the key concepts related to this topic, including systemd, network targets, and SSHD service management.
Prerequisites
Before we begin, it is assumed that you have a basic understanding of the Raspberry Pi, Raspbian, and the command line. You should also have SSH enabled on your Raspberry Pi, as we will be making changes to the SSHD configuration.
Systemd and Network Targets
Systemd is an init system and service manager for Linux. It is responsible for starting and stopping services, as well as managing the system's boot process. Network targets are systemd units that control the network configuration and interface activation. The network.target unit is started when the network is brought up, while the network-online.target unit is started when the network is online.
Configuring SSHD Service
To configure the SSHD service to start only when the network is online, we will create a new configuration file for the SSHD service in the /etc/systemd/system/sshd.service.d/ directory. This directory allows us to override the default settings for the SSHD service. In this file, we will add a new configuration option that specifies the network target that the SSHD service should depend on:
[Unit]
Requires=network-online.target
By specifying that the SSHD service requires the network-online.target unit, we ensure that the SSHD service will only start when the network is online. Once we have created this file, we can reload the systemd configuration and restart the SSHD service to apply the changes:
sudo systemctl daemon-reload
sudo systemctl restart sshd
We can verify that the SSHD service now starts only when the network is online by using the systemctl command:
sudo systemctl start network-online.target
sudo systemctl status sshd
This will show that the SSHD service has started successfully. However, if we try to start the SSHD service without starting the network-online.target unit, the service will fail to start:
sudo systemctl status sshd
This confirms that the SSHD service now starts only when the network is online.
Things You May Have Tried
One approach that you may have tried is editing the /etc/systemd/system/sshd.service file directly. However, this is not recommended, as any changes that you make to this file will be overwritten when the SSHD package is updated. Instead, it is best to create a new configuration file in the /etc/systemd/system/sshd.service.d/ directory, as described above.
- Systemd is a system and service manager for Linux
- Network targets control the network configuration and interface activation
- We can configure the SSHD service to start only when the network is online by creating a new configuration file that specifies the
network-online.targetunit