AutoSSH: Automatically Reconnect SSH Tunnels
Software version | 1.4 |
Operating System | Debian 7 |
Website | AutoSSH Website |
Last Update | 20/03/2013 |
Others |
Introduction
If like me you need to maintain a permanent tunnel, and you want it to automatically reconnect in case of disconnection, you need to use a tool like AutoSSH. I’ll skip the details of its complete operation, but you should know that it can work in 2 ways:
- By establishing a loop with the remote server and regularly checking that data is flowing through it
- By querying a service on the remote machine at regular intervals
Installation
To install it, it’s simple:
aptitude install autossh
Configuration
Personally, I don’t use the method that regularly listens to a port; the other one is quite sufficient for me. So I’ll cover the basic method. Let’s imagine that I usually use an SSH connection by creating a socks and a local forward:
ssh -D12345 -N -f -L2222:10.0.0.1:22 deimos@server.deimos.fr
To use it with autossh, it’s simple, just use all the SSH options and paste them after the autossh command:
autossh -M 0 -D12345 -N -f -L2222:10.0.0.1:22 deimos@server.deimos.fr
The -M 0 option allows you to not use the monitoring option (solution #1). And that’s it, autossh manages this connection for you.
At Boot
If you want to enable it at boot, here’s an example to add to rc.local:
# /etc.rc.local
autossh -M 0 -q -f -N -oServerAliveInterval=60 -oServerAliveCountMax=3 -L2222:10.0.0.1:22 deimos@server.deimos.fr
The SSH options ServerAliveInterval and ServerAliveCountMax allow the connection to be cut if there’s a problem to force autossh to restart it.
References
Last updated 20 Mar 2013, 10:44 +0200.