Ubuntu 通过 SSH 登录慢,常见原因和对应解决方法如下(按出现频率排序):
现象
输入密码后等待很久才进入系统。
原因
SSH 默认会对客户端 IP 进行 DNS 反向解析。
解决方法
sudo vim /etc/ssh/sshd_config
添加或修改:
UseDNS no
重启 SSH:
sudo systemctl restart sshd
# 或
sudo systemctl restart ssh
✅ 90% 的情况能解决
现象
连接过程卡在认证阶段。
解决方法
GSSAPIAuthentication no
位置:/etc/ssh/sshd_config
然后重启 SSH。
现象
SSH 尝试 IPv6 连接失败再转 IPv4。
解决方法
方法 1:服务器禁用 IPv6(推荐)
AddressFamily inet
方法 2:客户端连接时禁用 IPv6
ssh -4 user@ip
原因
DNS、密钥交换慢。
客户端优化(可选)
# ~/.ssh/config
Host *
GSSAPIAuthentication no
PreferredAuthentications publickey,password
检查
ssh -v user@ip
解决
KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
排查
top
iostat -x 1
如果是磁盘慢,可能是:
排查
journalctl -u ssh
常见原因
ssh -v user@ip
观察卡在哪一步:
pledge: network → DNSAuthentications that can continue → GSSAPIexpecting SSH2_MSG_KEX → 密钥交换UseDNS no
GSSAPIAuthentication no
AddressFamily inet
如果你愿意,可以把 ssh -v 的输出 或 卡在哪一步 发我,我可以精准帮你定位。