Total Pageviews

Wednesday, 30 December 2015

SSH连接远程vps长时间不操作而不掉线的小技巧







用*nix的Terminal远程连接VPS的时候,长时间不操作,就会掉,这时候可以通过修改ssh_config,来使得长时间不操作,也不会掉线。


Linux桌面系统下,可以通过修改 /etc/ssh/ssh_config文件(注意:不是修改/etc/ssh/sshd_config文件),添加

ServerAliveInterval 300
ServerAliveCountMax 5
而OS X桌面系统的此文件为/etc/ssh_config(注意:不是修改/etc/sshd_config文件),修改内容同上。
添加的内容意思为,每隔300秒向服务器请求一次心跳,超过5次没响应就认为掉线。

注:以上修改均在客户端机器修改,而不是在VPS上修改.

参考:https://patrickmn.com/aside/how-to-keep-alive-ssh-sessions/ (此文最详细。它还顺便提到了服务器上如何修改,服务器上是修改sshd_config文件:
You can also make your OpenSSH server keep alive all connections with clients by adding the following to /etc/ssh/sshd_config:
ClientAliveInterval 300
ClientAliveCountMax 5
These settings will make the SSH client or server send a null packet to the other side every 300 seconds (5 minutes), and give up if it doesn’t receive any response after 5 tries。建议在服务器上也进行相应的修改
我测试了,两边都修改后,好像真的长时间不操作而不掉线了。

以及在windows桌面系统的putty中如何设置:

On PuTTY

In your session properties, go to Connection and under Sending of null packets to keep session active, set Seconds between keepalives (0 to turn off) to e.g. 300(5 minutes). )
https://www.a2hosting.com/kb/getting-started-guide/accessing-your-account/keeping-ssh-connections-alive
http://www.howtogeek.com/howto/linux/keep-your-linux-ssh-session-from-disconnecting/
https://www.maketecheasier.com/keep-ssh-connections-alive-in-linux/