Enabling and disabling sshd at boot via systemd

29,211

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.

Share:
29,211

Related videos on Youtube

BeeOnRope
Author by

BeeOnRope

Unless otherwise specified in a specific answer, any code included in my answers1 is released by me into the public domain or under CC0 at your option. 1 This includes only code in the text of answers, and doesn't extend to any linked code or anything in non-answers such as comments or questions. Furthermore, it evidently cannot apply to code which I've copied (generally with attribution) from another author.

Updated on September 18, 2022

Comments

  • BeeOnRope
    BeeOnRope over 1 year

    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]`.

    • arryph
      arryph over 6 years
      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??
    • BeeOnRope
      BeeOnRope over 6 years
      @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
    BeeOnRope over 6 years
    Excellent, it works. It makes aliases pretty unusable since they work for some commands and not for others.
  • arryph
    arryph over 6 years
    that's how service aliases work, you can use them if the service is loaded.
  • Dmitry Papka
    Dmitry Papka almost 4 years
    Oh my gosh, thank you so much!