欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > 如何解决Unit sshd.service could not be found

如何解决Unit sshd.service could not be found

2025/2/6 0:44:29 来源:https://blog.csdn.net/weixin_42108533/article/details/145399971  浏览:    关键词:如何解决Unit sshd.service could not be found

出现 Unit sshd.service could not be found 错误时,通常是因为系统中未安装 OpenSSH 服务、服务名称不匹配或系统未使用 systemd 管理服务。以下是详细的解决方案:


一、确认 SSH 服务是否安装

1. 检查是否已安装 OpenSSH 服务器

不同 Linux 发行版的包名称可能略有不同:

  • Debian/Ubuntu
    dpkg -l | grep openssh-server
    
  • CentOS/RHEL
    rpm -qa | grep openssh-server
    
2. 若未安装,执行安装命令
  • Debian/Ubuntu
    sudo apt update && sudo apt install openssh-server
    
  • CentOS/RHEL
    sudo yum install openssh-server
    # 或
    sudo dnf install openssh-server
    

二、确认服务名称

某些系统使用 ssh 而非 sshd 作为服务名(如 Ubuntu 18.04+):

# 查看服务名称
systemctl list-unit-files | grep -E "ssh|sshd"# 尝试以下命令:
sudo systemctl status ssh   # Ubuntu/Debian
sudo systemctl status sshd  # CentOS/RHEL

三、手动启动 SSH 服务

1. 使用 systemctl(针对 systemd 系统)
  • 如果服务名是 ssh
    sudo systemctl start ssh    # 启动服务
    sudo systemctl enable ssh   # 设置开机自启
    
  • 如果服务名是 sshd
    sudo systemctl start sshd
    sudo systemctl enable sshd
    
2. 使用 service 命令(兼容 SysVinit)
sudo service ssh start    # Debian/Ubuntu
sudo service sshd start   # CentOS/RHEL

四、处理非 systemd 系统

如果系统使用 SysVinit(如旧版 Debian 或嵌入式系统):

# 检查服务脚本
ls /etc/init.d/ | grep -E "ssh|sshd"# 启动服务
sudo /etc/init.d/ssh start    # Debian/Ubuntu
sudo /etc/init.d/sshd start   # CentOS/RHEL

五、检查防火墙和端口

确保 SSH 端口(默认 22)已开放:

  • Ubuntu
    sudo ufw allow 22
    
  • CentOS/RHEL
    sudo firewall-cmd --permanent --add-port=22/tcp
    sudo firewall-cmd --reload
    

六、日志排查

查看 SSH 服务日志以定位问题:

# Debian/Ubuntu
tail -f /var/log/auth.log# CentOS/RHEL
tail -f /var/log/secure

七、特殊场景

1. 使用 Docker 或容器环境

容器内可能默认未安装 SSH,建议通过 docker exec 进入容器,无需额外配置 SSH。

2. 自定义编译安装 OpenSSH

如果手动编译安装 SSH,需自行配置服务:

# 示例:手动创建 systemd 服务文件
sudo vi /etc/systemd/system/sshd.service

文件内容参考:

[Unit]
Description=OpenSSH Daemon
After=network.target[Service]
ExecStart=/usr/local/sbin/sshd -D  # 根据实际路径调整
Restart=always[Install]
WantedBy=multi-user.target

重新加载 systemd:

sudo systemctl daemon-reload
sudo systemctl start sshd

总结步骤

  1. 安装 OpenSSH 服务器
  2. 确认服务名sshsshd)。
  3. 启动并启用服务
  4. 检查防火墙和端口
  5. 查看日志排查深层问题。

按上述步骤操作后,SSH 服务应能正常运行。若仍报错,请提供具体发行版和操作日志以进一步分析!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com