How to setup VNC on Ubuntu Linux

A lot of the tutorials online teaching us to install and configure tightVncServer.
However, that is totally unnecessary, Ubuntu comes with vnc4server right out of the box (I’m using Ubuntu 12.04 LTS). And vnc4server is quite easy to configure.

Make sure vnc4server does exist in your Linux box

You may check the existence of vnc4server by issuing this command:
dpkg -l | grep vnc

This will list all installed packages related to vnc, and you should find vnc4server in the list.

If vnc4server isn’t come installed, you may install it issuing:
sudo apt-get -y update; sudo apt-get -y install vnc4server

start vnc4server

Now that we know that vnc4server is installed, we are ready to start it.

After looking at the man page of vnc4server, I know that I can follow this basic syntax to start one (Yes I can launch multiple vnc servers with different display numbers. If I have Dual-Monitor setup on your client machine, I can have two vnc connections to increase productivity) vnc server:

This will launch a vncserver as display #55, at resolution 1920×1080, with full color supported(-depth 24).


Password:

Since this is the first time, we start the vnc server, it asks me for a password to use for the connection. Enter a desired password and enter again to verify it. This will only show on the first time we start the server, and I can leave it blank so to connect without a password.

The options used here are the most important ones. You can check out its man page for all available options and their usage.

It is very tedious to enter that long command each time we launch a vnc server. While we can create a shell script to save the typing.

Save this file as vncstart, and make it executable:

Now I can run the script to start a server with display #55:

Connect

There are many VNC clients on different systems. I am only covering the my choices on Windows, OS X and iOS.

Windows – Tight VNC Viewer

You can get a copy of TightVncViewer free at http://www.tightvnc.com/download.php.

OS X – Screen Sharing

The best vnc client on Mac actually comes preinstalled on every Mac. However, Apple hides it very deep.

To dig it out, launch Safari and enter vnc:// in the address field.

After I hit Return, Screen Sharing launches right away asking me for a host to connect.

Pay extra attention to the port # I put here. It is 5955 instead of 55 as I did in TightVNCViewer on Windows.


Why:

5900 is the default port used for VNC connections. Remember in the previous section I started the vnc server at display #55. 5955 (default #5900 + display #55) is the real port # setup for our vnc connection. However TightVNCViewer is smart enough to do the math and calculates the port #(5955) for me.

We’d better Keep Screen Sharing in Dock for easy access.

Curious about where Apple hides this great tool? 
Hold down the Command key while clicking on the Screen Sharing icon in Dock.
Finder immediately opens and shows us the location it.

iOS – Pocket Cloud

Remember to use the full port # just like in Screen Sharing on OS X.

Oops!

Although we are successful to connect, it is not perfect yet.
As you can see, it shows nothing but a Terminal window and pure grey background.

Configure xstartup File

We need to modify the xstartup file located under .vnc (invisible form Nautilus) in your home directory.

Open the file with gedit and replace its content with the following:

#!/bin/sh

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
#exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey 
vncconfig -iconic &;
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &;
x-window-manager &;

if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
fi

vncconfig -iconic -nowin &;
. /etc/X11/Xsession

Save the file and close gedit.

Kill the vnc server

Now, let’s kill the current vnc server and start a new one, so that our new configuration will be applied.

Success!!!

Now I can relax and enjoy VNCing into my Linux box.

Leave a Reply