Tip: Prevent SSH Session from Disconnecting

The network connection at office keeps my SSH session from running smoothly. Well, it means I keep on being disconnected from the server when my SSH session turns idle for a certain period of time. It gets annoying especially if I am in the middle of a script running silently.

One alternative I have written before here is using screen command or by editing the SSH config file to prevent SSH from disconnecting its connection.

The process is very simple: your SSH session consistently sends packets over the connection to let the remote computer know that the session is still active and there is no need for termination. This is what they call Keep Alive packets. For me, it means Keep My Sanity packets.

Anyway here is what you need to do. Edit your ssh_config file, usually in /etc/ssh/ directory:

# vi /etc/ssh/ssh_config

And put this line in the file:

ServerAliveInterval 60

Save and exit.

Open the ~/.ssh/config file (or create it if not present) and put this line in it:

Host *
ServerAliveInterval 60

Don’t forget the indent at the second line. Save and exit.

Lastly, reload your new SSH config file by doing:

# /etc/init.d/sshd reload

This should do the trick of fooling the remote server into thinking that your SSH connection is active, even if it is not.

Did this tip worked for you? Let me know in the comments section.