Fix SSH客户端登录会话超时设置

时间:2022-07-24
本文章向大家介绍Fix SSH客户端登录会话超时设置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

通常默认公有云上的ECS远程连接,很容易断开,当你有什么事情被打断或者去操作别的机器同步做点其他事情,你会发现你SSH客户端登录窗口经常会断开掉,非常烦人,经常要重新登录。

如果用一些Windows下客户端软件比如XShell or CRT都会有超时时间和心跳检测次数设置,但是默认Mac下的终端 Or Linux下直接远程命令客户端是没有这个设置窗口的。

没事菜菜有办法,下面就教你正确姿势?

1. Client端设置

万事先从本身先思考,如果能够先搞自己,就别搞别人,因为搞C你很容易负责,搞S搞坏了,那有时候就会很蛋疼,因为一般S会跑一些业务,C坏了,大不了一起从头再来,至少自己做的饭在难吃,你也会美美的吃掉,S坏了有可能你就要付出惨痛的代价, 所以先搞C端是比较Nice的选择。

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

姿势2

$ vim ~/.ssh/config #添加如下内容
Host *
 ServerAliveInterval 60
 ServerAliveCountMax 30

姿势3

$ vim /etc/ssh/ssh_config # 在Host *下面添加:

Host *
       SendEnv LANG LC_*
       ServerAliveInterval 60
       ServerAliveCountMax 30

如果三个都设置了读取顺序是否是姿势1 ---> 姿势2 ---> 姿势3:

说明:

本地SSH Client每隔60s向Server端SSHD发送 keep-alive 包,如果发送30次, Server端还无回应则断开连接。

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连接。

更多参考man ssh_config:

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.