Autossh Startup Script for Multiple Tunnels

Categories: Bash, CentOS, SSH
Comments: 8 Comments
Published on: Monday, December 10th, 2012

When an encrypted VPN is not available, the next best solution is usually port-forwarding one or more port(s) through an SSH tunnel. The down-side of SSH is that by itself it cannot maintain a persistent connection — network issues may force the tunnel to stop responding, or even drop completely. Autossh is a small front-end for SSH that can monitor the connection, and restart the tunnel if it drops or stops responding. I found that the startup scripts available for autossh on the internet were a little too basic for my needs — I wanted autossh to start multiple connections, and to start/stop each one individually if I needed — so I wrote my own.

You’ll first need to install autossh — if you’re on an rpm-based distro, you can probably use yum install autossh.

The /etc/autossh/ directory contains the tunnel config file(s). You would typically have one configuration file per host. The filename can be whatever you like, but shorter filenames are probably best, since you can (optionally) use them on the command-line (for example /etc/init.d/autossh start dbhost1 dbhost2). If you don’t specify any filename(s) on the command-line, then all the config files are started, and all autossh processes are stopped.

Here’s an example host config file. You’ll have to make sure the LocalUser’s public SSH key has been added to the RemoteUser’s ~/.ssh/authorized_keys file., and you should try an su - {localuser} -c 'ssh -i {identityfile} -p {remoteport} {remoteuser}@{remotehost}' command first, before using the /etc/init.d/autossh script, just to make sure everything is working as it should. Remember that to listen on ports lower than 1024, either locally or remote, you will have to use root on that side.

The /etc/init.d/autossh script was written for CentOS, but should work fine — with little or no modifications — on most rpm-based distributions. You will probably want to add the script to your startup / shutdown process with a chkconfig --add autossh command, and you can view a short usage message by executing the script without any parameters.

You can download the autossh script here.

Did you find this post useful? Share it with your circles / friends, or leave a quick note bellow.

Thank you,

js.

  • jsmoriss-photo
  • Looking for a Sr UNIX Systems Administrator specializing in Systems Integration? I may be available for freelance or contract work, either remotely or on-site. You can contact me by email at jsm@surniaulula.com to discuss your needs, and how my skills might contribute to your success. The Contact Me, Services and Rates pages contain additional information, including my current location and availability schedule.
  • facebook linkedin
Categories: Bash, CentOS, SSH
Tags: , , , , , ,
Published on: Monday, December 10th, 2012
8 Comments - Leave a comment
  1. ChrisD says:

    Hi,

    I have the problem. Basically, I had 2 tunnels with different key file. Sometime I want to start with 2 tunnels at the same with this – /etc/init.d/autossh start, the tunnel was created with the wrong PortForward. For e.g., the tunnel 1 use Local PortForward of tunnel 2 which will not work. This made me to start them separately – /etc/init.d/autossh start config tunnel1 & /etc/init.d/autossh start config tunnel2. Is there anyway that I can start multiple tunnels at the same time. Thank you.

  2. Gbenga Adigun says:

    Hi Jean,

    First of all, many thanks for this tutorial…Simply wonderful.

    Bu ti have a slight issue, I want to go beyond 2 tunnels ( I need like 5 tunnels for the project am working on), but any time i create and test the 3rd tunnel ( i have only successfully created 2 tunnels), it gives the following error: “Connection closed by remote host”

    Output of SSH debugging:

    Please is there any consideration when you are creating more than 2 tunnels at the same time?

    Thanks

    • Provided you are not trying to redirect a low port (less than 1024), you should be able to redirect any number of ports. To redirect low port(s), you have to be a privileged user (like root).

      Your output shows an error with the /var/log/nagios/.ssh/id_rsa key, so you might want to look at that.

      BTW, I see you are connecting back to localhost. Don’t forget that you can’t redirect a port back onto itself on the same server / IP — to do that you’ll need different source and destination port numbers.

      js.

      • Gbenga Adigun says:

        Many thanks Jean!

        Am actually using the project to implement some Nagios checks via SSH (Currently don’t have the luxury of SNMP or NRPE). So I want to use autossh to create the SSH tunnels which I will reference in my Custom Plugins.

        Please can you explain this a little bit, probably an example command :

        “BTW, I see you are connecting back to localhost. Don’t forget that you can’t redirect a port back onto itself on the same server / IP — to do that you’ll need different source and destination port numbers.”

        Many thanks Bro!

  3. Thanks for posting this. For my purpose I only need a single user but this gives me all that I need to move forward with my project. This was very helpful. I didn’t find anything this comprehensive anywhere else.

  4. [...] comment below main article – The Daily Build) SSH Socks proxy with Chromium 18 (krenel.org) Autossh Startup Script for Multiple Tunnels (Surnia [...]

  5. Franck says:

    Great stuff !! Just used it for one of my crazy projects :) Thank you for spending the time and sharing it !!

  6. Elifarley says:
    Instead of individually configuring ports, you could create a SOCKS proxy on port 1080 (for instance) by using ssh’s D option like this: [...] -D 1080 RemoteUser@RemoteHost which gives you automatic port forwarding. In some circumstances, this may be better than individual port forwarding configuration.

Leave a comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>