在 CentOS 上“修复 SSH 错误”要看具体报什么错。下面我按最常见场景给你一套排查与修复流程,你可以对照使用。
systemctl status sshd
如果没启动:
systemctl start sshd
systemctl enable sshd
原因:sshd 没运行 / 端口不对 / 防火墙拦了
检查端口:
ss -lntp | grep ssh
默认是 22
防火墙(CentOS 7/8):
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload
SELinux(少见但可能):
getenforce
setenforce 0 # 临时关闭测试
原因:密钥不对 / 禁止密码登录
检查:
cat /etc/ssh/sshd_config | grep -i pubkey
确保:
PubkeyAuthentication yes
如果是密码登录被禁:
PasswordAuthentication no
可改为 yes(测试用)
修改后重启:
systemctl restart sshd
查看详细错误:
journalctl -xe
或:
sshd -t
常见原因:
重建密钥:
ssh-keygen -A
systemctl restart sshd
本地执行:
ssh-keygen -R 服务器IP
再重新连接。
检查:
grep -E "PermitRootLogin|PasswordAuthentication" /etc/ssh/sshd_config
建议:
PermitRootLogin yes
PasswordAuthentication yes
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
你可以直接把 具体报错信息 或 sshd 日志 发给我,例如:
ssh user@ip
报错:xxx
我可以给你精准修复命令。