How to keep SSH session uninterrupted?

I don’t know if any of you have ever encountered the following situation:

After logging in to the Linux server using a terminal (XShell, secureCRT or MobaXterm, etc.), if there is no interaction for a period of time, the SSH session will be disconnected.

image

If some non-background commands are being executed, the disconnection of the SSH session may cause these commands to be interrupted, resulting in the task not being completed.

So how to keep the SSH session from being interrupted? let's take a look

Why does SSH close the connection?

The short answer is that it all comes down to TCP timeouts.

TCP timeout is the amount of time a TCP connection or network operation waits for a response before considering the process to have failed.

In Linux, the TCP timeout setting determines how long a TCP connection or operation should wait before packet loss or the connection becomes unresponsive.

TCP timeout mechanism ensures the reliability and efficiency of network communication

While maintaining an SSH session, there are three key system parameters we need to pay attention to:

  • tcp_keepalive_time : The interval between sending TCP keepalive probes on idle TCP connections, even when no actual data is being transferred. TCP keepalive probe is used to detect whether the remote host is still alive and responding
  • tcp_keepalive_probes : TCP keepalive probe, a data packet sent by the TCP end, used to check the health and responsiveness of the remote end in an idle connection. Helps detect if a remote host has become inaccessible or if the connection has been lost due to network issues
  • tcp_keepalive_intvl : Controls the time interval for sending keepalive probes for idle TCP connections

We can view the values ​​of the above three parameters through the following command:

[root@localhost ~]# cat /proc/sys/net/ipv4/tcp_keepalive_time
600

[root@localhost ~]# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9

[root@localhost ~]# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75

tcp_keepalive_time A value of 600 means that the TCP connection time is maintained for 600s or 10 minutes, but this does not mean that our SSH session will really be maintained for 10 minutes.

Because  tcp_keepalive_probes 9 and  tcp_keepalive_intvl 75 means that the system will send 9 probe packets every 75s (a total of 675 s), after which the session will be considered failed and closed.

That is, after 675s, the SSH session will terminate with no activity, i.e. no input in the terminal

How to maintain an SSH session

Maintaining an SSH session is a process involving client and server configuration

Linux client configuration

For Linux client, we modify  ~/.ssh/config the file in the home directory (create it if it does not exist)

vim  ~/.ssh/config

The following is the configuration

Host *
ServerAliveInterval 120
ServerAliveCountMax 30
  • Host: The configuration only takes effect on the hosts listed after the "Host" keyword. They apply to all hosts due to the use of wildcards (*)
  • ServerAliveInterval: Set the timeout interval (in seconds). If no data is received from the server, SSH will send a message through the encrypted channel to request a response from the server. The default value is 0, which means these messages will not be sent to the server
  • ServerAliveCountMax: Set the number of keepalive messages sent to the server when SSH does not receive any messages. If this threshold is reached, SSH will disconnect from the server and terminate the session (default value is 3)

Indicates that the client sends a keepalive message to the server every 120s, a total of 30 times, which is 120 * 30 = 3600 s (one hour). During this hour, the SSH session will be maintained.

Windows client configuration

For Windows, we generally use the terminal to access the server

Take secureCRT as an example

Options->Session Options

image


Then click [Terminal]
 

image


Linux server configuration

The above describes the client-side configuration. Next, we introduce the server-side configuration.

Modify  /etc/ssh/sshd_config files

vim /etc/ssh/sshd_config
TCPKeepAlive yes
ClientAliveInterval 120 
ClientAliveCountMax 30
  • TCPKeepAlive: Whether TCP keepalive information should be sent to the client
  • ClientAliveInterval: Set the timeout interval (in seconds). If no data is received from the client, SSH will send a message through the encrypted channel to request a response from the client. The default value is 0, which means these messages will not be sent to the client
  • ClientAliveCountMax: Set the number of keepalive messages sent to the client when SSH does not receive any messages. If this threshold is reached, SSH will disconnect from the client and terminate the session (default value is 3)

As with the Linux client configuration mentioned earlier, the server will maintain the SSH session for one hour (120 * 30 = 3600s)

Restart the SSH service after configuration

systemctl restart sshd

Guess you like

Origin blog.csdn.net/qq_41221596/article/details/132920516