Tuesday, July 11, 2017

autossh and systemd to start a reverse ssh tunnel

I have a machine behind my firewall. When I turn this machine on, I want it to make a reverse ssh connection to another machine, which is available from outside the firewall. I use autossh to make and maintain the connection, and systemd to start the task on boot. The systemd unit file looks like:

cat /etc/systemd/system/autossh.service
Description=Keeps a tunnel to 'MYEXTERNALDOMAIN.com' open

ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -i /root/.ssh/id_rsa -R 22222:localhost:22 -p 23 USER@MYEXTERNALDOMAIN.com


And then do:

systemctl start autossh
systemctl enable autossh