解决ssh自动掉线问题

Author Avatar
呃哦 10月 23, 2017

问题描述

ssh在一段时间没有操作输入时将会掉线,原因是 在防火墙中会有闲置超时断开 的默认机制
因此,在解决 闲置超时断开 问题,可以采用定时发送心跳包的方式解决

服务器端解决方法:

# /etc/ssh/sshd_config
TCPKeepAlive yes
ClientAliveInterval 300
ClientAliveCountMax 3

在服务器端ssh配置文件添加上述内容.其中,
TCPKeepAlice yes 表示TCP连接保持不断开
ClientAliveInterval 300 表示每个300秒向客户端发送心跳包,然后等待客户度响应,成功则保持连接
ClientAliveCountMax 3 表示客户端无响应的情况下最多尝试请求次数,超过则自动断开
最后,重启sshd服务:
sudo systemctl restart ssh.service

客户端解决方法:

  • 配置文件解决:
      ServerAliveInterval 60
      ServerAliveCountMax 3
    
    ServerAliveInterval 60 表示每隔60秒向服务器端发送心跳包
    ServerAliveCountMax 3 表示无响应最多尝试次数
  • 连接参数解决:
    ssh -o ServerAliveInterval=30 user@host