在 Linux 系统(如 Ubuntu、Debian、CentOS 等)中使用 systemctl start ssh 或 systemctl start sshd 启动 OpenSSH 服务时,有时会遇到如下错误:
Job for ssh.service failed because the control process exited with error code. See "systemctl status ssh.service" and "journalctl -xe" for details.
本文将帮助你诊断并修复该问题。
首先,运行以下命令获取具体错误日志:
sudo systemctl status ssh # 或(某些系统使用 sshd) sudo systemctl status sshd
同时可查看系统日志:
sudo journalctl -u ssh --since today # 或 sudo journalctl -xe
编辑 /etc/ssh/sshd_config 后若格式不正确,会导致服务无法启动。
验证配置文件:
sudo sshd -t
若输出错误信息,请根据提示修正 /etc/ssh/sshd_config 文件。
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
OpenSSH 默认使用 22 端口。若该端口已被占用或非 root 用户尝试绑定低端口(<1024),将启动失败。
检查端口占用:
sudo ss -tulnp | grep ':22'
确保 Port 22 在配置文件中未被重复定义,且无其他服务(如 Apache、旧 SSH 实例)占用。
不同发行版服务名可能不同:
sshsshd请使用正确的命令:
# Ubuntu sudo systemctl start ssh # CentOS sudo systemctl start sshd
首次安装后若未生成密钥,服务可能无法启动。
重新生成密钥:
sudo rm /etc/ssh/ssh_host_* sudo dpkg-reconfigure openssh-server # Debian/Ubuntu # 或 sudo ssh-keygen -A # 通用方法
sudo systemctl restart ssh # 或 sshd sudo systemctl enable ssh # 开机自启
sudo systemctl is-active ssh # 应返回 "active" ss -tuln | grep :22 # 检查端口监听
“Failed to start openssh server” 通常由配置错误、端口冲突或缺失密钥引起。通过 systemctl status 和 sshd -t 可快速定位问题。按上述步骤逐一排查,绝大多数情况都能顺利解决。