ssh 设置超时时间



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

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


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

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



在命令参数里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.


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.


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.


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


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 *

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


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 :-).


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.


Leave a Reply

Your email address will not be published. Required fields are marked *