How to keep ssh sessions alive


#1

There are two ways to keep ssh sessions alive.

Client-Side:

On the client side, it is possible to configure your ssh client to send keep alive packets to server. It can be configured by system wide through /etc/ssh/ssh_config or user specific ~/.ssh/config files.

Host *
ServerAliveInterval 60
ServerAliveCountMax 3

With above configuration, your ssh client will send keep alive packets to server on every 60 seconds and if it couldn’t receive any response for sequential 3 keep alive packets, connection will be closed.

Server-Side:

It is also possible to change ssh keep alive packets behaviour on the server side. By this way, you can keep your clients’ sessions alive without need to make special configuration on the client side.

Append following lines to your /etc/ssh/sshd_config file:

ClientAliveInterval 180
ClientAliveCountMax 2

and restart your ssh service:

$ sudo service ssh restart

After this configuration your sshd server will start to send keep alive packets to their clients on every 180 seconds and if it couldn’t receive answer on 2 sequential packets, client connection will be closed.