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
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.
-
CachyOS Adds Support for System76's COSMIC Desktop
The August 2024 release of CachyOS includes support for the COSMIC desktop as well as some important bits for video.
-
Linux Foundation Adopts OMI to Foster Ethical LLMs
The Open Model Initiative hopes to create community LLMs that rival proprietary models but avoid restrictive licensing that limits usage.
-
Ubuntu 24.10 to Include the Latest Linux Kernel
Ubuntu users have grown accustomed to their favorite distribution shipping with a kernel that's not quite as up-to-date as other distros but that changes with 24.10.