13

I have openssh-server installed but I'd like to sometimes leave the sshd service off by default at boot, and only start it from the terminal as needed.

Based on the the advice of many other questions, enabling and disabling the service at boot should be simple on my systemd using 16.04 distro:

$sudo systemctl disable sshd.service

That seems to work. However, I can no longer enable the service at boot after that:

$sudo systemctl enable sshd.service 
Failed to execute operation: No such file or directory

Even un-installing and re-installing openssh-server doesn't fix it, but a purge does.

How do I re-enable sshd at boot once I've disabled it via systemd?

Note that even in this messed up state I can still manually start and stop the service via service ssh[start|stop]`.

BeeOnRope
  • 675

1 Answers1

29

sshd service is originally written as ssh.service and sshd.service is set as alias name. Check out the last line of following output.

arryph@localhost:~$ systemctl cat sshd.service 
# /lib/systemd/system/ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify

[Install]
WantedBy=multi-user.target
Alias=sshd.service

Because of this, when ssh.service is enabled, we can refer it as sshd.service. But when you disabled sshd.service and rebooted, ssh.service is no longer loaded and because of this you can't refer it as sshd.service in that condition. You have to refer is as ssh.service instead. so if you run sudo systemctl enable ssh.service, it will enable ssh.service (aliased as sshd.service) successfully.

arryph
  • 659