解决mac下ssh空闲一段时间自动断开的问题

转自:https://blog.csdn.net/moxiaomomo/article/details/52982047

在macbook中使用终端, 通过ssh连接上远程服务器后, 隔一段时间没有数据传递时会自动断开,而表象就是终端会卡着无响应,要等几分钟后才会显示断开并且返回到本地终端命令行。这个问题太影响流畅性和心情, 决定修一修。

第一种方法, 在本地或者远程服务器修改ssh配置, 定时发起类似心跳的信号。

1)在服务器中的/etc/ssh/sshd_config中去掉原有注释并改成这样: 

[plain]  view plain  copy
  1. ClientAliveInterval 60  
  2. ClientAliveCountMax 3  
ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 从而保持长连接. ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到3次, 就自动断开。

2)在客户端中的/etc/ssh/ssh_ssh_config中去掉注释并改成这样:

[plain]  view plain  copy
  1. ServerAliveInterval 60  
  2. ServerAliveCountMax 2  
对应的意义与上面类似。


第二种方法,使用autossh, 它会在ssh断开后自动再连接上。

目前我用的是这个版本: https://github.com/wufeifei/autossh


第三种方法,使用mosh工具, 参考http://mosh.mit.edu/ 。

据说是神器, 不过我没用上。


猜你喜欢

转载自blog.csdn.net/tcf_jingfeng/article/details/80417946