Hosticlefifer WebLog

Topics:

Hosticlefifer (Website updates) 
Eye in the Sky 
Solar APRS Weather Station 
Home Biology Lab 
Programming Tidbits 

Latest Posts:

Home

In Eye in the Sky
Published: 28 Aug 2021
See just this article

AutoSSH persistent connection

I found that my reverse SSH tunnel with autossh was not persistent.

The remote server reboots daily, with the local machine (to which the server's requests are forwarded) rebooting weekly. The tunnel was exiting each time the remote server restarted, meaning that it was only persistent for one day each week. My script to establish tunnels is now as follows:

#!/usr/bin/env bash
autossh -i ~/.ssh/id_rsa_tunnel -N -R *****:localhost:***** user@example.com &
autossh -i ~/.ssh/id_rsa_tunnel -N -R *****:localhost:***** user@example.com &
autossh -i ~/.ssh/id_rsa_tunnel -N -R *****:localhost:***** user@example.com &
autossh -i ~/.ssh/id_rsa_tunnel -N -R *****:localhost:***** user@example.com

I was running the script @reboot in a cron job, but I created a systemd service to run it and restart the script if the last connection fails:

[Unit]
Description=AutoSSH reverse SSH tunnel connection
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service sshd.service

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s
User=pi
ExecStart=/home/pi/establish_tunnel

[Install]
WantedBy=multi-user.target

Hopefully this time it will stay up!