15

In older versions of Ubuntu I commented out the "start on ..." line in /etc/init/ssh.conf. This worked well, but not in Ubuntu 15.04.

Fango
  • 491

4 Answers4

22

The command

systemctl disable ssh

did it for me.

@Jakuje: Thanks for giving me the idea.

A.B.
  • 90,397
Fango
  • 491
8

This is documented in man systemctl:

systemctl disable ssh

prevents ssh service from automatic starting. But this is the way systemd does it, but ubuntu does not accept it and they have to do it their own way:

Official documentation: https://wiki.ubuntu.com/SystemdForUpstartUsers#Automatic_starting

According to this you should create unit override without directive WantedBy=multi-user.target in /etc/systemd/system/ssh.service (instead of the current symlink):

rm /etc/systemd/system/ssh.service
cp /lib/systemd/system/ssh.service /etc/systemd/system/ssh.service
sed -e "/WantedBy=multi-user.target/d" -i /etc/systemd/system/ssh.service
systemctl daemon-reload
Jakuje
  • 6,605
  • 7
  • 30
  • 37
  • Commenting out the line as described above "WantedBy=multi-user.target" does not affect the autostart on my system. But after executing "systemctl disable ssh" sshd doesn't start anymore, even it was activated manually in the previous session. – Fango Jul 29 '15 at 04:58
  • sure I meant disable ... I don't know why I put there status. It didn't work for me on Ubuntu (unlike on the other systems) so I was searching also for different solution ... – Jakuje Jul 29 '15 at 05:26
  • Maybe it didn't work in your system because you typed sshd instead of ssh? – Fango Jul 29 '15 at 05:40
  • You should prefer the service name ssh over its alias sshd. For example, systemctl enable sshd doesn't work because the aliases cannot be used to enable services. – BeeOnRope Nov 21 '17 at 19:15
  • @BeeOnRope thank you for the comment. I am mostly using Fedora/RHEL where it is named sshd and which is more self-explaining. I am not sure why Debian/Ubuntu started using just ssh for the server. I will edit this answer to reflect it. Thanks. – Jakuje Nov 22 '17 at 08:37
1

I want to disable a service and systemctl disable myservice doesn't work, but run a systemctl daemon-reload after disabling makes it disable.

SAMPro
  • 111
1

It does a bit more than requested, but the foolproof approach is to remove the package:

sudo apt-get remove openssh-server

This works in all versions of Ubuntu.

Assuming you have internet access, or have cached the package, reinstalling (and automatically restarting) is not a problem:

sudo apt-get install openssh-server