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.
4 Answers
The command
systemctl disable ssh
did it for me.
@Jakuje: Thanks for giving me the idea.
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
-
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.– FangoJul 29, 2015 at 4:58
-
sure I meant
disable
... I don't know why I put therestatus
. It didn't work for me on Ubuntu (unlike on the other systems) so I was searching also for different solution ...– JakujeJul 29, 2015 at 5:26 -
Maybe it didn't work in your system because you typed sshd instead of ssh?– FangoJul 29, 2015 at 5:40
-
You should prefer the service name
ssh
over its aliassshd
. For example,systemctl enable sshd
doesn't work because the aliases cannot be used to enable services. Nov 21, 2017 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 justssh
for the server. I will edit this answer to reflect it. Thanks.– JakujeNov 22, 2017 at 8:37
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
I want to disable a service and systemctl disable myservice
doesn't work, but run a systemctl daemon-reload
after disabling makes it disable.