How to keep SSH session uninterrupted?

2023.09.14

How to keep SSH session uninterrupted?


The time between sending TCP keepalive probes on an idle TCP connection, even when no actual data is being transmitted. TCP keepalive probe is used to detect whether the remote host is still alive and responding

Hello everyone, I am Xianyu

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.

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

Original link: https://linuxiac.com/how-to-keep-ssh-session-alive/

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 there is no actual data transfer. 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:

A tcp_keepalive_time 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 is 9 and tcp_keepalive_intvl is 75, it means that the system will send 9 probe packets every 75 seconds (a total of 675 seconds), and then 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 the ~/.ssh/config file in the home directory (create it if it does not exist)

vim  ~/.ssh/config
  • 1.

The following is the configuration

Host *
ServerAliveInterval 120
ServerAliveCountMax 30
  • 1.
  • 2.
  • 3.

  • Host: The configuration only takes effect on the host 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

picturepicture

Then click [Terminal]

picturepicture

Linux server configuration

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

Modify the /etc/ssh/sshd_config file

vim /etc/ssh/sshd_config
  • 1.

TCPKeepAlive yes
ClientAliveInterval 120 
ClientAliveCountMax 30
  • 1.
  • 2.
  • 3.

  • 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