juejin_content

2 阅读1分钟

SSH连接用着用着就断了?不是网络问题,是配置没做好💡

🔧 客户端配置(~/.ssh/config)

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3
  TCPKeepAlive yes

每60秒发一次心跳,3次无响应才断开

🖥️ 服务端配置(/etc/ssh/sshd_config)

ClientAliveInterval 60
ClientAliveCountMax 3

服务端也要配,双向保活更稳

⏱️ 会话超时设置

TMOUT=0  # 写入 ~/.bashrc

禁用shell自动超时

🌐 跳板机场景

Host jump-server
  ProxyCommand ssh -W %h:%p bastion
  ServerAliveInterval 30

多级跳转也能保持连接

📱 移动网络优化

Host mobile-*
  ServerAliveInterval 30
  TCPKeepAlive no

移动网络环境缩短心跳间隔

💡 小贴士

  • 配置完记得 sudo systemctl restart sshd
  • 客户端配置优先级高于服务端
  • 心跳间隔别设太短,会增加流量消耗

再也不用担心SSH连接突然断了!