How to Stop an SSH Session From Disconnecting in Linux

This guide will show you how to edit the SSH daemon config file to stop the server from disconnecting your sessions when you’re idle.

Anyone who has worked with a remote Linux server using SSH must have faced the problem of having the SSH session drop dead when you head out to get a cup of coffee. Well, here’s a solution to help you configure your SSH daemon on the Linux server to stop it from disconnecting. What happens is that if you leave your SSH connection on and don’t use it for some time the SSH server goes into an idle mode and kills the session. This is really a security measure which can sometimes be a little annoying.

To set this up log on to your remote Linux server using SSH. Open the SSH configuration file:

# vim /etc/ssh/ssh_config

Add the following line at the end of the file:

ServerAliveInterval 60

Save the file and quit the text editor (get help with vim here). Now, similarly, open the file ~/.ssh/config. Create the file if it does not already exist. Add the following lines:

Host *
 ServerAliveInterval 60

Note: you need to indent the second line by entering a space at the beginning of the line.

You’re all done now. Time to restart your SSH server. Run the following command:

# /etc/init.d/sshd restart

This should take a second or two. Then you should be back in business. What we have done here is that we have pretty much fooled the SSH server into thinking that your SSH session is active even when it’s not. Now you can peacefully take a short coffee break without losing your SSH connection.

If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for

Home » Linux » How to Stop an SSH Session From Disconnecting in Linux

Leave a Comment

Your email address will not be published.