Help Center> Elastic Cloud Server> Troubleshooting> SSH Connection Issues> How Do I Keep an SSH Session Alive?
Updated on 2022-07-15 GMT+08:00

How Do I Keep an SSH Session Alive?

Scenarios

This section describes how to keep an SSH session being connected to an ECS running CentOS 6.5.

The method described here applies to ECSs running CentOS or EulerOS.

Restarting sshd in the following procedure will disconnect SSH sessions.

Procedure

Modify the /etc/ssh/sshd_config file to keep an SSH session alive.

  1. Add the following configuration items to the /etc/ssh/sshd_config file:
    ClientAliveInterval 600      
    ClientAliveCountMax 10

    ClientAliveInterval 600 indicates that the ECS sends a request to the SSH client every 600 seconds to keep the SSH connection alive.

    ClientAliveCountMax 10 indicates that the ECS automatically disconnects from the client if the client does not respond for 10 times after the ECS sends requests.

    In such a case, the unresponsive SSH client will be disconnected after approximately 6,000 seconds (600 x 10).

    ClientAliveInterval is a timeout interval in seconds. If the ECS receives no data from the client within the timeout interval, the ECS will send a message through the encrypted channel to request a response from the client. The default value is 0, indicating that these messages will not be sent to the client. The ClientAliveInterval option applies to SSH protocol version 2 only.

    ClientAliveCountMax specifies the number of client active messages which may be sent when the sshd service does not receive any messages back from the client. If this threshold is reached while client alive messages are being sent, sshd will disconnect the client, terminating the session.

    The use of client alive messages is very different from TCPKeepAlive. The client alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable.

  2. Run the following command to restart sshd for the configuration to take effect:
    • CentOS 6

      # service sshd restart

    • CentOS 7 or EulerOS

      # systemctl restart sshd