How to Install and Configure VNC on Ubuntu 18.04 – This guide covers the steps required to install and configure a VNC server on an Ubuntu 18.04 system. I’ll also show you how to securely connect to a VNC server via an SSH tunnel or Tunnel.
Virtual Network Computing (VNC) is a graphical desktop sharing system that allows you to use a keyboard and mouse to remotely control another computer. This is an open source alternative to Microsoft remote desktop protocol (RDP.
VNC is platform independent, where there are clients and servers for many GUI based operating systems and for Java. Multiple clients can connect to the VNC server at the same time. Popular uses for this technology include remote technical support and accessing files on a work computer in an office from a home computer, or vice versa.
Before proceeding with the tutorial how to install and configure VNC on Ubuntu 18.04, make sure you are logged in as a user with sudo privileges. → How to Create a Sudo User and a Sudo Group on Ubuntu.
Installing Desktop Environment
Most servers don’t have a desktop environment installed, so I’ll start by installing a lightweight desktop environment.
There are several desktop environments (DE) available in the Ubuntu repositories. In this tutorial we will install Xfce. It is a fast, stable and lightweight desktop environment, which makes it ideal for use on remote servers.
First update your system with:
$ sudo apt update
$ sudo apt upgrade
Then, type the following command to install Xfce on your server:
$ sudo apt install xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils
Depending on your system, downloading and installing the Xfce package may take some time.
How to Install VNC Server on Ubuntu
There are also several different VNC servers available in the Ubuntu repositories such as TightVNC, TigerVNC and x11vnc. Each VNC server has different advantages and disadvantages in terms of speed and security.
Here I will install TigerVNC, which actively maintains high-performance VNC servers.
Type the following command to install TigerVNC on your Ubuntu server:
$ sudo apt install tigervnc-standalone-server tigervnc-common
Now that the VNC server is installed, the next step is to run the command
vncserver which will create the initial configuration and set a password. Don’t use sudo when running the following commands:
You will be asked to enter and confirm a password and whether to set it as a view-only password. If you choose to set a view-only password, users will not be able to interact with the VNC instance with a mouse and keyboard.
Output You will require a password to access your desktops. Password: Verify: Would you like to enter a view-only password (y/n)? n /usr/bin/xauth: file /home/verandalounge/.Xauthority does not exist New 'server2.verandalounge.co.uk:1 (verandalounge)' desktop at :1 on machine server2.verandalounge.co.uk Starting applications specified in /etc/X11/Xvnc-session Log file is /home/verandalounge/.vnc/server2.verandalounge.co.uk:1.log Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/verandalounge/.vnc/passwd :1 to connect to the VNC server.
The first time the vncserver command is run, it will create and save a password file in the directory
~ / .vnc which will be created if there isn’t.
:1 after hostname in the output shown above. It shows the display port number where the vnc server is running. In my case, the server is running on a TCP port
5901 (5900 +1). If you create a second instance with
vncserver, it will run on the next free port ie
:2 which means the server is running on the port
5902 (5900 + 2).
The important thing to remember is that when working with a VNC server,
:X is the display port that it refers to
Before continuing with the next step, first stop the VNC instance using the command
vncserver with options
-kill and the server number as arguments. In my case the server is running on the port
:1), so I’ll stop it by:
$ vncserver -kill :1
Output Killing Xtigervnc process ID 7264... success!
How to Configure a VNC Server
Now that we have Xfce and TigerVNC installed on our server, we need to configure TigerVNC to use Xfce. To do this, create the following files:
$ nano ~/.vnc/xstartup
Output #!/bin/sh unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS exec startxfce4
Save and close the file. The command above will be executed automatically every time you start or restart the TigerVNC server.
~/.vnc/xstartup also need to have execute permission. Run the command
chmod following to make sure the permissions are correct:
$ chmod u+x ~/.vnc/xstartup
If you have to pass additional options to the VNC server you can create a file named
config and add one option per line. Here’s an example:
Create a Systemd unit file
I’m going to create a systemd unit file which will allow me to start, stop, and restart the VNC service easily, just like any other systemd service.
Open your text editor and copy and paste the following configuration into it. Make sure to change the username on line 7 to match your username.
$ sudo nano /etc/systemd/system/[email protected]
[Unit] Description=Remote desktop service (VNC) After=syslog.target network.target [Service] Type=simple User=verandalounge PAMName=login PIDFile=/home/%u/.vnc/%H%i.pid 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 [Install] WantedBy=multi-user.target
Save and close the file.
Tell systemd that we are creating 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]
1 after the sign
@ specifies the display port on which the VNC service will run. This means that the VNC server will listen on the port
5901, as we discussed in the previous section.
Start the VNC service by running:
$ sudo systemctl start [email protected]
Verify that the service starts successfully with:
$ sudo systemctl status [email protected]
Output [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
Connect to a VNC server
VNC is not an encrypted protocol and is subject to packet sniffing. The recommended approach is to create an SSH Tunnel that will safely forward traffic from your local machine on the port
5901 to the server on the same port.
Set up SSH Tunneling on Linux and macOS
If you are 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:127.0.0.1:5901 -N -f -l username server_ip_address
You will be asked to enter the user’s password.
Don’t forget to replace
server_ip_address with your username and server IP address.
Set up SSH Tunneling in Windows
If you are running Windows, you can set up SSH Tunneling using PuTTY SSH client.
Open PuTTY and enter your server’s IP Address in the field
Host name or IP address.
Under the menu Connection, choose SSH and choose Tunnels. Enter the VNC server port (
5901) in the field Source Port and enter
server_ip_address:5901 in the field Destinations and click the button Add as shown in the image below:
Return to page Session to save the settings so you don’t have to enter them every time you want to use them. Now all you need to do is select a saved session and login to the remote server with the click of a button Open.
Connect using Vncviewer
Now that you have setup SSH tunneling, it is time to open your Vncviewer and to connect to VNC Server on
You can use any VNC Viewer such as TigerVNC, TightVNC, RealVNC, UltraVNC Vinagre and VNC Viewer for Google Chrome.
In this example we will use TigerVNC. Open your VNC viewer, enter
localhost:5901 and click the button Connect.
Enter your user password when prompted and you will see the default Xfce desktop. It should look something like this:
You can start interacting with the XFCE desktop remotely from your local computer using the keyboard and mouse.
Now that you can Install and configure a VNC Server and run a VNC server, 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 to start the display to create more than one user, make the initial configuration and set a password using the command
vncserver. You will also need to create a new service file using a different port.