How to Increase SSH Connection Timeout in Linux


Secure Shell (SSH) is a widely used protocol for accessing remote systems securely. It is used to establish a secure and encrypted connection between two computers over an insecure network. When establishing an SSH connection, there is a timeout value that determines how long the connection can be idle before it is closed. This is called the SSH connection timeout.

The SSH connection timeout is essential to prevent unauthorized access and to reduce the risk of security breaches. However, it can be a hassle when you need to work on a remote system for an extended period. In this article, we will discuss how to increase the SSH connection timeout in Linux.

Understanding SSH Connection Timeout

The SSH connection timeout is the period of inactivity after which the SSH server will terminate the connection. By default, most SSH servers have a connection timeout of 15 minutes. This means that if you do not interact with the remote system for more than 15 minutes, the connection will be terminated.

This timeout is essential to prevent unauthorized access and to reduce the risk of security breaches. However, in some situations, it may be necessary to increase the timeout period. For example, when working on a remote system that requires long periods of inactivity, such as when downloading large files or running long processes.

Increasing SSH Connection Timeout

To increase the SSH connection timeout in Linux, you need to modify the sshd_config file on the remote system. This file contains the configuration settings for the SSH server. Here are the steps to increase the SSH connection timeout −

Step 1: Connect to the Remote System

To modify the sshd_config file, you need to log in to the remote system. You can do this using the SSH client on your local system. Open a terminal and enter the following command −

ssh username@remote-system-ip

Replace "username" with the username you use to log in to the remote system and "remote-system-ip" with the IP address of the remote system./

Step 2: Modify the sshd_config File

Once you have logged in to the remote system, you need to modify the sshd_config file. Open the file using a text editor such as nano or vi. For example −

sudo nano /etc/ssh/sshd_config

Step 3: Locate the ClientAliveInterval and ClientAliveCountMax Parameters

In the sshd_config file, locate the following parameters −

ClientAliveInterval
ClientAliveCountMax

The ClientAliveInterval parameter specifies the time interval in seconds after which the server will send a keep-alive message to the client. The ClientAliveCountMax parameter specifies the number of keep-alive messages that can be sent without a response from the client before the server terminates the connection.

Step 4: Increase the ClientAliveInterval and ClientAliveCountMax Values

To increase the SSH connection timeout, you need to increase the values of the ClientAliveInterval and ClientAliveCountMax parameters. For example, to increase the timeout to 60 minutes, set the values as follows −

ClientAliveInterval 600
ClientAliveCountMax 6

The ClientAliveInterval is set to 600 seconds (60 minutes), and the ClientAliveCountMax is set to 6. This means that the server will send a keep-alive message every 10 minutes, and the connection will be terminated after one hour of inactivity.

Step 5: Save and Close the sshd_config File

Once you have modified the sshd_config file, save the changes and close the file.

Step 6: Restart the SSH server

To apply the changes, you need to restart the SSH server on the remote system. You can do this by entering the following command −

sudo service ssh restart

Testing the SSH Connection Timeout

To ensure that the changes you made to the SSH connection timeout are working, you need to test the SSH connection. You can do this by establishing an SSH connection to the remote system and leaving it idle for the specified timeout period.

For example, if you have set the SSH connection timeout to 60 minutes, establish an SSH connection to the remote system and leave it idle for one hour. If the connection is still active after one hour, the changes you made to the SSH connection timeout are working correctly.

Additional Tips and Considerations

Here are some additional tips and considerations to keep in mind when increasing the SSH connection timeout in Linux −

Be cautious when increasing the timeout value. Increasing the SSH connection timeout can increase the risk of security breaches, especially if the remote system is accessible from the internet. It is recommended to only increase the timeout value when necessary and to limit the increase to a reasonable value.

Test the connection timeout value. After making changes to the SSH connection timeout, it's important to test that the new value is working as expected. Establish an SSH connection to the remote system and leave it idle for the specified timeout period to confirm that the connection is terminated correctly.

Consider using other methods to keep the SSH connection active. If you don't want to increase the SSH connection timeout value, you can use other methods to keep the SSH connection active. For example, you can run a command in the background that generates output every few minutes or use an SSH tunnel.

Make sure to follow best practices for SSH security. SSH is a powerful tool for remote access, but it can also be a potential security risk if not properly configured. To ensure the security of your SSH connections, make sure to follow best practices such as disabling root login, using strong passwords, and limiting access to trusted users.

Alternative Methods to Increase SSH Connection Timeout

Apart from modifying the sshd_config file, there are other methods to increase the SSH connection timeout. Here are some of the alternatives −

Use the ServerAliveInterval and ServerAliveCountMax Options

You can use the ServerAliveInterval and ServerAliveCountMax options in your SSH client configuration file to keep the SSH connection active. These options work in a similar way to the ClientAliveInterval and ClientAliveCountMax parameters in the sshd_config file. To use these options, add the following lines to your SSH client configuration file (usually located at ~/.ssh/config) −

Host *
   ServerAliveInterval 600
   ServerAliveCountMax 6

In this example, the ServerAliveInterval is set to 600 seconds (10 minutes), and the ServerAliveCountMax is set to 6. This means that the client will send a keep-alive message to the server every 10 minutes, and the connection will be terminated after one hour of inactivity.

Use the TCPKeepAlive Option

The TCPKeepAlive option can also be used to keep the SSH connection active. This option sends TCP keep-alive packets to the server at regular intervals to check if the connection is still alive. To use this option, add the following line to the sshd_config file on the remote system −

TCPKeepAlive yes

This option is enabled by default on most SSH servers and can be used in combination with the ClientAliveInterval and ClientAliveCountMax parameters to increase the SSH connection timeout.

Use the SSH ControlMaster Option

The SSH ControlMaster option allows you to reuse an existing SSH connection for subsequent connections to the same server. This can help reduce the time it takes to establish a new SSH connection and keep the connection active. To use this option, add the following lines to your SSH client configuration file −

Host *
   ControlMaster auto
   ControlPath ~/.ssh/%r@%h:%p
   ControlPersist 600

In this example, the ControlMaster option is set to auto, which means that SSH will automatically set up a master connection when establishing a new SSH connection. The ControlPath option specifies the path to the control socket used for communication between the master and slave connections, and the ControlPersist option sets the time (in seconds) to keep the master connection alive after the last slave connection is closed.

Conclusion

Increasing the SSH connection timeout in Linux is a simple process that can be accomplished by modifying the sshd_config file on the remote system. By increasing the timeout, you can work on a remote system for an extended period without worrying about the connection being terminated due to inactivity. However, it is important to keep in mind that increasing the SSH connection timeout may increase the risk of security breaches, and you should only do so when necessary.

Updated on: 28-Apr-2023

35K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements