Reverse SSH tunnel and autossh
Charly's Column – Reverse SSH Tunnel
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.)
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
- "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
- Dual-Stack Lite: https://en.wikipedia.org/wiki/IPv6_transition_mechanism#Dual-Stack_Lite_(DS-Lite)
- autossh: https://linux.die.net/man/1/autossh
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.