How to Install and Configure VNC on Ubuntu 20.04

Posted on

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

This article explains how to install and configure a VNC server on Ubuntu 20.04. We’ll also show you how to securely connect to the VNC server through an SSH tunnel.

Installing Desktop Environment

Ubuntu servers are managed from the command line and do not have a desktop environment installed by default. If you run the desktop version of Ubuntu, skip this step.

There are various desktop environments available in Ubuntu repositories. One option is to install Gnome, which is the default desktop environment in Ubuntu 20.04. Another option is to install Xfce . It is a fast, stable, and lightweight desktop environment, which makes it ideal for usage on a remote server.

In this guide, we’ll install Xfce. Enter the following commands as a user with sudo privileges :

sudo apt updatesudo apt install xfce4 xfce4-goodies

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

Installing VNC Server 

There are 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’ll be installing TigerVNC. It is an actively maintained high-performance VNC server. Type the following command to install the package:

sudo apt install tigervnc-standalone-server

Configuring VNC Access

Once the VNC server is installed, the next step is to create the initial user configuration and set up the password.

Set the user password using the vncpasswd command. Do not use sudo when running the command below:

vncpasswd

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

Password:
Verify:
Would you like to enter a view-only password (y/n)? n

The password file is stored in the ~/.vnc directory, which is created if not present.

Next, we need to configure TigerVNC to use Xfce. To do so, create the following file:

~/.vnc/xstartup

nano ~/.vnc/xstartup
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4 

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

The ~/.vnc/xstartup file also needs to have execute permissions. Use the chmod command to set the file permissions:

chmod u+x ~/.vnc/xstartup

If you need to pass additional options to the VNC server, create a file named config and add one op

You can now start the VNC server using the vncserver command:

vncserver
New 'server2.linuxize.com:1 (linuxize)' desktop at :1 on machine server2.linuxize.com

Starting applications specified in /home/linuxize/.vnc/xstartup
Log file is /home/linuxize/.vnc/server2.linuxize.com:1.log

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

Note the :1 after the hostname in the output above. This indicates the number of the display port on which the vnc server is running. In this example, the server is running on TCP port 5901 (5900+1). If you create a second instance with vncserver it will run on the next free port i.e :2, which means that the server is running on port 5902 (5900+2).

What is important to remember is that when working with VNC servers, :X is a display port that refers to 5900+X.

ou can get a list of all the currently running VNC sessions by typing:

vncserver -list
TigerVNC server sessions:

X DISPLAY #	RFB PORT #	PROCESS ID
:1		      5901		    5710

Before continuing with the next step, stop the VNC instance using the vncserver command with a -kill option and the server number as an argument. In this example, the server is running in port 5901 (:1), so we’ll stop it with:

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

Leave a Reply

Your email address will not be published. Required fields are marked *