autossh
is a drop-in replacement for ssh
with one caveat. Drop-in replacement means you call it instead of ssh
, passing exactly the same arguments as with ssh
. The caveat is that autossh
has a mandatory option (oxymoron alert!) that you must also include in your command invocation.
The required autossh
option is -M port
where port is the number of an unused port on your system such that port+1 is also unused. The autossh
manpage uses ports 20000
and 20001
as an example. Since those are both free on my system I use those as well so I add -M 20000
to my autossh
calls.
If you use the same ports your command will be:
autossh -M 20000 -f -N -R 0:localhost:22 username@ip
FYI these ports are used to monitor availability of the remote. Test data is sent out on 20000
and returned on 20001
. If nothing is returned then autossh
tries to reestablish a connection (that's simplifying things a bit but you get the point).