淘先锋技术网

首页 1 2 3 4 5 6 7

 

ssh连接超时问题解决方案:

1.修改server端的etc/ssh/sshd_config

ClientAliveInterval 60 #server每隔60秒发送一次请求给client,然后client响应,从而保持连接

ClientAliveCountMax 3 #server发出请求后,客户端没有响应得次数达到3,就自动断开连接,正常情况下,client不会不响应

 

2.修改client端的etc/ssh/ssh_config添加以下:(在没有权限改server配置的情形下)

ServerAliveInterval 60 #client每隔60秒发送一次请求给server,然后server响应,从而保持连接

ServerAliveCountMax 3  #client发出请求后,服务器端没有响应得次数达到3,就自动断开连接,正常情况下,server不会不响应

 

3.另一种方式: 

不修改配置文件

在命令参数里ssh -o ServerAliveInterval=60 这样子只会在需要的连接中保持持久连接, 毕竟不是所有连接都要保持持久的

 

=====================补充========================

keeping your ssh session alive through  
pesky(讨厌的,麻烦的) NAT firewalls.

NAT firewalls like to time out idle sessions to keep their state tables clean and their memory footprint low. 

NAT防火墙喜欢对空闲的会话进行超时处理,以确保它们状态表的干净和内存的低占用率。

Some firewalls are nice, and let you idle for up to a day or so; some are gestapo and terminate your session after 5 minutes. 

一些防火墙比较友好,允许你的空闲会话时间为一天甚至超过一天;另一些却如盖世太保,5分钟空闲就终止你的会话。

I finally got tired of my ssh sessions getting disconnected at places where I don't control the firewalls, and figured out how to stop it. 

我最终疲于我的ssh连接会话被我不能控制的防火墙断开,并找出了阻止它的方案。

Turn out ssh has a nice inband keepalive mechanism, and even lets you set it on a per-destination basis.

结论是ssh内带有一个很好的保持会话存活机制,甚至允许你在每一台目标机器上设置。 

Just create a ~/.ssh/config file with something like the following (* will match any host, if you wanted you could restrict this to particular destinations like *kehlet.cx):

只要用以下类似的内容创建一个 ~/.ssh/config文件(*号将匹配所有的主机,如果你想,你可以严格的设置它为特定的目标主机,像kehlet.cx):

----------------

Host *

ServerAliveInterval 240

----------------

That's how often, in seconds, ssh will send a keepalive request (at the application layer) to the other end if the connection's been otherwise idle. 4 minutes should be good :-). 

这通常是以秒为单位,如果这个连接空闲,ssh将发送一个保持存活的请求(在应用层)给另一端。4分钟应该不错。

The Host line lets you pattern match your destinations. Minimal effort, no impact to your system (say, as you would have if you mucked with your system's default TCP keepalive settings), and it works like a charm.

Host行使你匹配你的目标主机。极小的努力,对你的系统毫无影响(即,如果你弄乱你的系统默认TCP保持存活时间设置的话,将影响你的系统),并且很有效。