How To Stop SSH Session From Disconnecting In Linux
“My SSH session is closed after few minutes of inactivity, how can I prevent this?” – A friend of mine 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:
$ 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.
$ 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.
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, professional networks and support OSTechNix.
More good stuffs to come. Stay tuned!