温馨提示×

centos为何ssh连接失败

小樊
35
2026-07-19 07:38:18
栏目: 智能运维

SSH 连接 CentOS 失败 的原因很多,下面按最常见 → 较少见的顺序给你一个排查清单。你可以对照一步一步查。


一、最常见原因(80% 都在这里)

1️⃣ SSH 服务没启动

systemctl status sshd

如果没运行:

systemctl start sshd
systemctl enable sshd

2️⃣ 防火墙拦截 22 端口

查看防火墙状态:

systemctl status firewalld

放行 SSH:

firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

或直接测试(临时关闭):

systemctl stop firewalld

3️⃣ SSH 端口不是 22

查看端口:

ss -lntp | grep ssh

如果改过端口,比如 2222:

ssh -p 2222 user@ip

4️⃣ SELinux 阻止(CentOS 特有)

查看状态:

getenforce

临时关闭测试:

setenforce 0

如果关闭后能连,说明是 SELinux 问题。


二、网络与配置问题

5️⃣ IP 不通 / 网络问题

ping 服务器IP

如果是云服务器:

  • 检查 安全组 / 防火墙规则
  • 是否允许 22 端口入站

6️⃣ SSH 配置错误

查看配置文件:

vim /etc/ssh/sshd_config

重点检查:

PermitRootLogin yes
PasswordAuthentication yes
PubkeyAuthentication yes

修改后重启:

systemctl restart sshd

7️⃣ 账号被锁 / 密码错误

passwd -S 用户名

解锁:

passwd -u 用户名

三、密钥 / 权限问题

8️⃣ 公钥权限不对(密钥登录失败)

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

9️⃣ 私钥权限不对(客户端)

chmod 600 id_rsa

四、查看 SSH 日志(非常重要)

服务器端

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 网络不通

六、如果你愿意,我可以精准定位

你可以直接把下面信息发我(可打码):

  1. CentOS 版本
    cat /etc/centos-release
    
  2. 报错信息
  3. 本地 / 云服务器
  4. 密码还是密钥登录

我可以一步一步帮你修 ✅

0