Friday, March 14, 2008

How to Set up VNC on Debian GNU/Linux

Installation and usage


You've just installed Debian, but your wife wants her monitor back. That's OK, you were planning on running it headless, anyway. But, wouldn't it be nice to check out some of those groovy GUI apps? Don't fret, VNC will let you interact with a desktop environment from just about any platform available.


Install vncserver (as root):
apt-get install vncserver


Choose your desired window size and color depth, then, as an ordinary user, open a terminal and type:
vncserver -geometry 1024x768 -depth 24
or
vncserver -geometry 1024x768 -depth 24 -pixelformat rgb565


This will prompt you to create a password:

You will require a password to access your desktops.

Password:
Verify


The server will start and tell you where to access it:

New 'X' desktop is foobar:1

Starting applications specified in /etc/X11/Xsession
Log file is /home/jorey/.vnc/foobar:1.log



Open the VNC viewer on your remote machine, enter the hostname:screen and password (use a hostname or IP that your client machine understands), and your Linux desktop will open in a window! Network speed and processor power will affect performance, but it's amazing how many apps will run fine under VNC. You might not be able to play Frozen Bubble, but you can use productivity applications without any trouble.


To kill the server enter a command similar to this, using the appropriate settings:
vncserver -kill :1


The reason for killing the session is that you may need to edit the default configuration file that vncserver creates for you, for example to get the vncserver to run the K desktop environment instead of twm, you may want to edit the $HOME/.vnc/xstartup file to replace the line:


twm &


with this line is you use KDE:


startkde &


and with this line if you use GNOME:


gnome-session &


before launching the vncserver again using:


vncserver :1 -geometry 1024x768 -depth 16 -pixelformat rgb565


VNC over OpenSSH


The following is just a summary of the full explanation. To use VNC over OpenSSH, first you need to run an ssh session on the machine where you will be running the vncviewer, to request that ssh listen on a particular port on your local machine, and forward communication on that port down the secure connection to a port on the machine running the vncserver.


For example:

ssh -L x:localhost:y vncserver_machine

means "Start an SSH connection to the vncserver_machine, and also listen on port x on my machine, and forward any connections there to port y on the vncserver_machine."


Now, the VNC protocol normally uses port 59xx, where xx is the display number of the server. So a VNC server on a Windows machine, which normally uses display number 0, will listen on port 5900. The first VNC server on linux will probably use display number 1, and subsequent servers would use 2, 3, etc. and so the vncservers on linux will be listening on ports 5901, 5902 and so forth. By forwarding these ports to a remote machine running vncserver, you can make the remote VNC server appear to be a server running on your local machine. So, imagine you had a VNC server running as display :1 on vncserver_machine, and you wanted a secure connection to it from your local machine. You could start the ssh session using:

ssh -CL 5902:localhost:5901 vncserver_machine

After that, starting up the vncviewer as follows on your local machine: vncviewer -encodings "copyrect hextile" localhost:2 would actually connect to display :1 on the vncserver_machine.


Note that the above OpenSSH command-line is deliberately meant to accept incoming connections only from the local machine. This means that to use the ssh connection that we have just set up, we must connect to it from the same machine, using the special name localhost, rather than using the local machine's own unique name.

This article is compiled from following resources:


http://www.joreybump.com/code/howto/debian/vnc.html
http://www.penlug.org/twiki/bin/view/Main/TightVNC

2 comments:

Anonymous said...

That's a very concise, accurate, and helpful howto article, thank you.

Unknown said...

hi, can i remote my linux desktop from windows via vnc ?