CentOS SSH 连接速度优化
一 快速定位瓶颈
ssh -v user@host,观察输出在何处停留(如 DNS 反查、GSSAPI、密码提示等)。time ssh user@host exit,对比优化前后的耗时。ping、traceroute、top/free/df,排除链路抖动、丢包与资源紧张。二 服务端关键优化(sshd_config)
UseDNS no,避免等待 DNS 超时。GSSAPIAuthentication no(如需 Kerberos 再开启)。TCPKeepAlive yes,减少 NAT/防火墙空闲断开。ClientAliveInterval 30(每 30 秒发一次保活),ClientAliveCountMax 99999(允许大量无响应探测,避免误断)。systemctl restart sshd。三 客户端与解析链路优化
~/.ssh/config 增加Host *
ControlMaster auto
ControlPath /tmp/ssh-%r@%h:%p
ControlPersist 600
/etc/ssh/ssh_config 或 ~/.ssh/config 设置 GSSAPIAuthentication no。/etc/hosts 添加 服务器IP 主机名 映射。/etc/nsswitch.conf 将 hosts: 设为 files(如 hosts: files),避免无谓 DNS 查询。/etc/resolv.conf 清理无效或不可达的 nameserver 条目。/etc/hosts 正确映射本机:127.0.0.1 对应 uname -n 的结果,必要时加入 服务器IP 主机名。四 超时与保活设置建议
ClientAliveInterval 300(每 5 分钟发一次),ClientAliveCountMax 保持较大值(如 99999)。ClientAliveInterval 60,ClientAliveCountMax 3,约 3 分钟 无响应即断开。/etc/ssh/ssh_config 或 ~/.ssh/config 设置 ServerAliveInterval 300。/etc/profile 设置 TMOUT=180(单位秒,示例为 3 分钟),按需调整。