Hugo Martins

Thoughts on programming, engineering and curly brackets.

github linkedin
Keeping an SSH Connection Alive from a Client
Apr 5, 2018
One minute read

Sometimes you run scripts that take a lot of time to finish, through SSH. Also, the server, sometimes, closes your connection. This is bound to create issues. I have recently faced similar issues and went searching for a possible solution. Turns out that there’s a simple solution for this. From ssh_config [1]:

ServerAliveInterval Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server. This option applies to protocol version 2 only.

Open ~/.ssh/config on an editor and type:

    ServerAliveInterval 300

This will send a message to the server every 300 seconds (5 minutes) which will keep your connection alive. You can tweak that value to something that works out best for each use case. If you want to apply this to all hosts just replace with *.

This is a pretty simple way of avoiding connections closed, when using nohup or & is not an option.



Back to posts