Reverse SSH tunnel and autossh

Charly's Column – Reverse SSH Tunnel

Article from Issue 215/2018
Author(s):

This month, Charly Kühnast draws attention to a widely unknown weather phenomenon: The instability of rarely used tunnels leading to a Raspberry Pi. Read on for greater insights.

Recording environmental data is one of my hobbies; I already reported about my magnificent dust sensor [1]. I also own a small weather station: the popular WH1080, which is sold under names like Fine Offset, Nextrend, Froggit, or TFA Nexus. It includes various external sensors, as well as an indoor base station.

I continually extract the measurement data via the USB port of the base system – any Linux machine would be capable of it, but my old Raspberry Pi 2B is the perfect choice. I use RRDtool to write the data to a round-robin database and conjure up colorful graphs for a web server (Figure 1.)

Figure 1: Charly's Raspberry Pi server records the weather station data with RRDtool.

I had to come up with something to publish the data on the web. My ISP uses Dual-Stack Lite [2] and doesn't even offer static IPs for an extra charge – and, I have an aversion to dynamic DNS services.

A reverse SSH tunnel should fix it. The Raspberry Pi opens a connection from the inside through the NAT to a server outside, which I rented for a little money. (Performance doesn't matter; it's all about having a static IP address.) This SSH connection creates a direct tunnel between port 80 of the web server and port 80 of the Raspberry Pi. This was fine as a proof of concept; later, I converted it to HTTPS with a certificate from Let's Encrypt.

The syntax for tunneling is simple. On the Raspberry Pi, you enter:

ssh -R <Webserver>:80:localhost:80<User>@<Webserver>

The tunnel is set up; I access the web server and see Connection refused. So what went wrong? After five minutes with a search engine that I really don't trust, I found this out: On the server, I have to add a GatewayPorts clientspecified line to /etc/ssh/sshd_config. Without this, the port is only bound to localhost.

Not Enough Action in the Tunnel

Unfortunately SSH disconnects after a period of inaction; because my weather server is not quite as popular as the one run by the Met Office, this happens quite soon. I can set the timeouts, but sooner or later the tunnel always breaks down, and it does not reopen. I succeeded in eliminating this annoyance with autossh [3]. The tool monitors the connection and restarts crashed tunnels. The syntax for doing this is:

autossh -M 9999 -N -R <Webserver>:80:localhost:80 <User>@<Webserver>

The -M switch is the monitoring connection. I chose the port number arbitrarily; it and the next one (i.e., 10000) both need to be free.

Caution: The machine (here, the Raspberry Pi) must not have access to the internal LAN at the same time, because if an attacker took over, they would be handed my entire home network on a silver platter. In my environment, the Raspberry Pi is connected to special DMZ ports on the firewall, which isolates it from the LAN. So let's see: 21°C – time to go out into the garden and see if the watering Raspberry Pi is doing its job properly.

Infos

  1. "Charly's Column: Particulate Matter Measurement with the Raspberry Pi" by Charly Kühnast, Linux Pro Magazine, issue 213, August 2018, p. 54, http://www.linuxpromagazine.com/Issues/2018/213/Breathe-deeply/(language)/eng-US
  2. Dual-Stack Lite: https://en.wikipedia.org/wiki/IPv6_transition_mechanism#Dual-Stack_Lite_(DS-Lite)
  3. autossh: https://linux.die.net/man/1/autossh

The Author

Charly Kühnast manages Unix systems in the data center in the Lower Rhine region of Germany. His responsibilities include ensuring the security and availability of firewalls and the DMZ.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Charly's Column: Corkscrew

    Sys admin columnist Charly never takes a vacation from the Internet. A beach bar with WiFi is quickly found, but it runs a forced proxy, which thinks that the SSH port (22) is in league with the devil and blocks the connection. Time to drill a tunnel.

  • The sys admin's daily grind: sshuttle

    When he doesn't want to deal with OpenVPN version conflicts or congestion control problems during TCP tunneling, Charly catches a ride on sshuttle.

  • Charly's Column

    Some weather forecasts give you the temperature and a windchill adjustment, and a similar forecast for bandwidth would be helpful. If you feel the bandwidth is too low – and it always is – you need some kind of measuring instrument to reveal the truth.

  • LUG Camp 2010

    From the Lower Rhine to Central Franconia, on his journey, Charly found beaten gold, relaxed Linux users, abandoned beer cellars, and a Python one-liner for presentable photos of the tour. A once-in-a-year experience.

  • Charly's Column: PomodoPi

    Charly starts the tomato and dill season aboveground with a traffic light and underground with a soaker hose, along with assistance from two gardeners and the ubiquitous Raspberry Pi board.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News