高版本的Linux自带的OpenSSH在使用的时候,几分钟不操作的话就会自动断开连接,这是出于安全的考虑,但是对于需要长时间使用的用户来说很麻烦,每次都要重新连接。解决方法如下:
将/etc/ssh/sshd_config中的“ClientAliveInterval”设置取消注释,并把0改为一个较小的数值即可。具体说明参看下面的解释:
OpenSSH ClientAliveInterval
Using an OpenSSH server’s ClientAliveInterval, it is possible for the ssh server to send periodic “keep alive” messages to the ssh client, keeping the connection open indefinitely. This is useful when a firewall or other packet filtering device drops idle connections after a certain period of time. Note that this is different from the KeepAlive directive in ssh_config.
From the sshd_config manpage:
ClientAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the client, sshd will send a message through
the encrypted channel to request a response from the client. The
default is 0, indicating that these messages will not be sent to
the client. This option applies to protocol version 2 only.
Example (send “keep alive” messages every 5 minutes) on Red Hat Linux:
1. Add ClientAliveInterval 300 to /etc/ssh/sshd_config
2. Reload the sshd server configuration with /sbin/service sshd reload
Note: you may want to configure the ClientAliveCountMax value in sshd_config to set the number of times that “keep alive” messages are sent. If ClientAliveCountMax number of “keep alive” messages are not acknowledged by the ssh client, the connection is terminated by the ssh server. The default value of 3 should be sufficient for most users.