autossh
is awesome. I use the following command to keep a persistent connection between my computer and another one for the purposes of ssh tunneling.
I use the following command to keep a persistent connection between my computer and another one for the purposes of ssh tunneling.
autossh -M 0 -q -f -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 8022:localhost:8022 remotecomp
Note that my computer uses 8022 as the local ssh port.
I created a systemd script which will manage the autossh
instance(s). To do this I created the file ~/.config/systemd/user/autossh.service
with the following contents
[Unit]
Description=autossh remote
After=network.target
[Service]
ExecStart=/usr/bin/autossh -M 0 -q -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R 8022:localhost:8022 me@remote
[Install]
WantedBy=default.target
Note that the new autossh
command lacks the -f
flag. This is important; systemd won’t work this way if the process goes into the background. Now all you have to do is enable the user script for it
systemctl --user start autossh.service
systemctl --user enable autossh.service
and then check that everything is running correctly
systemctl --user enable autossh.service
I changed autossh.service
to autossh-remote.service
to be more specific, and in case I want more than one tunnel active at a time.