22 December 2012

299. Briefly: Start autossh at boot on debian testing/wheezy

I have one of my work computers set up to create a reverse tunnel to my WRT54G router at home, so that by login in to my router at home and then connecting to localhost:19996 I can access my work network from home. The problem is that if I reboot my work computer remotely I need to make sure that it tries to recreate the reverse ssh tunnel.

The way to do that is by editing /etc/rc.local and putting the following in it:
su lindqvist -c 'autossh -N -f -M 29001 -R 19996:localhost:22 remoteuser@my.router.com' &

Make sure you put it before the line that says
exit 0

linqdvist is the user at on my work computer I want to be running the autossh, 19996 is the port I'll connect to on my home router to gain access to the ssh port on my work computer. Since I'm using WRT54G with Tomato the remoteuser is root, and I got my dns (remote hostname) as shown in this post: http://verahill.blogspot.com.au/2012/02/tomato-router-and-free-dns.html
It's that simple.


  1. Thank you, I spent about 4 hours trying to make rc.local work on my raspberry pi with autossh. Logging in as a user instead of doing it on root with the "su user -c '' & did the trick for me.

  2. Thank you. Now it's working for me to.

  3. Why is su -c needed? adding it made things work for me but I dont understand why it has to be there

  4. su lindqvist -c '' runs the command in '' as user lindqvist. Otherwise it'd be run as root.

  5. Thank you, that was helpful! Do I need to use su because of keys storage in user's home?

  6. Very nice, I was just about to give up for a few hours and BAM you solved my problem. Thank You..

    su user -c worked like a charm..