2

I need an init script which will establish a reverse tunnel connection to a server. I've come up with the following start script:

#! /bin/sh
### BEGIN INIT INFO
### END INIT INFO
case "$1" in
    start)
        echo "Starting autossh"
        /usr/bin/autossh -M 22222 -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /path/to/my.key -R 9999:localhost:22 ubuntu@host
    ;;
    stop)
        echo -n "Shutting down autossh"
        /usr/bin/killall -KILL autossh
    ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac
exit 0

Which has one problem: It doesn't launch the process in the background, otherwise it works as expected. I have attempted to use the -f flag and altered the start) line to /usr/bin/autossh -f -M 22222 but that does not seem to work and I'm wondering why. What am I doing wrong, the man page says

-f' causes autossh to drop to the background before running ssh.

What's going on here?

In /var/log/syslog on server, I can see:

Nov 3 22:01:10 ip-172-31-33-223 systemd[1]: Started Session 1524 of user ubuntu.

with and without the -f flag, i.e. the client does establish a connection to host but when the -f flag is present, it won't let me log in through localhost on port 9999.... :o

Your Answer

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

Browse other questions tagged or ask your own question.