CentOS 上优化 SSH 性能的可操作清单
一 定位瓶颈
ssh -vv user@host,若看到 “gssapi-with-mic” 阶段耗时,多半是 GSSAPI 导致;若连接初期停顿,常与 DNS 反向解析 相关。time ssh user@host exit,对比优化前后耗时差异。ping、traceroute、top/free/df -h,排除丢包、延迟与资源紧张。二 服务端关键优化
UseDNS no(即便被注释,默认也是 yes,建议显式关闭)。GSSAPIAuthentication no,必要时 GSSAPICleanupCredentials no,可显著缩短登录握手时间。TCPKeepAlive yes,设置 ClientAliveInterval 30、ClientAliveCountMax 99999,减少空闲断开与重复握手。Protocol 2。systemctl restart sshd。三 客户端与连接复用优化
Host *
ControlMaster auto
ControlPath /tmp/ssh-%r@%h:%p
ControlPersist 600
GSSAPIAuthentication no。-p <new_port> 或在配置中声明端口。四 安全加固与性能兼得的设置
PubkeyAuthentication yes,验证可用后再 PasswordAuthentication no,既提升安全也减少口令协商开销。PermitRootLogin no,通过普通用户 + sudo 提权。MaxAuthTries 6、MaxStartups 5(或适度更高并发但配合防火墙/登录审计)。Port 822(示例),并配合防火墙放行新端口;此举主要降低噪音与自动化攻击面,对单次握手性能影响有限。X11Forwarding no。五 网络与系统层面的配合
hosts: 设为 files(仅本地解析,若需外网域名解析再改为 files dns),并清理 /etc/resolv.conf 中无效或慢速的 nameserver。firewall-cmd 或 iptables 放行 SSH 端口(默认 22 或自定义端口),避免策略导致的连接失败或超时。