Linux Administration Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

The concept of enabled and disabled is relatively easy to understand, those states being that the service will either try to run or it won't.

static is something different; this is the terminology used when a unit file exists, isn't enabled, but has no ability to become enabled, owing to a missing [Install] section of its unit file.

We can list these services with the following line:

$ systemctl --no-pager list-unit-files --type service --state static

Taking a service at random (sshd-keygen), we can have a look at its service file like so:

$ systemctl cat sshd-keygen.service
Using systemctl's cat option is great, as it also shows you the location of the unit file in question.

We get the following:

# /usr/lib/systemd/system/sshd-keygen.service
[Unit]
Description=OpenSSH Server Key Generation
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_rsa_key
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_ecdsa_key
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_ed25519_key
PartOf=sshd.service sshd.socket

[Service]
ExecStart=/usr/sbin/sshd-keygen
Type=oneshot
RemainAfterExit=yes

From this file, we can see it has a PartOf definition, suggesting it's run as part of the sshd service.

Taking a look at that service (again using systemctl cat) reveals the following:

# /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

We can see the Wants section here, suggesting sshd-keygen is run when sshd starts.

This explains why it doesn't have to be enabled on its own.