延长 SSH 连接时间,防止短暂闲置时断开

几种方法

  • 编辑远程连接时读取的 SSH 配置文件
    Host remote_host_name:
      HostName remote_host_ipaddr
      ServerAliveInterval 240
    
  • 编辑程序配置文件
    $ sed -i 's/#ClientAliveCountMax 3/ClientAliveCountMax 60/g'  /etc/ssh/sshd_config && systemctl restart sshd
    
  • 编辑用户配置文件
    $ touch ~/.ssh/config; echo ServerAliveInterval 60 >> ~/.ssh/config
    
  • 编辑全局配置文件
    $ echo "TMOUT=3600" >> /etc/profile
    
  • 服务器上使用 Expect 模拟键盘操作
    #!/usr/bin/expect  
    set timeout 60  
    spawn ssh [username]@[host]
    interact {          
        timeout 300 {send "\x20"}  
    } 
    expect keepalives
    
  • 虚拟终端发送模拟键盘操作或NO-OP协议包(以 MobaXterm 为例)
    MobaXterm > Settings > Configuration > SSH > SSH settings > SSH keepalive
    

参考资料

评论