autossh is a near drop-in replacement for ssh
ssh. By drop-in replacement I mean you call it instead of
ssh and, normally, pass the exact same arguments as you would supply to
ssh. The near qualifier to that indicates that you need to provide one additional, non-ssh flag (option plus option argument) to
The extra option+arg is
-M port. port is the number of an unused port on your system where the adjacent port one higher than itself is also unused. For example, the manpage uses
20001 as an example and since those are both free on my system I use those as well. So I pass
-M 20000 to
If you used the same ports your command would look like...
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).