autossh – Automatically restart SSH sessions and tunnels

autossh is a program to start an instance of ssh and monitor it, restarting it as necessary should it die or stop passing traffic. The idea is from rstunnel (Reliable SSH Tunnel), but implemented in C. Connection monitoring is done using a loop of port forwardings. It backs off on the rate of connection attempts when experiencing rapid failures such as connection refused.

Install autossh in debian

#aptitude install autossh

This will complete the installation

Usage

autossh -M [port][:echo_port] [-f] [SSH OPTIONS]

-M

[:echo_port], to specify the base monitoring port to use, or
alternatively, to specify the monitoring port and echo service
port to use.When no echo service port is specified, this port and the port
immediately above it (port# + 1) should be something nothing
else is using. autossh will send test data on the base monitoring
port, and receive it back on the port above. For example, if you
specify “-M 20000”, autossh will set up forwards so that it can
send data on port 20000 and receive it back on 20001.

Alternatively a port for a remote echo service may be
specified. This should be port 7 if you wish to use the
standard inetd echo service. When an echo port is specified,
only the specified monitor port is used, and it carries the
monitor message in both directions.

Many people disable the echo service, or even disable inetd,
so check that this service is available on the remote
machine. Some operating systems allow one to specify that the
service only listen on the localhost (loopback interface),
which would suffice for this use.

The echo service may also be something more complicated:
perhaps a daemon that monitors a group of ssh tunnels.

-M 0 will turn the monitoring off, and autossh will only
restart ssh on ssh exit.

For example, if you are using a recent version of OpenSSH, you
may wish to explore using the ServerAliveInterval and
ServerAliveCountMax options to have the SSH client exit if it
finds itself no longer connected to the server. In many ways
this may be a better solution than the monitoring port.

-f Causes autossh to drop to the background before running ssh. The
-f flag is stripped from arguments passed to ssh. Note that there
is a crucial a difference between the -f with autossh, and -f
with ssh: when used with autossh, ssh will be *unable* to ask for
passwords or passphrases.

-V to have autossh display its version and exit.

Autossh example

autossh -M 5122 -N -R 5022:localhost:22 rex

Explanation:

Forward port 5022 on rex (the remote machine) to port 22 on localhost (the local machine).

Use port 5122 and 5123 will be used for connection monitoring.

Sponsored Link

2 thoughts on “autossh – Automatically restart SSH sessions and tunnels

Leave a comment

Your email address will not be published. Required fields are marked *