CentOS SSH配置中的常见问题及解决方法
无法远程连接的首要原因是服务未运行。使用systemctl status sshd
检查服务状态,若未启动,执行systemctl start sshd
启动;若需开机自启,执行systemctl enable sshd
。若启动失败,需检查配置文件语法(sshd -t
)或系统日志(journalctl -xe -u sshd
)定位具体错误。
修改/etc/ssh/sshd_config
(如更改端口、禁用root登录)后,语法错误会导致服务无法重启。使用sshd -t
命令检查配置文件语法,若有错误,根据提示修正(如遗漏“:”或拼写错误),修正后重启服务。
firewalld
,需开放SSH端口(默认22):firewall-cmd --permanent --add-service=ssh
,然后firewall-cmd --reload
;若使用iptables
,执行iptables -A INPUT -p tcp --dport 22 -j ACCEPT
并保存规则。enforcing
模式,临时禁用setenforce 0
,或永久修改/etc/selinux/config
(将SELINUX=enforcing
改为permissive
),重启系统生效。netstat -tulnp | grep 22
检查端口是否被其他进程占用,若有,终止占用进程(kill -9 PID
)或修改SSH端口(/etc/ssh/sshd_config
中修改Port
项,需同步更新防火墙/SELinux规则)。netstat
无22端口输出,需确认/etc/ssh/sshd_config
中ListenAddress
设置为0.0.0.0
(监听所有接口),重启服务。600
(chmod 600 ~/.ssh/id_rsa
),服务器端~/.ssh/authorized_keys
权限为600
,且公钥内容正确。PasswordAuthentication no
),需确保密钥认证正常;若启用,检查用户密码是否正确。/etc/ssh/sshd_config
,设置UseDNS no
,重启服务,避免SSH反向解析客户端主机名。GSSAPIAuthentication no
,减少认证延迟。/etc/ssh/sshd_config
中启用TCPKeepAlive yes
、ClientAliveInterval 60
、ClientAliveCountMax 3
,保持连接活跃,防止超时断开。/etc/ssh/sshd_config
中PermitRootLogin no
,需用普通用户登录后切换root(su -
);若需允许,设置为yes
(不推荐,降低安全性)。AllowUsers user1 user2
(/etc/ssh/sshd_config
)限制可登录用户,修改后重启服务。