1. Home
  2. Linux
  3. Ubuntu
  4. Installing and configuring VNC for Ubuntu 14.04

Installing and configuring VNC for Ubuntu 14.04

Installing and Configuring VNC on Ubuntu 14.04
Installing and Configuring VNC on Ubuntu 14.04

Introduction to VNC

VNC or ‘Virtual Network Computing’ is a connection system that allows you to use your keyboard and mouse to interact with a graphical desktop environment on a remote server.
VNC makes managing files, software, and settings on a remote server easier for users who are not yet comfortable in working with the command line.
In this tutorial we will teach you how to set up VNC on an Ubuntu 14.04 VPS and connect to it securely using an SSH tunnel.
The type of VNC server we will be using is ‘TightVNC’, a lightweight and fast remote-control package.


Before you follow this guide, you will need to complete a few steps.
You will need to have an Ubuntu 14.04 VPS installed and configured with a non-root user which has the sudo privileges.
After you have your non-root user, feel free to use it to SSH into your Ubuntu VPS and move on with your VNC server installation.

Installling the Desktop Environment and VNC Server

By default, the Linux server installations will not come with a graphical desktop environment. If this is the case, you will need to start by installing one that you can work with. As an example, we will install XFCE4 as it is very lightweight while also still being familiar to a lot of users.
You could use the XFCE packages, as well as the package for TightVNC, directly from Ubuntu’s software repositories using apt.

sudo apt-get update
sudo apt-get install xfce4 xfce4-goodies tightvncserver

To finish the VNC server’s initial configuration, execute the vncserver command to change to a more secure password.


Once you have setup your access password, you will be asked if you want to enter a view-only password. Users who use the view-only password to login will not be able to control the VNC instance using their mouse or keyboard. That will be a very helpful option if you would like to demonstrate anything to other people using your VNC server.
The ‘vncserver’ command will complete the installation of VNC by making default configuration files and connection information for the server to use. With the packages installed, you should be ready to configure the VNC server and graphical desktop.

Configuring the VNC Server.

First thing you must do is tell your VNC server what commands it needs to execute once it starts up. The commands are located in the configuration file called ‘xstartup’. The VNC server has an ‘xstartup’ file being preloaded already, though we will need to use a couple of different commands for the XFCE desktop.
After VNC is set up, it will launch a default server Instance on port 5901. This port is known as a display port and is referred to by VNC as ‘:1’. The VNC will launch multiple instances on more display ports, such as ‘:2′ and ‘:3’. After working with VNC servers, do not forget that ‘:X’ will be a display port which refers to 5900+X.
You are going to change how your VNC servers are configured, so you first have to stop the VNC server instance that is running on port 5901.

vncserver -kill :1

Before you start configuring the new ‘xstartup’ file, you can back up the original in case you require it later.

mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

You can now open a new ‘xstartup’ file using nano.

nano ~/.vnc/xstartup

Enter the following commands into the file so that they are made automatically whenever you decide to start or restart the VNC server.

xrdb $HOME/.Xresources
startxfce4 &

Your first command in the file:

xrdb $HOME/.Xresources

This command will tell VNC’s GUI framework to read the server user’s ‘.Xresources’ file.
‘.Xresources’ is the place where a user is able to make changes to certain settings of the graphical desktop, such as terminal colors, cursor themes, and font rendering.
The second command will tell the server to launch XFCE; this is where you are going to find all of the graphical software that you will require in order to comfortably manage your server.
To make sure that the VNC server is able to use the new startup file completely, you have to grant some executable privileges to it using the following command.

sudo chmod +x ~/.vnc/xstartup

Creating a VNC Service File

In order to control your new VNC server easily, you will need to set it up as a Ubuntu service which will allow it to start, stop, and restart the VNC server whenever needed.
Begin by opening a new service file in ‘/etc/init.d’ with nano.

sudo nano /etc/init.d/vncserver

The first block of data will be where you state some common settings that VNC is going to be referring to quite often such as your username and the display resolution.

export USER="user"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY} -localhost"
. /lib/lsb/init-functions

Make sure you switch user with the non-root user you have set up; makes sure to replace 1024×768 if you would like to use a different screen resolution for your virtual display.
Now, you may start by inserting the command instructions which will allow you to control the new service. The next block binds the command necessary to start a VNC server, along with feedback that it is being started, to the command keyword ‘start’.

case "$1" in
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"

The next block will create the command keyword ‘stop’ which will instantly kill an existing VNC server instance.

log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"

The last block will be used for the command keyword ‘restart’ which this is both of the last commands combined.

$0 stop
$0 start
exit 0

After all of the blocks above are in the service script, you may save and close that file. Then, make this service script executable so you are able to use the commands that you have just set up.

sudo chmod +x /etc/init.d/vncserver

Attempt using the service and command to start a new VNC server instance.

sudo service vncserver start

Connecting to your VNC Desktop

To try your VNC server, you will have to use a client which supports VNC connections over SSH tunnels. If you are using Windows, you may use TightVNC, RealVNC, or UltraVNC. Users who using Mac OS X should be able to screen share, or possibly use a cross-platform application like RealVNC.
You have to make an SSH connection on your local computer which safely forwards to the localhost connection for VNC. You should be able to do this via the terminal on Linux or the OS X with the following command.
Reminder: Change ‘user’ and ‘your_ip_address’ with the username and IP you used to connect to your server with SSH.

ssh -L 5901: -N -f -l user your_ip_address

If you are using a graphical SSH client, like PuTTY, use ‘your_ip_address’ as the connection IP and set ‘localhost:5901’ as the new forwarded port in the program’s SSH tunnel settings.
Now, you may use the VNC viewer in order to connect to the VNC server at ‘localhost:5901’. Ensure you remember the ‘:5901’ at the end as that is the only port that the VNC instance is accessible from.
After you have connected, you will see the default XFCE desktop ready for configuration and use.
Furthermore, after you have verified that the VNC connection is working, add the VNC service to the default services so that  it will automatically start once you boot your server.

sudo update-rc.d vncserver defaults


You will now have a secured VNC server up and running on your Ubuntu 14.04 VPS. Now you are going to be able to easily manage your server’s files, software, and settings using an easy-to-use graphical interface.

Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']