Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In short, every time my computer boots, I would like to start a ssh port-forwarding connection that will reconnect with the server if the connection drops. Computer is Debian Wheezy x64.

An initial and successful connection can be made as follows:

screen -dmS autossh autossh -M 29000 -N -v -p 22 -l user -i /path/to/.ssh/id_rsa -L port:my-computer:port my-server.com

How can this be made to run at startup? (preferably by user, not root). Here is currently how I have been starting programs at boot:

rc.local calls my startup script as follows:

su -c "/home/username/scripts/startup" username

Currently, /home/username/scripts/startup contains the following:

screen -dmS program1 python my-python-program
screen -dmS program2 python my-python-program2
screen -dmS autossh autossh -M 29000 -N -v -p 22 -l user -i /path/to/.ssh/id_rsa -L port:my-computer:port my-server.com

Once booted, all three programs appear to be running (by list screen -ls). There is no problem with program1 or program 2. However, autossh is not actually forwarding the port.

The difference between a successful connection run by me, and an unsucessful connection run at startup is that a successful connection has the line:

debug1: channel 4: free: direct-tcpip: listening port 8082 for 192.168.1.104 port 80, connect from ::1 port 59681, 
nchannels 5

While the unsuccessful connection keeps trying like so:

debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 4: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 5: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 6: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 7: new [direct-tcpip]
debug1: Connection to port 8082 forwarding to 192.168.1.104 port 80 requested.
debug1: channel 8: new [direct-tcpip]

Any idea what is wrong? Why would a program work fine when I run it manually, but not inside my startup script?

share|improve this question
    
Sorry - dont have the time for a full answer, but : harding.motd.ca/autossh should help you along the way. –  FreudianSlip Dec 27 '12 at 20:06
add comment

1 Answer

up vote 3 down vote accepted

Check out the program autossh for automatic reconnection. screen will not mess up because you always specify external commands and flags for those commands after screen specific flags.

share|improve this answer
    
Autossh works just fine, thank you. I am having a hard time getting it to start at boot. I like to use my startup script for everything (and it has worked fine so far starting everything else). However, it does not work for autossh. I have edited my question above, please take a look at it. –  Oddworld Dec 28 '12 at 14:28
    
Thank you for pointing me to autossh, that works great! Now, my only issue is getting it to run at startup. For some odd reason, it seems to work well when I run it by hand, but not when it is in my startup script. Odd –  Oddworld Dec 28 '12 at 15:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.