VNC (Virtual Network Computing) is a software package which serves as a medium of connection between a user client and a remote computer. It creates a session with the remote server, which allows us to treat the remote server as if it were our machine, access the GUI of the remote server, and interact with it using the mouse and keyboard as a normal user.
This is a very convenient tool for users who are not comfortable with using the terminal and has the advantages of being very easy to use and available on almost all client operating systems, with minimal setup.
Let’s figure out how we could connect to our remote Ubuntu 18.04 server from our client, which can be anything from Windows, MacOS, or Linux.
NOTE: We are not going to look at setting up the TightVNC Client since it is a very simple setup on the TightVNC Website. So this tutorial aims to set up a VNC server on Ubuntu 18.04 and connect to it from a remote client.
Install a Desktop Environment on Ubuntu Server
By default, an Ubuntu 18.04 server does not come with a graphical desktop environment or a VNC server installed, so we must install that first.
On the Ubuntu server, first, update system critical packages first.
sudo apt update
Now, install the Xfce DE, using
sudo apt install xfce4 xfce4-goodies
We are ready to install the TigerVNC Server and its dependencies.
sudo apt install tigervnc-standalone-server tigervnc-common
Now, we have the basic requirements ready, so now let’s configure it to work properly.
Create Configuration Files
To complete the VNC server’s initial configuration after installation, use the
vncserver command to set up a secure password and create the initial configuration files.
This will prompt us to create a new password for secure access to the server remotely.
NOTE: The password MUST be between 6-8 characters long. Anything more than that will be truncated and reduced to 8 characters automatically, so it is very important to choose a correct password within 8 characters.
After you setup your password, this now creates some default configuration files for our Ubuntu TigerVNC Server.
When the VNCServer starts up, it may need to execute certain commands, such as connecting to a graphical environment. Since this is not provided automatically, we must modify appropriate configuration files.
But before that, let’s first terminate our existing VNC Session, since we’ve created our password now.
When VNC is first set up, it launches a default server instance on port
5901. This is the port that VNC uses to display the screen of the server to the connected client. Based on the connection number, a suitable reference suffix is associated with it. For example,
:2, denotes the first and the second VNC Server connection instances on port
5901. So, we can directly connect to or close VNC Server instances using this reference count.
For example, in our system, since we ran
vncserver, an instance is now running with a reference count of
:1. If we wish to kill that process, we simply type:
vncserver -kill :1
To kill all such instances, we use the wildcard character
vncserver -kill :*
You’ll get an output like this:
root@ubuntu:~# vncserver -kill :1 Killing Xtigervnc process ID 6268... success!
We need to create a startup script to run our Xfce4 Desktop Environment automatically, so that we can connect to the server remotely from a client even after the server reboots, so let’s do that now.
Create a Startup Script
The startup commands are located in a config file called xstartup in the $HOME/.vnc directory for any user (where $HOME is your Home Directory Environment Variable)
If that file does not exist, it’s not a problem. We will create a new file.
Now edit this file using a text editor and add the below script to it:
#!/bin/sh xrdb $HOME/.Xresources startxfce4 &
This looks into the user’s
Xresources file and starts the Xfce4 Desktop Environment as a background process.
Exit the editor after saving changes.
We must also make this script executable, so that our server can execute it. Let’s do that using chmod.
sudo chmod +x ~/.vnc/xstartup
Now, let’s start our VNC Server again:
You’ll get an output similar to this, indicating that we have loaded the DE from our startup script.
Let’s now connect to the server from our local client machine.
Connect to the VNC Server
Since VNC does not use any secure protocol to establish the connection, we need to setup one manually, for example, ssh.
Let’s create an SSH tunnel to connect securely to the server, and then tell the VNC Client to also use the SSH tunnel to make the connection, rather than an insecure direct connection.
Create the SSH Tunnel
We will make an SSH connection on our local client machine that forwards to the
localhost connection for VNC.
We will use the terminal for this (If you’re using Windows, make sure you use PuTTY)
ssh -L 5901:127.0.0.1:5901 SERVER_USER_NAME@SERVER_IP
We are using
ssh to bind to port 5901 of the remote connection to port 5901 on the client machine. The
-L option does this, using a port forwarding to localhost (127.0.0.1)
Therefore, we can connect to the server by making a connection from localhost:5901.
Once we are connected to the
ssh tunnel, let’s keep this running and leave this terminal session alone.
Now connect to the VNC Server using our VNC Client.
For me, I use
vncviewer on Linux to make the connection to the secure SSH Tunnel and finally control our remote server!
Enter the VNCServer password that you used to set it up, and you’re good to go!
Once you are connected, you’ll see the default Xfce desktop. It should look something like this:
To stop the session, press Ctrl + C, or close the window.
NOTE: Closing the SSH tunnel session will also close your VNC connection.
Thus, we have successfully connected our remote client to the Ubuntu Server using
In this tutorial, we showed you how you could setup a VNC Connection using TigerVNC on Ubuntu 18.04 Server.