一、环境
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.6 LTS
Release: 18.04
Codename: bionic
二、背景
在安装了openssh-server后,将自动将sshd加入到自启动服务中
$ sudo apt install openssh-server
某天觉得不需要sshd自启动时,将sshd停用,并禁用了sshd
$ systemctl stop sshd
$ systemctl disable sshd
Removed /etc/systemd/system/sshd.service.
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
某天需要远程ssh连接时,开启sshd, 启用失败
$ systemctl enable sshd
Failed to enable unit: Unit file sshd.service does not exist.
三、解决方案
方案一、 卸载重装
最暴力的方式,直接卸载了再重新安装
$ sudo apt remove openssh-server --purge -y
$ sudo apt install openssh-server
方案二、建立软连接
细心的人会发现,在disable时只是删除了systemd中的软连接,恢复软连接就可以恢复正常。
$ sudo ln -s /lib/systemd/system/ssh.service /etc/systemd/system/sshd.service
$ sudo ln -s /lib/systemd/system/ssh.service /etc/systemd/system/multi-user.target.wants/ssh.service
可以看到已经恢复正常
$ systemctl start sshd
$ ps aux | grep sshd
root 6846 0.0 0.0 72304 5660 ? Ss 09:52 0:00 /usr/sbin/sshd -D