Ubuntu SSH性能优化清单
一 快速定位瓶颈
ssh -v user@host,若长时间停在“Next authentication method: gssapi-with-mic”,多半是 GSSAPI 导致;若建立连接后输入命令回显慢,常见于 DNS 反向解析耗时。time ssh user@host exit,对比优化前后的耗时差异。/etc/nsswitch.conf 的 hosts: 行若包含 dns 且 DNS 不可达,会放大连接时延;必要时临时改为 hosts: files 验证。sudo systemctl restart ssh(或 ssh.service)。二 服务器端优化
UseDNS no,可显著缩短握手时间。GSSAPIAuthentication no(无 Kerberos 环境),避免 GSS 协商带来的额外往返。Compression yes,在带宽受限或传输文本/日志时有效(CPU 开销略增)。MaxStartups 10:30:60(示例值),抵御暴力破解并避免资源被耗尽。Protocol 2,禁用过时协议与弱算法。net.ipv4.tcp_window_scaling=1,选择拥塞控制算法如 net.ipv4.tcp_congestion_control=cubic,执行 sudo sysctl -p 生效。sudo systemctl restart ssh。三 客户端优化
ConnectTimeout=10、ServerAliveInterval=30、ServerAliveCountMax=3、TCPKeepAlive=yes。ForwardAgent no、ForwardX11 no,减少额外通道开销。KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256Cipher aes256-ctr(兼顾性能与安全性)。ControlMaster auto、ControlPath ~/.ssh/sockets/%r@%h:%p、ControlPersist 600,或使用 tmux/screen 保持会话。sudo apt update && sudo apt install --only-upgrade openssh-client。四 认证与会话策略
ClientAliveInterval/ServerAliveInterval 保活,网络抖动时不易卡死。sudo apt-get install mosh,连接:mosh user@host。五 安全与维护建议
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak-$(date +%F)),变更后在测试环境验证,再重启服务。