How to install and configure VNC on Ubuntu 18.04

s2 0

This guide describes the steps required to install and configure a VNC server on Ubuntu 18.04. We will also show you how to securely connect to the VNC server via an SSH tunnel.

Virtual Network Computing (VNC) is a graphical desktop sharing system that allows you to use your keyboard and mouse to remotely control another computer. It is an open source alternative to Microsoft Remote Desktop Protocol (RDP).


Before proceeding with the tutorial, make sure that you are logged in as a user with sudo permissions.

Install desktop environment

Most servers don’t have a desktop environment installed, so let’s start with installing a lean desktop environment.

Several desktop environments (DE) are available in Ubuntu repositories. We install Xfce
. It’s a fast, stable, and lightweight desktop environment that makes it ideal for use on a remote server.

Enter the following commands to install Xfce on your server:

sudo apt updatesudo apt install xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils

Depending on your system, downloading and installing the Xfce packages may take some time.

Install VNC server

There are also several different VNC servers available in Ubuntu repositories such as: TightVNC
, TigerVNC
and x11vnc
. Each VNC server has different strengths and weaknesses in terms of speed and security.

We will install TigerVNC. It is an actively maintained high-performance VNC server.

Enter the following command to install TigerVNC on your Ubuntu server:

sudo apt install tigervnc-standalone-server tigervnc-common

After the VNC server is installed, the next step is to create the initial configuration and set up the password. Don’t use sudo when doing this vncserver Command:


You will be prompted for and confirm the password and whether to set it as a read-only password. If you set a read-only password, the user will not be able to interact with the VNC instance using the mouse and keyboard.

You will require a password to access your desktops.

Would you like to enter a view-only password (y/n)? n
/usr/bin/xauth:  file /home/linuxize/.Xauthority does not exist

New ' (linuxize)' desktop at :1 on machine

Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/linuxize/.vnc/

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/linuxize/.vnc/passwd :1 to connect to the VNC server.

The first time the vncserver Command is executed, it creates and saves the password file in ~/.vnc. This directory will be created if it does not exist.

Notice that :1 after the host name in the output above. This indicates the display port number the vnc server is running on. In our case, the server runs on the TCP port 5901 (5900 + 1). If you create a second instance with vncserver it is executed on the next free port, ie :2which means that the server is running on port 5902 (5900 + 2).

It is important to remember that when working with VNC servers :X is a display port that focuses on. relates 5900+X.

Before proceeding to the next step, stop the VNC instance with the vncserver Command with a -kill Option and the server number as an argument. In this example the server is running on port 5901 (:1) so we end it with:

vncserver -kill :1
Killing Xtigervnc process ID 7264... success!

Configure VNC server

Now that both Xfce and TigerVNC are installed on the server, we need to configure TigerVNC to use Xfce. To do this, create the following file:

~ / .vnc / xstartup
nano ~/.vnc/xstartup
exec startxfce4 

Save and close the file. The above commands will be executed automatically whenever you start or restart the TigerVNC server.

the ~/.vnc/xstartup The file must also have execute permissions. Do the following chmod
Command to make sure the permissions are correct:

chmod u+x ~/.vnc/xstartup

If you have to pass Additional options
to the VNC server, create a file called config and add one option per line. Here is an example:

~ / .vnc / config

Create a systemd unit file

Create a systemd unit file so that you can start, stop, and restart the VNC service as needed, just like any other systemd service.

Open your text editor and copy and paste the following configuration. Make sure to change the username on line 7 to match your username.

sudo nano /etc/systemd/system/[email protected]
/etc/systemd/system/[email protected]
Description=Remote desktop service (VNC)

ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver :%i -geometry 1440x900 -alwaysshared -fg
ExecStop=/usr/bin/vncserver -kill :%i


Save and close the file.

Notify systemd that we have created a new unit file with:

sudo systemctl daemon-reload

The next step is to activate the unit file with the following command:

sudo systemctl enable [email protected]

The number 1 after this @ sign defines the display port on which the VNC service is executed. This means that the VNC server is listening on port 5901as we discussed in the previous section.

Start the VNC service by running:

sudo systemctl start [email protected]

Check if the service started successfully with:

sudo systemctl status [email protected]
[email protected] - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
   Active: active (running) since Thu 2018-08-16 19:05:54 UTC; 4s ago
  Process: 9893 ExecStartPre=/bin/sh -c /usr/bin/vncserver -kill :1 > /dev/null 2>&1 || : (code=exited, status=0/SUCCESS)
 Main PID: 9900 (vncserver)
    Tasks: 0 (limit: 507)
   CGroup: /system.slice/system-vncserver.slice/[email protected]
           ‣ 9900 /usr/bin/perl /usr/bin/vncserver :1 -geometry 1440x900 -alwaysshared -fg

Establish a connection to the VNC server

VNC is not an encrypted protocol and can be subject to packet sniffing. The recommended approach is to create an SSH tunnel that securely forwards traffic from your local computer on port 5901 to the server on the same port.

Setting up SSH tunneling on Linux and macOS

If you’re running Linux, macOS, or any other Unix-based operating system on your computer, you can easily create an SSH tunnel with the following command:

ssh -L 5901: -N -f -l username server_ip_address

You will be asked to enter the user password.

Don’t forget to replace username and server_ip_address with your username and the IP address of your server.

Set up SSH tunneling under Windows

If you’re running Windows, you can use SSH tunneling using the PuTTY SSH client

Open Putty and put your server IP address in the Host name or IP address Area.

Under the Connection Menu, box, expand SSH and choose Tunnels. Enter the VNC server port (5901) by doing Source Port Field and enter server_ip_address:5901 by doing Destination Field and click on the Add Button as shown in the picture below:

Go back to the Session Page to save the settings so that you don’t have to re-enter them every time. To the remote server, select the saved session and click the Open Button.

Connect with Vncviewer

After the SSH tunnel has been created, it is time to open your Vncviewer and connect to the VNC server localhost:5901.

You can use any VNC viewer like TigerVNC, TightVNC, RealVNC, UltraVNC, Vinagre and VNC viewer for Google Chrome.

We will be using TigerVNC. Open the viewer, enter localhost:5901and click the Connect Button.

Enter your user password when prompted and you should see the default Xfce desktop. It will look something like this:

You can interact with the remote XFCE desktop from your local computer using your keyboard and mouse.


By now you should have a VNC server up and running, and you can easily manage your Ubuntu 18.04 server from your local desktop computer using an easy-to-use graphical interface.

To configure your VNC server so that it starts a display for more than one user, create the initial configuration and set up the password with the vncserver Command. You will also need to create a new service file with a different port.

Feel free to leave a comment if you have any questions.