AutoSSH -ND at Ubuntu Boot – Setup Guide

serverssh

I have setup key-less login at a remote server. I use the remote server for tunneling my data. I want my local system to ssh into the remote server at boot time automatically.

I tried the solutions suggested in the following links but none of them worked for me:

  1. How to launch a SSH tunnel with upstart
  2. How do I make a script run upon startup of the Ubuntu machine?
  3. Autossh with Ubuntu Upstart

I did what was mentioned in those posts. The autossh command should open a port (9090 in my case) so that I can tunnel my traffic. After applying each of those methods, I restarted the PC and upon reboot I ran nmap to see if the particular port was listening or not. Sadly, in each case the port was still closed so I have to manually enter the command at terminal every time I reboot my PC.

Can some one help me? Do note that the remote server and my local system are both running Ubuntu 14.04, and the local system has no GUI installed.

Best Answer

I have this little script kept in ~/bin/infinite_ssh(1):

#! /bin/bash -x
#
while :; do 
         ssh -R 2222:localhost:22 remoteserver sleep 86400
         sleep 10
done

this is connecting to the remoteserver and creates a backward tunnel so that in the remote server I can ssh my desktop by using ssh -p 2222 localhost. Your application may, obviously, vary. The remoteserver is defined in ~/ssh/config so that you can login without a password, and has KeepAlive on option.

Then you can call it from /etc/rc.local with

su -l youruser -c /home/youruser/bin/infinite_ssh &

so that it starts as your user at boot time, in background.


Footnotes:

(1) obviously, you need to give the script execution permission... chmod a+x ~/bin/infinite_ssh.

Related Question