Typically, if you are logged into SSH and keep it idle for sometime, then your SSH connection is automatically terminated after timeout period. In this case, you need to create a new session by connecting to your SSH server once again. As a result, you may lose some of your work. This can be annoying if you want to keep your SSH session alive for a long time. So it is advisable to increase SSH connection timeout. In this article, we will look at how to increase SSH timeout. You can use these steps on almost every Linux distribution.
How to Increase SSH Connection Timeout
Here are the steps to increase SSH timeout. You can increase SSH connection from server side as well as from client side. In this article, we will look at both these approaches.
Server Side
1. Open SSH Configuration file
Open terminal and run the following command to open SSH configuration file.
$ sudo vi /etc/ssh/sshd_config
2. Update SSH parameters
Look for the following line.
#ClientAliveInterval #ClientAliveCountMax
SSH server keeps a session alive by sending null packets to client at regular intervals specified by ClientAliveInterval parameter. As long as the client responds back with another null packet, SSH server will keep the session alive. You need to specify the number of seconds SSH server should wait before sending a null packet to client, as ClientAliveInterval.
ClientAliveCountMax specifies the number of attempts to be made by SSH server in sending null packets to client and waiting for client to respond. If the client does not respond even after the limit set by ClientAliveCountMax parameter, then SSH server will close the session.
Using the above parameters, you can easily calculate the timeout value as
ClientAliveInterval * ClientAliveCountMax
Let us say you want SSH server to wait for 600 seconds before sending null packet and make 5 attempts before closing the session, then here is how you should configure SSH timeout parameters. Please make sure to remove # sign at the beginning of both the parameters.
ClientAliveInterval 600 ClientAliveCountMax 5
3. Restart SSH Server
Once you have updated SSH timeout parameters, restart SSH server with the following command.
$ sudo systemctl reload sshd
That’s it. In this article, we have looked at how to increase SSH timeout value in SSH server to keep the session alive for longer duration.
Client Side
You may also disable timeout from SSH client end. In this case, open the SSH config file in the computer/workstation/laptop you are connecting from.
$ sudo vi /etc/ssh/ssh_config
Add the following line to keep SSH session alive.
ServerAliveInterval 60
In this case, the SSH session will send a keep-alive message to server every 60 seconds so that the SSH server does not close the session.
It is important to keep the SSH session alive if you need to work on other things at the same time, while keeping the session alive to get back once in a while. This is especially useful in case you are coding or writing commands that require you to read up stuff from other sites and documents.
Also read:
How to Run Sudo Command Without Password
How to Save Dictionary to File in Python
How to Kill Unresponsive Process in Linux
How to Lock User After Failed Login Attempts
How to Find Failed SSH Login Attempts
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.