I would like to launch a ssh reverse tunnel on system launch. The following line let's me establish the tunnel fine - but I end up being logged into server which I don't want, especially not from an init script.

/usr/bin/autossh -M 22222 -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /path/to/my/key.key -R 9999:localhost:22 ubuntu@server

man autossh says that the -f option should do what I need:

causes autossh to drop to the background before running ssh.

but the problem is, it doesn't seem to work when I change my command to usr/bin/autossh -f -M.... I've tried this out as part of my init script too which looks like:

#! /bin/sh
case "$1" in
        echo "Starting autossh"
        /usr/bin/autossh -M 22222 -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /path/to/the.key -R pppp:localhost:22 user@server
        echo -n "Shutting down utossk"
        /usr/bin/killall -KILL autossh
        echo "Usage: $0 {start|stop}"
        exit 1
exit 0

But I need an option to throw it in the background, how do I do this if the -f option doesn't work for me?


What you're looking for is the -N option. From SSH's man page:

-N Do not execute a remote command. This is useful for just for‐ warding ports.

Your full command would be:

/usr/bin/autossh -M 22222 -f -N -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /path/to/my/key.key -R pppp:localhost:22 user@server

Which is also what I am using (with cron's @reboot), with success.
Replace pppp: with the local port you'd like to use.

|improve this answer|||||

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.