I would like to write a script that gets as an argument a local computer ip (on the network of the ssh server), and forwards port 5900 on the client's computer to that ip:5900 on the server network.

Right after the connection is established I would like to execute vnc viewer and connect to that remote host, using the port forwarded.

The script I came up with is as followed:

ssh -L 5900:${1}:5900 user@server-ip

And it should be used like that:
Say we have on the server's network a computer with ip, so executing the command:


on my home (client) computer is supposed to open up the vnc viewer and allow me to control the local network computer (which runs vnc server ofcourse).

Only problem is that after the first line of the script is executed (ssh -L 5900:${1}:5900 user@server-ip), the connection is established and the next command (vncviewer won't be executed until the ssh session is closed...

My question is how can I establish the task I want?

I already tried appending '&' after the ssh command, but then when I execute the script I get the message:
"Psedo-terminal will not be allocated because stdin is not a terminal."
and it seems like that ssh is automatically closed (and even if it did work, I would have an indication that it succeeded before executing the vncviewer command).


You have to stick the SSH connection in the background. The ampersand at the end of the command does that and I'm passing in the N flag too to stop a shell running on the remote end (saves a handful of resources and stops SSH getting stroppy about having a console connected):

ssh -NL 5900:${1}:5900 user@${1} &
sleep 1

I used to do something similar but run x11vnc on the remote computer, on demand. This had the additional side-effect that it was only up while x11vnc was running... And that quits after the first disconnect (desirable!):

ssh -L 5900:${1}:5900 user@${1} "x11vnc" &
| improve this answer | |
  • Thanks for your answer. It seems like it should work, but as I try it it says "bind: Address already in use".. I guess I tried something similar before and the connection is still on in the background... how do I kill all connections to the ssh server? By the way, I really like your second method, but the vnc server is not on the same server as the ssh server, and actually it's on windows computers which does not have ssh server.. so I just prefer to let the vnc server on those computer work all the time and accept connections only from the ssh server... – rboy Aug 1 '13 at 9:44
  • This will kill all outgoing ssh connections: ps aux | grep "ssh " | awk '{print $2}' | xargs kill -9 – Oli Aug 1 '13 at 9:50
  • Thanks, it works! I still have the problem that vncviewer executes too fast (the connection to ssh is not fully established yet).. the script should figure out when the connection has established, or wait for a second or two before executing 'vncviewer'..I have no idea how I do this (I'm new to linux). appreciate your help! – rboy Aug 1 '13 at 9:57
  • You could wang a sleep 5 in after the ssh command. Bit crude but should do the job. Either that or you stick your vncviewer command in a "while not" loop that contains a sleep... Edit: I've edited my answer to include the second type. It'll keep trying until vncviewer exits with a zero status. I haven't tested it so could be dodgy. Edit2: Couldn't make it work, going for the easier version. – Oli Aug 1 '13 at 10:00
  • Sleep 1 does the job :) Thanks a lot! – rboy Aug 1 '13 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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