12

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
  • 656
  • 1
  • 7
  • 21
  • Do you need to re-enable it to start it at boot or do you need to start it from terminal manually only when you need to use it?? – arryph Nov 21 '17 at 18:22
  • @arryph - admittedly I made the question more confusing by mixing up my original goal and a tangential problem I ran into. My goal was to disable sshd at boot and enable it as needed from the terminal. This is actually working since I can use service ssh start to start it. However, what I found is that if I want to again enable it at boot via systemctrl it doesn't work: the ssh/systemd association seems permanently broken now (solvable via purge which changes all my keys, etc). I edited the question to make it clearer the problem is about systemctrl enable. – BeeOnRope Nov 21 '17 at 18:37

1 Answers1

26

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
  • 629