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?


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

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 20000 and 20001 as an example and since those are both free on my system I use those as well. So I pass -M 20000 to autossh.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.