Monday, July 18, 2011

Keeping SSH connections alive

To stop a SSH connection from timing out due to inactivity, we can configure the server or the client, to send a regular keep alive message.

For an explanation of the following ssh_config options see

Configuring the server

Edit /etc/ssh/ssh_config

sudo vim /etc/ssh/ssh_config

Add the following lines

ClientAliveInterval 15
ClientAliveCountMax 4

And restart the SSH server for the changes to take affect

/etc/init.d/ssh restart

Configuring the client

Edit vim ~/.ssh/config

sudo vim ~/.ssh/config

Add the following lines so connections to all Hosts have the same SSH options

Host *
    ServerAliveInterval 15
    ServerAliveCountMax 4

Security consideration

Keeping a SSH connection permanently open could be a security issue.

To explicitly close an inactive connection after 60 seconds set the ServerAliveInterval/ClientAliveInterval to 60, and the ServerAliveCountMax/ClientAliveCountMax to 0

*AliveInterval 60
*AliveCountMax 0

The above was tested using:

- Ubuntu 10.04 Lucid Lynx

- Ubuntu 11.04 Natty Narwhal

No comments:

Post a Comment