Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm working on setting up an unattended Ubuntu PC with Ubuntu Server 12.04 Minimal, and since this PC will be placed in a remote location with no keyboard or screen (unattended), I need to have a permanent SSH connection from this machine to a middle-server (between itself and me) - reason being, this PC will be behind a NAT firewall.

I followed dot-to-dot instructions at http://wiki.fabelier.org/index.php?title=Permanent_Reverse_SSH_Tunneling but when I connect, I just get an error Quote: ssh: connect to host port 19999: Connection refused So I went back googling for a solution and found http://www.alexonlinux.com/reverse-ssh-tunnel-or-connecting-to-computer-behind-nat-router

Surprisingly, Alexonlinux's solution worked out-of-the-box.

Seeing this, I tried the previous method, this time using Port 6333, but no luck. Then I tried Alexonlinux's method using Port 19999 and again it works fine.

The reason I can't stick to Alexonlinux's solution is, that it requires some commands to be entered on the PC behind the NAT, and that's not possible in my case.

I really wonder what could be stopping the connection when using Fabelier's method. Maybe the script is not loading? I did try manually running the script but still no luck.

I have also set

GatewayPorts yes

on the PC behind the NAT.

For the record, here's the script I've used from Fabalier's method:

a=`ps -ef | grep 19999 | grep -v grep`
if [ ! "$a" ]; then
    ssh -fN -R 19999:localhost:22 <middle-usename>@<middle-hostname>

(obviously I've changed the @ to the necessary) I had to change the first line as brackets were not acceptable.

Would really appreciate some help on this, pls.

share|improve this question
1) Both links are 404s, please fix them. 2) you start out very well (first paragraph), but I'm lost later on, could you rephrase pieces like "what could be stopping the connection"? 3) Does your actual question boils down to... "How can I keep this SSH reverse tunnel active in the background and automatically respawned?" as that's my best guess about your question right now. 4) that piece of script is a horrible way of checking things... –  gertvdijk Jan 11 '13 at 11:34
sorry about the links. Here they are, corrected: wiki.fabelier.org/… alexonlinux.com/… "what could be stopping the connection"; By this I was meaning, what could be causing the "connection failed" error. You're correct. I need to keep this SSH reverse tunnel active in the background and automatically respawned. But I need it to connect in the first place. –  user93078 Jan 11 '13 at 14:03
Please edit your question rather than providing new info via comments. –  gertvdijk Jan 11 '13 at 14:07
Okay, by re-reading your question I think your issue just boils down to a "connection refused" error on the remote/headless machine while running the ssh -R command. Is that correct? Then 1) make sure the 'machine in between' is accepting connections on port 22 (as you connect on the regular SSH port). 2) provide some more information like: does this work from another machine or from nowhere? (then issue is with the middle one). 3) I will probably be able to answer your issue, but this now looks way more complicated than it is, probably. –  gertvdijk Jan 12 '13 at 13:10
Thanks. Yes, the "middle" machine is accepting connections on Port22 - infact I do alot of SSH to it from anywhere. Also, as I mentioned, the reverse SSH tunnel is working when I use the method suggested at alexonlinux.com/… where there is no script involved. I would stick to this method, but the only thing is, the remote/headless machine is an unattended one and I cannot have anyone entering commands into it, thus I need a script that will keep the remote/headless machine permanently connected to the middle machine. –  user93078 Jan 13 '13 at 7:27

Your Answer


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

Browse other questions tagged or ask your own question.