Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a VNC server (x11vnc) configured to allow me to log into the machine at the local display (:0) via SSH tunnel, following the instructions here.
My question: is there a way to create the tunnel AND launch vncviewer with a single command/script?

Here's how I'm launching x11vnc from /etc/gdm/Init/Default:

x11vnc -localhost -nopw -ncache 0 -o /var/log/x11vnc.log -forever -bg

Edit: please note that in this way x11vnc starts from gdm, so that I can log into the local session myself.

(Server and client: Xubuntu 9.04)

share|improve this question
up vote 6 down vote accepted

The vncviewer can connect on it's own through SSH, so a shorter command would be:

vncviewer -via username@hostname.tld localhost:x

On the above, x is your local display (0, 1...).

share|improve this answer
Very convenient, thanks :) I still have the "disconnects after login" problem though.. Maybe the only way for that is to use two terminals.. – Joril May 5 '09 at 11:50
Setting up VNC as a remote terminal service requires a bit of footwork, but you might be better off installing the tightvnc package instead. Not only does it have slightly better options but it also supports compression, a must-have for remote work over slow internet connections (tunneling with SSH, of course). Also, I believe that there is a command option that discourages disconnects...can't recall ATM... – Avery Payne May 5 '09 at 12:53

Note: untested

try this

ssh host -f -L5900:localhost:5900 "sleep 10000000000" && vncviewer localhost:5900
share|improve this answer
This works fine, thanks! I'll look for a way to close ssh when the vnc connection gets closed.. Anyway, now the only remaining problem is logging in.. When I enter user and password vncviewer exits and if I try to reconnect, the login screen is still there.. If I use two terminal windows instead (one for the SSH tunnel and one for VNC) I can login just fine.. :/ – Joril May 5 '09 at 9:43

Your Answer


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.