I am tunneling an nc connection to my target host (example.com) over an ssh connection to my gateway. Below is my .ssh/config setup for this connection:

Host target
Hostname example.com
HostKeyAlias target
ProxyCommand ssh me@gateway /bin/nc example.com 22

This is great and allows me to ssh to the target host in one step. What I would like to do is be able to tunnel an NX or VNC session over this connection.

Anyone know how this might be done?


I think you should use port forwarding that SSH already can do:

ssh -L 5900:example.com:5900 me@gateway

This will forward port 5900 from your local machine to example.com through gateway. Now just "vncviewer localhost" from your local machine.

  • much easier than my solution! – johnny_bgoode Sep 11 '10 at 23:41

Here's how a VNC session can be created over an ssh tunnel with a gateway hop in between the client and target machines:

/etc/ssh/sshd_config (on target machine) must have these lines:

X11Forwarding yes
XAuthLocation /usr/bin/xauth

Your path to xauth may be different, but by default sshd looks in the wrong location so this value should be set.

.ssh/config (on the client machine):

Host example.com
Hostname foo.example.com
HostKeyAlias example.com
ProxyCommand ssh -t -C me@gateway.com /bin/nc %h 22

Then from the client I can run

me@localhost$ ssh -X -C example.com
This starts an X session on my client as well as an ssh connection to example.com. Now, just a couple more commands on the remote host to bring up VNC:

me@example.com$ vncserver :n 
me@example.com$ vncviewer -encodings "copyrect tight zlib corre rre raw" :n

In the above commands, :n is is the display number where you want to start vnc and the display where vncviewer should look for the vnc session. Since this looks like a local connection to VNC it is a good idea to specify some better encodings as vncviewer will default to raw, giving poor performance over the network.

That's it!

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.