Ubuntu 20.04 - 解决 Failed to enable unit: Unit file sshd.service does not exist.

文章目录

  • 起因
  • 解决方法
  • 参考

Ubuntu 版本信息:

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.3 LTS
Release:	20.04
Codename:	focal

起因

事情是这样的,原本 SSH 服务是可用的:

$ sudo systemctl status sshd
[sudo] password for mk: 
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-02-19 19:19:56 CST; 1min 23s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 840 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 856 (sshd)
      Tasks: 1 (limit: 4588)
     Memory: 2.4M
     CGroup: /system.slice/ssh.service
             └─856 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

219 19:19:56 mk-virtual-machine systemd[1]: Starting OpenBSD Secure Shell server...
219 19:19:56 mk-virtual-machine sshd[856]: Server listening on 0.0.0.0 port 22.
219 19:19:56 mk-virtual-machine sshd[856]: Server listening on :: port 22.
219 19:19:56 mk-virtual-machine systemd[1]: Started OpenBSD Secure Shell server.

嗯,我手痒,执行了 sudo systemctl disable sshd 命令:

$ sudo systemctl disable sshd
Removed /etc/systemd/system/sshd.service.
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.

当我再次使用 sudo systemctl status sshd 命令,就出现了:

$ sudo systemctl status sshd
Unit sshd.service could not be found.

使用 sudo systemctl enable sshdsudo systemctl restart sshd 命令,也出现类似情况:

$ sudo systemctl enable sshd
Failed to enable unit: Unit file sshd.service does not exist.
$ sudo systemctl restart sshd
Failed to restart sshd.service: Unit sshd.service not found.

解决方法

后来是卸载、重装 SSH 才得以解决:

$ sudo apt-get remove --purge openssh-server
$ sudo apt-get update
$ sudo apt-get install openssh-server

参考

SSH Server cannot be found - Even though installed

你可能感兴趣的:(Linux,ubuntu,linux,ssh,systemctl)