温馨提示×

centos ssh被拒绝为何

小樊
98
2026-09-05 20:26:19
栏目: 智能运维

在 CentOS 上遇到 SSH 被拒绝(Connection refused / Permission denied),常见原因和排查思路如下:


一、Connection refused(连接被拒绝)

通常表示 目标端口没开或没监听

1. SSH 服务未启动

systemctl status sshd

若未运行:

systemctl start sshd
systemctl enable sshd

2. SSH 未安装

rpm -q openssh-server

未安装则:

yum install -y openssh-server

3. 端口不是 22

查看配置:

grep Port /etc/ssh/sshd_config

连接时需指定端口:

ssh -p 端口号 user@ip

4. 防火墙阻止

firewall-cmd --list-all

放行 SSH:

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

5. SELinux 影响(少见)

getenforce

可临时测试:

setenforce 0

二、Permission denied(权限被拒绝)

通常表示 连上了,但登录失败

1. 用户名或密码错误

ssh user@ip

2. 禁止 root 登录

grep PermitRootLogin /etc/ssh/sshd_config

若为 no,需用普通用户登录。

3. 密钥问题

  • 公钥未写入 ~/.ssh/authorized_keys
  • 权限错误:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

4. 密码认证被关闭

grep PasswordAuthentication /etc/ssh/sshd_config

若为 no,只能用密钥。


三、网络 / 安全组问题

  • 云服务器:安全组未放行 22 端口
  • 本地网络:IP 不通
ping ip
telnet ip 22

四、快速排查顺序(推荐)

  1. ping ip
  2. telnet ip 22
  3. systemctl status sshd
  4. firewall-cmd --list-all
  5. 查看 /var/log/secure

如果你能提供 具体报错信息(如 Connection refused 还是 Permission denied),我可以更精准帮你定位。

0 踩