How to apply dynamic port allocation in AutoSSH? In the remote device using ssh we make a reverse tunnel by this command:
ssh -f -N -R 0:localhost:22 username@ip

It allocates a dynamic port on the server and prints that port on the machine . I want to apply the same thing using autossh.

So my question how can I achieve this?

1 Answer 1


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).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .