How to prevent SSH from disconnecting sessions

Introduction

If you work with SSH, you already know that, after few minutes of inactivity, a session would be closed automatically, for security reasons. In fact, you could have forgotten to shut it down, and someone could take control of your system. But, if you think it’s not your problem, you can change this behaviour in your GNU/Linux configuration. The following commands must be executed from SSH client.

How to stop SSH from disconnecting

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

First of all, open your text editor and modify your current user config file, which is located at:

~/.ssh/config

Add the following lines:

Host *
 ServerAliveInterval 60

ensuring that the second line will start with a space.

The first line, tells SSH to apply this configuration for all remote hosts. Of course, you can specify just one of them, changing ‘*’ with the desired host.

After you did this, you need to apply the above settings:

sudo source ~/.ssh/config

To apply this settings globally, add or modify the following line in /etc/ssh/ssh_config file.

ServerAliveInterval 60

Save and close the file.

In this way, your SSH session will not be closed for inactivity.

Conclusion

Remember that the security of your system, expecially in server case, is not secondary, and that is all in your hand. Changing the behaviour of programs like SSH must be done only if you know exactly what you want, and after being sure that this will not put you in some problem.