What is the default monitoring port for autossh?

The autossh command has the following -M option:

   -M port[:echo_port]

specifies the base monitoring port to use. Without the echo port, this port
and the port immediately above it ( port + 1) should be something nothing else
is using. autossh will send test data on the base monitoring port, and receive
it back on the port above. For example, if you specify "-M 20000", autossh will
set up forwards so that it can send data on port 20000 and receive it back on
20001. 

Alternatively, you can specify the same behavior using the AUTOSSH_PORT environment variable.

My question is the following:

If neither the -M option nor the env variable is specified, what port is used for monitoring? Or is the behavior disabled? The documentation is unclear.


If neither the -M option nor the env variable is specified, what port is used for monitoring?

In my several tests on Kubuntu client, random high ports were used in a range about (my estimation) 30000-60000. Compare: ephemeral port. I mean the first (port) was randomly chosen, the second one was just above it (port+1).

-M without a proper argument (autossh -M -- … or autossh -M foo …) exits immediately; it doesn't print any error but the exit status is 1.

autossh -M 0 … indeed disables the feature.

But…

I tried to analyze the source and haven't found this randomness. Then I discovered this:

$ type autossh
autossh is /usr/bin/autossh
$ file /usr/bin/autossh
/usr/bin/autossh: POSIX shell script, ASCII text executable
$ head -n 4 /usr/bin/autossh
#!/bin/sh
# little wrapper to choose a random port for autossh, falling back to $fallback_port

fallback_port="21021"
$ tail -n 1 /usr/bin/autossh
exec /usr/lib/autossh/autossh "$@"
$ file /usr/lib/autossh/autossh
/usr/lib/autossh/autossh: ELF 64-bit LSB executable, …

This means in Kubuntu I have a wrapper that randomly chooses a port, if it's not specified by -M nor AUTOSSH_PORT. I'm not posting the full code here but I've read it: if the wrapper cannot randomly hit an unused pair of ports (i.e. port and port+1) in 42 tries then it finally tries 21021 and 21022; if these ports cannot be used, the wrapper exits with an error.

If the wrapper succeeds, it exports the first port as AUTOSSH_PORT and runs the real autossh (/usr/lib/autossh/autossh). This executable requires -M or AUTOSSH_PORT, otherwise it won't run.

My final answer is: there is no default monitoring port for vanilla autossh. You have to set AUTOSSH_PORT or use -M. This is inconvenient, hence the wrapper. You may or may not have one in your Linux. It's possible some flavors of Linux use wrappers that set and use a fixed default port.


There is no default port because it can be any unused port on the remote system however it also must be open. However, I don't think there's any 'sane' use case for it, as documented here you might as well just use SSH's own stay alive options instead:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 5000:localhost:3306 [email protected]

This would 'disconnect' after 90 seconds of no response from the server, and then AutoSSH would reconnect automatically.