SSH 连接 CentOS 失败 的原因很多,下面按最常见 → 较少见的顺序给你一个排查清单。你可以对照一步一步查。
systemctl status sshd
如果没运行:
systemctl start sshd
systemctl enable sshd
查看防火墙状态:
systemctl status firewalld
放行 SSH:
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
或直接测试(临时关闭):
systemctl stop firewalld
查看端口:
ss -lntp | grep ssh
如果改过端口,比如 2222:
ssh -p 2222 user@ip
查看状态:
getenforce
临时关闭测试:
setenforce 0
如果关闭后能连,说明是 SELinux 问题。
ping 服务器IP
如果是云服务器:
查看配置文件:
vim /etc/ssh/sshd_config
重点检查:
PermitRootLogin yes
PasswordAuthentication yes
PubkeyAuthentication yes
修改后重启:
systemctl restart sshd
passwd -S 用户名
解锁:
passwd -u 用户名
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 id_rsa
journalctl -u sshd -f
或
tail -f /var/log/secure
ssh -vvv user@ip
| 报错 | 原因 |
|---|---|
| Connection refused | sshd 未启动 / 端口错 |
| Connection timeout | 防火墙 / 云安全组 |
| Permission denied | 密码错 / 密钥错 |
| Too many authentication failures | 密钥太多 |
| No route to host | 网络不通 |
你可以直接把下面信息发我(可打码):
cat /etc/centos-release
我可以一步一步帮你修 ✅