When using SSH protocol, you might have noted that your sessions time out after a certain period of inactivity. While this is a sound security measure to prevent a bystander or any unauthorized user from taking over your session, it can be annoying and can pose challenges especially when you are running a lengthy task or operation. In this tutorial, we examine various tips that will help you prevent SSH from timing out.
Prevent SSH from timing out on the server-side
To keep the SSH session alive during a period of inactivity, the server needs to be configured to send a null packet to the client system every so often. This can easily be accomplished on the server-side as follows.
On the server, open the /etc/ssh/sshd_config file with your preferred text editor.
$ sudo vim /etc/ssh/ssh_config
Append this line at the end of the file.
ClientAliveInterval 60
The ClientAliveInterval parameter specifies the number of seconds that the server will wait before sending a null packet to the client to prevent the session from timing out. In this case, the server will wait every 60 seconds before sending the null packet.
Once you are done, save and exit the file, then restart the SSH service.
$ sudo systemctl restart ssh
And confirm that SSH is running.
$ sudo systemctl status ssh
Prevent SSH from timing out on the Client-side
On the client-side open the /etc/ssh/sshd_config configuration file and append the line below.
ServerAliveInterval 60
The ServerAliveInterval directive indicates the time in seconds that the client waits before sending a null packet over to the server to maintain the SSH session. In this example, the clients wait for 60 seconds before sending the null packet.
Save and exit the SSH configuration file. Again, restart the SSH protocol
$ sudo systemctl restart ssh
And verify that the service is running.
$ sudo systemctl status ssh
Use Putty client to keep SSH sessions alive
Putty is a popular free SSH client that allows you to connect to remote devices over a TCP/IP network. To keep the SSH sessions alive, launch Putty and click on the ‘Connection’ option on the left sidebar. Then check on the ‘Enable TCP keepalives (SO_KEEPALIVE option)’ option as indicated.
From here click on the ‘Connection’ option at the very top and enter the remote server’s IP and click ‘Open’.
Conclusion
That was a summary of various ways of keeping SSH sessions alive and preventing them from needlessly timing out. These are handy tips that you can use especially when there are no associated risks with someone taking over your SSH session when you are away.