CentOS SSH 登录慢是非常常见的问题,通常不是网络本身慢,而是 SSH 服务端在做额外的检查或解析。下面按最常见 → 次常见依次说明原因和解决方案,基本对着做就能解决 90% 的情况。
SSH 默认会对客户端 IP 做 DNS 反向解析,如果 DNS 不可用或慢,就会卡住十几秒。
在 SSH 服务端(CentOS) 关闭 DNS 解析:
vim /etc/ssh/sshd_config
找到或添加:
UseDNS no
重启 SSH:
systemctl restart sshd
✅ 这是解决 SSH 登录慢的第一要务
SSH 默认尝试 GSSAPI 认证(Kerberos),如果没有配置,会超时等待。
编辑配置:
vim /etc/ssh/sshd_config
设置:
GSSAPIAuthentication no
重启 SSH:
systemctl restart sshd
也可在客户端禁用(临时):
ssh -o GSSAPIAuthentication=no user@ip
/etc/resolv.conf 里有不可用的 DNS/etc/hosts 配置异常确保本机 IP 对应主机名:
vim /etc/hosts
示例:
127.0.0.1 localhost
192.168.1.10 centos
SSH 同时监听 IPv6,但网络环境只支持 IPv4。
限制 SSH 只监听 IPv4:
AddressFamily inet
或:
ListenAddress 0.0.0.0
重启 SSH:
systemctl restart sshd
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
如果 SSH 明显变快,说明 SELinux 有影响。
top
iostat -x 1
如果磁盘 IO 很高,SSH 也会看起来“卡”。
-vssh -v user@ip
卡在哪里一目了然,比如:
expecting SSH2_MSG_KEX_ECDH_REPLY → 网络Authentication that can continue: gssapi-with-mic → GSSAPIreverse mapping checking → DNSUseDNS no
GSSAPIAuthentication no
AddressFamily inet
systemctl restart sshd
| 原因 | 是否常见 | 解决 |
|---|---|---|
| DNS 反向解析 | ⭐⭐⭐⭐⭐ | UseDNS no |
| GSSAPI | ⭐⭐⭐⭐ | GSSAPIAuthentication no |
| IPv6 | ⭐⭐⭐ | AddressFamily inet |
| hosts / DNS | ⭐⭐ | 检查 /etc/hosts |
| SELinux | ⭐ | setenforce 0 测试 |
如果你愿意,可以把 ssh -v 输出卡住的那几行贴出来,我可以帮你精确定位是哪一步慢。