跳转至

阿里云服务器SSH短时间不操作自动断开问题

SSH连接的阿里云服务器,短时间不操作就会被自动断开,经常是Google搜索一个问题,再切回SSH准备操作的时候,发现SSH居然断开了。其实,通过修改服务器端SSH的设置,是可以避免这种情况的。

修改SSH设置

登录服务器后,修改sshd_config:

sudo vi /etc/ssh/sshd_config
修改其中的ClientAliveInterval的值,可能该项是被注释了,删除注释符,或者在末尾重新添加ClientAliveInterval。将该值设置为120,至于为啥是120呢?因为Google的服务器上面设置的就是120,抄的Google!

ClientAliveInterval 120

ClientAliveInterval: number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive).Setting a value of 0 (the default) will disable these features so your connection could drop if it is idle for too long.

大概意思是:ClientAliveInterval指定了服务器端向客户端请求消息的时间间隔(单位:秒), 默认是0,不发送。而ClientAliveInterval 120表示每2分钟发送一次,然后客户端响应,这样就保持长连接了。跟ClientAliveInterval配套的有个ClientAliveCountMax,至于这个ClientAliveCountMax,设置与否都可以,默认值是3。ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值,就自动断开,正常情况下,客户端不会不响应。

重新载入SSH配置

最后,重新载入该配置,使得修改立即生效:

service sshd restart