### 1. Client端设置

SSH Client会从以下途径获取配置参数:

1. SSH命令行参数；
2. 用户配置文件 (~/.ssh/config);
3. 系统配置文件 (/etc/ssh/ssh_config)。

#### 姿势1

ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=30 root@10.0.1.25 -p22


$vim ~/.ssh/config #添加如下内容 Host * ServerAliveInterval 60 ServerAliveCountMax 30  #### 姿势3 $ vim /etc/ssh/ssh_config # 在Host *下面添加：

Host *
SendEnv LANG LC_*
ServerAliveInterval 60
ServerAliveCountMax 30


### 2. Server端设置

SSH Server在这里就是服务器端的sshd服务（类Unix的系统都有），可以通过修改sshd的配置文件来改变SSH Session的超时时间：

vim /etc/ssh/sshd_config


ClientAliveInterval 60
ClientAliveCountMax 30


• ClientAliveInterval: 这个其实就是SSH Server与Client的心跳超时时间，也就是说，当客户端没有指令过来，Server间隔ClientAliveInterval的时间（单位秒）会发一个空包到Client来维持心跳，60表示每分钟发送一次，然后客户端响应，这样就保持长连接了保证Session有效, 默认是0, 不发送;
• ClientAliveCountMax：当心跳包发送失败时重试的次数，比如现在我们设置成了30，如果Server向Client连续发30次心跳包都失败了，就会断开这个session连接。

ServerAliveCountMax Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. It is important to note that the use of server alive messages is very different from TCPKeepAlive (below). The server alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valuable when the client or server depend on knowing when a connection has become inactive.

The default value is 3. If, for example, ServerAliveInterval (see below) is set to 15 and ServerAliveCountMax is left at the default, if the server becomes unresponsive, ssh will disconnect after approximately 45 seconds. This option applies to protocol version 2 only; in protocol version 1 there is no mechanism to request a response from the server to the server alive messages, so disconnection is the responsibility of the TCP stack.

ServerAliveInterval Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server, or 300 if the BatchMode option is set. This option applies to protocol version 2 only. ProtocolKeepAlives and SetupTimeOut are Debian-specific compatibility aliases for this option.


https://15tar.com/linux/2017/07/31/ssh-session-timeout.html

http://einverne.github.io/post/2017/05/ssh-keep-alive.html

https://daemon369.github.io/ssh/2015/03/21/using-ssh-config-file

https://www.jianshu.com/p/1246cfdbe460

https://www.jianshu.com/p/92d60c6c92ef

https://www.hi-linux.com/posts/14346.html

https://stackoverflow.com/questions/25084288/keep-ssh-session-alive