How To Stop SSH Session From Disconnecting In Linux

Stop SSH Session From Disconnecting In Linux

My SSH session is closed after few minutes of inactivity, how do I prevent this? One of my friend has asked this question. It’s easy! As you might know already, SSH sessions will be terminated after 10 minutes of inactivity for security reasons. So, it prevents other users to access your server in case you left it connected by mistake. However, you can change this if you want to. Here is a simple solution to stop SSH session from disconnecting automatically in Linux. This method will work on most Linux systems.

Stop SSH Session From Disconnecting In Linux

The following steps needs to be performed in your SSH client, not in the remote server.

To configure the current user, edit SSH config file:

sudo nano ~/.ssh/config

Add the following lines:

Host *
 ServerAliveInterval 60

Please ensure you indent the second line with a space. Let me explain what these lines do. Once you added these lines in your SSH client system, it will send a packet called no-op (No Operation) to your Remote system. The no-op packet will inform the remote system “Nothing to do”. Also it tells the SSH client is still connected with the remote system, hence do not close the TCP connection and log you out.

Here “Host *” indicates this configuration is applicable for all remote hosts. “ServerAliveInterval 60” indicates the number of seconds to wait to send a no-op packet.

To configure this to particular host, you need add the remote host’s name after “Host” line as shown like below.

Host ostechnix
 ServerAliveInterval 60

The above configuration only prevents the SSH session of a remote host called “ostechnix” from being disconnected.

Run the following command to apply the settings.

sudo source ~/.ssh/config

To apply this settings for all users (globally) in your system, add or modify the following line in /etc/ssh/ssh_config file.

ServerAliveInterval 60

Save and close the file.

Now the SSH session will not terminate after a particular period of time.

That’s it. If you find this guide useful, please share it on your social networks and support OSTechNix.


Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

1 Response

  1. David Okwii says:

    Cool. Been looking for this. I’ve implemented, will test and see how my sessions go.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.