Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (76 sloc) 4.43 KB
.. author:: Thomas Hoffmann <uberlab@emptyweb.de>
.. tag:: lang-c
.. tag:: console
.. tag:: automation

autossh

.. tag_list::

autossh is a program to start an SSH session and monitor it, restarting it as necessary should it die or stop passing traffic.

When following best practices laid out by Uberspace (every service should use its own Uberspace account), it can sometimes be necessary to connect two Uberspace hosts with each other privately. For example, this is the case if you are running an OpenLDAP installation on host A and want to use it for authentication for a Nextcloud installation on host B. autossh allows you to set up an automatically monitored tunnel between hosts to use for port forwarding.


Note

For this guide you should be familiar with the basic concepts of

License

autossh was written by Carson Harding and released as freeware. It uses no known licensing model.

Installation

Step 1

Download the latest version of autossh from the website http://www.harding.motd.ca/autossh/index.html, extract it and enter the directory:

[isabell@stardust ~] wget http://www.harding.motd.ca/autossh/autossh-99.9f.tgz
[isabell@stardust ~] tar zxf autossh-99.9f.tgz
[isabell@stardust ~] cd autossh-99.9f
[isabell@stardust autossh-99.9f]

Step 2

Run configure, make and make install (the --prefix options tells make to install to your home directory):

[isabell@stardust autossh-99.9f] ./configure --prefix=$HOME
[...]
[isabell@stardust autossh-99.9f] make
[...]
[isabell@stardust autossh-99.9f] make install
[...]

After running make install, which autossh should return ~/bin/autossh. If not, check the output of the respective commands for errors.

Configuration and Usage

Step 1

In order to use autossh comfortably, you need to define a connection in your ~/.ssh/config - for the sake of the example, let's assume the following connection in ~/.ssh/config:

Host service-tunnel
 HostName tsudrats.uberspace.de
 User llebasi
 IdentityFile ~/.ssh/servicetunnel
 LocalForward 9000 localhost:9000

This configuration will forward the local port 9000 to the remote port 9000. It could be opened manually by calling ssh service-tunnel.

To ensure that the remote host and it's key fingerprint are trusted, either add them manually to your ~/.ssh/known_hosts or connect to the host once, check the fingerprint and then answer yes when prompted.

[isabell@stardust ~] ssh llebasi@tsudrats.uberspace.de exit
The authenticity of host 'tsudrats.uberspace.de (185.26.156.254)' can't be established.
ED25519 key fingerprint is SHA256:taekOiAJ0efuyKpBtDKuE9c04LXJqJhVNcP1ltr798E.
ED25519 key fingerprint is MD5:dc:df:0a:f7:2c:cd:62:8a:7e:a3:d7:a1:43:56:0c:36:0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'tsudrats.uberspace.de,185.26.156.254' (ED25519) to the list of known hosts.

Note

This guide assumes that you have set up public key authentication (see IdentityFile parameter in ~/.ssh/config). This is required as otherwise you will always be prompted for your password when trying to open the tunnel. See :manual_anchor:`the Uberspace manual <basics-ssh.html#working-with-keys>` for setting up public key authentication if you don't know how.

Step 2

With the information from step 1, it is time to configure :manual:`supervisord <daemons-supervisord>` to handle our autossh process. Create the file ~/etc/services.d/autossh.ini with the following content:

[program:autossh]
command=autossh -M 0 service-tunnel -T -N
autostart=true
autorestart=false

This will make sure that autossh is automatically started if the host reboots but ignore termination of autossh (which will only happen if there are repeated errors with the connection). -M 0 will cause autossh not to send dummy data through the connection, -T -N will launch a non-interactive ssh connection.

If it's not in state RUNNING, something went wrong.

That's it, you have successfully configured an automatically launching port forwarding tunnel between to hosts!

.. author_list::
You can’t perform that action at this time.