5

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.

Faisal
  • 183
  • 1
    "I have [...] tried some solutions but they didn't work for me" --- please specify what you tried; people will be much more prone to spend time for you if you do the same in the first place. Read http://askubuntu.com/help/how-to-ask – Rmano Apr 17 '15 at 09:01
  • https://www.async.fi/2013/07/autossh-with-ubuntu-upstart/ , http://superuser.com/questions/155476/how-do-i-make-a-script-run-upon-startup-of-the-ubuntu-machine and there was another post that I can't find right now.. It asked to put the script in /etc/init.d or someplace similar – Faisal Apr 17 '15 at 09:08
  • please add the new information to the question, in a format easy to understand. I will try an answer, but as is I do not know if it will be useful to you. – Rmano Apr 17 '15 at 09:16
  • Updated the post – Faisal Apr 17 '15 at 10:17
  • 1
    The three of them should work. Please tell us why they do not work, and what you did exactly. – Rmano Apr 17 '15 at 11:04
  • 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. – Faisal Apr 17 '15 at 12:09
  • 2
    @Faisal Welcome to Ask Ubuntu. It is always better to edit/update original post with such important info instead of comments. That will help much to keep post clean. – user.dz Apr 17 '15 at 13:10

1 Answers1

3

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.

Rmano
  • 31,947
  • I created the script in /home/user/bin/ folder and replaced the while loop with autossh command. But at login prompt I get this error: "su: /home/user/bin/myssh - Permission Denied" – Faisal Apr 17 '15 at 10:16
  • 1
    @Failsal, have you gave the script execution permission? – Rmano Apr 17 '15 at 11:05
  • 2
    I gave execution permission and now its working just like I wished! I thought that the command, being run from /etc/ directory, needed root access that's why it was throwing that error. I am just a beginner. Thanks a lot for your help. – Faisal Apr 17 '15 at 12:13