×


Prevent SSH From Timing Out - Best Method ?

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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related SSH queries on Debian Linux System.

In this context, we shall look into tips that will help you prevent SSH from timing out.


How to 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


How to 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 our case, 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


How to 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'.


[Need assistance in fixing Linux system issues? We can help you. ]


Conclusion

This article covers different 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. SSH sessions will timeout and the client will automatically be disconnected from the server after being idle or inactive for a while. 


To Avoid SSH timeout from the server:

1. Edit SSHd configuration file using your favorite editor,

$ sudo vi /etc/ssh/sshd_config

2. Set these options as the followings:

TCPKeepAlive no 
ClientAliveInterval 30
ClientAliveCountMax 240

Here, the server will not send the TCP alive packet to check if the client's connection is working, yet will still send the encrypted alive message every 30 seconds. It will only disconnect after at least 2 hours of inactivity.