Matthew West

Remote Desktops with VNC

This page describes how to connect to a remote desktop on a Linux machine from a local OS X laptop.

You will need a VNC client installed on your laptop. A good one is Chicken of the VNC.

Format conventions

On this page, commands that you should type at a local and remote command prompt will respectively be written like:

local$ commandname -opt
remote$ commandname -opt
In each case, just type the text after "local$ " or "remote$ " (in this case "commandname -opt").

Local commands should be run in the Terminal program, while remote command should be run by first connecting to the remote machine with ssh, like this:

local$ ssh
remote$ remotecommand

Initial VNC setup

Login to the remote machine with ssh and create a VNC password with:

remote$ vncpasswd

The password you set must be at least 6 characters in length and will be called your VNC password. You only need to do this once ever.

Starting a remote VNC server

The remote Linux computer must run a VNC server than you can connect to. This is your desktop on the remote machine. It will persist whether you are connected to it or not.

To start a VNC server on the remote machine, login to it and run:

remote$ vncserver -geometry 1024x768 -depth 16 -nolisten tcp -localhost

New ' (mwest)' desktop is

This will print out some information, similar to that shown above. Make a note of the number following the colon (":") character (2 in the example above). This number is your VNC display number. After you have started a VNC server you should logout from the remote machine.

You will generally leave a VNC server running for a long time and will connect and disconnect from it repeatedly, as described below. When you first start a VNC server you are not connected to it, so you will need to establish a path and connect following the instructions below before using it for the first time. You only need to start a new VNC server if the old one is killed for some reason, or if you want a VNC server on a different remote machine.

Controlling remote VNC servers

To see whether you have any remote VNC servers running already on a particular remote machine you can do:

remote$ ps -ef | grep Xvnc

If this returns something like:

<username> <...> /usr/bin/Xvnc :2 -desktop <...>

where <username> is your username then you have one remote server running with a VNC display number of 2. Multiple such lines indicate multiple remote servers. Here the <...> indicates extra text.

You can kill all of your running VNC servers with the command:

remote$ pkill -u <username> Xvnc
where <username> should be replaced with your remote Linux username.

Establishing a path to a VNC server

Before you can connect to the remote VNC server, you must establish a secure path from your laptop to the remote Linux machine where the VNC server is running. To do this, you need to compute the VNC port number. This is given by 5900 plus the VNC display number, so the example above with a VNC display number of 2 will give a VNC port number of 5902.

To establish the local-to-remote secure path, on your laptop run:

local$ ssh -L <vnc_port_num>:<remote_machine>:<vnc_port_num>

where <vnc_port_num> is replaced with the VNC port number and <remote_machine> is replaced with the name of the remote machine. In the example above this would give:

local$ ssh -L

This ssh session must stay active while you are connected to the remote VNC server. If it is terminated then the connection to the VNC server will be broken, but this will not kill the VNC server and you can reconnect to it later. This method of using ssh is called port redirection.

Connecting to a VNC server

To connect to the VNC server you must first have a path established (see the previous step). Next you must run your local VNC client (Chicken of the VNC) on your laptop. In the connection window you need to enter several variables, which you should give as:

Host: localhost
Display: <vnc_display_num>
Password: <vnc_password>

where <vnc_display_num> is your VNC display number (2 in the example above) and <vnc_password> is your VNC password.

The first time you connect to a newly created VNC server you may need to login to the remote machine. You should use your remote Linux password, not your VNC password.

Disconnecting from a VNC server

You can disconnect from a remote VNC server by quitting Chicken of the VNC and exiting from all ssh connections to the remote machine.

This does not kill the remote VNC server, which continues to run completely unaffected. If you later reconnect to the VNC server then it will be exactly as you left it. Any computations running in it will have continued to run while you were disconnected.