How-to

How to Install VNC Server on Ubuntu 20.04

What is VNC?

VNC or Virtual Network Computing is an independent platform that allows you to remote and control another computer desktop with GUI (Graphical User Interface) over the network connection.

It’s desktop-sharing software that transmits keyboard and mouse events from one computer to another through the RFB (Remote Frame Buffer) protocol.

The VNC works on the client-server model architecture. A VNC Client uses the VNC viewer application (installed on the client-side) to connect and remote to the VNC server, which is the VNC Server components installed on it.

In this tutorial, you will learn how to install VNC Server on Ubuntu 20.04 Server. You will install Graphical Desktop Environment XFCE, setup the VNC Server, and connect securely to the VNC server through SSH Tunnel.

Prerequisites

For this tutorial, make sure you have got a Ubuntu 20.04 server with root privileges. Also, you need to install a VNC Viewer on your local computer.

Below is the tutorial about the Ubuntu 20.04 server installation.

Install Ubuntu 20.04 Server

Before going any further, log in to your Ubuntu server and type the sudo command to get the root privileges on your system.

sudo su

Step 1 – Install XFCE Desktop and VNC Server on Ubuntu 20.04

First, you will install the XFCE Desktop Environment (DE) and the ‘tightvncserver’ packages. The main advantage of using XFCE Desktop as default DE for VNC Server is because it’s lightweight with rich-features and running with low-memory consumption.

Update all packages list and install XFCE Desktop and VNC Server software using the apt command below.

apt update
apt install xfce4 xfce4-goodies tightvncserver

During the installation, you will get the prompt for the default ‘Display Manager’ configuration.

Setup Display Manager VNC Server

Select the ‘gdm3‘ as default display manager and ‘OK‘ to continue.

Once all installation packages are finished, move to the next stage.

Step 2 – Create a New User

For this tutorial, you will run the VNC Server for a non-root user but have the root/sudo privileges.

For this example, the VNC Server will run as a user ‘chaewon’.

Create a new user ‘chaewon’ and set up its password using the following commands.

useradd -m -s /bin/bash chaewon
passwd chaewon

Now type the strong password for user ‘chaeowon’.

After that, add the ‘chaewon’ user to the ‘sudo’ group, so the user will be able to run the sudo command to get the root privileges.

usermod -a -G sudo chaewon

Now verify the new user and root privilege using the following command.

su - chaewon
sudo su

Type your password for the user ‘chaewon’ and you will get the root shell as below.

Create a New User

As a result, you’ve created a new user ‘chaewon’ with the root/sudo privileges through the ‘sudo’ group on the Ubuntu system.

Step 3 – Set up VNC Server with XFCE Desktop on Ubuntu 20.04

In this step, you will set up the VNC Server with the default XFCE as a default Desktop Environment. You will initialize the VNC Server configuration and password, setup the XFCE as default DE, and verify both XFCE and VNC Server configurations.

– Initialize VNC Server Configuration and Password

Before going any further, login to the user ‘chaewon’ using the following command.

su - chaewon

Next, initialize the VNC Server configuration using the ‘vncserver’ command as below.

vncserver

Now type your VNC Server password (6-8 characters) and verify it.

You will require a password to access your desktops.

Password: 
Verify:

After that, you will get the prompt for the ‘view-only’ password configuration. The ‘view-only’ password will allow you to limit the user from using their mouse and keyboard to control the VNC Server desktop. This is useful for demonstrating to other users, but not required.

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

Once the initialization is complete, you will be displayed the following output.

xauth: file /home/chaewon/.Xauthority does not exist

New 'X' desktop is sysadminjournal:1

Creating default startup script /home/chaewon/.vnc/xstartup
Starting applications specified in /home/chaewon/.vnc/xstartup
Log file is /home/chaewon/.vnc/sysadminjournal:1.log

As a result, you’ve successfully initialized the VNC Server configuration, and you’ve set up the VNC Server password. All configuration for VNC Server located at ‘~/.vnc’ directory.

Initialize VNC Server Configuration and Password

Also, the first VNC Server is running on the display-port ‘vnc-server:1’. You can launch multiple VNC Server process on other ports such as ‘:2’, ‘:3’, etc.

– Setup XFCE as Default DE for VNC Server

After initializing the VNC Server configuration and password, you will setup the XFCE Desktop as a default Desktop Environment for the VNC Server session.

first, kill the first VNC Server display-port using the following command.

vncserver -kill :1

Now you will be displayed the result as below.

Killing Xtightvnc process ID 19166

Next, backup the default VNC configuration “~/.vnc/xstartup” and create another one.

mv ~/.vnc/xstartup ~/.vnc/xstartup.orig
vim ~/.vnc/xstartup

Paste the following configuration to set up the XFCE as default Desktop Environment for the VNC Server.

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
xrdb $HOME/.Xresources
startxfce4 &

Save and close.

Next, make the VNC Server startup script executable using the chmod command below.

chmod +x ~/.vnc/xstartup

As a result, you’ve successfully set up the XFCE as default DE for your VNC Server.

Additionally, you can create the “~/. Xresources” configuration to set up the default DPI, anti-aliasing, hinting, terminal colors, terminal preferences, etc.

Create a new “~/.Xresources” configuration using vim editor.

vim ~/.Xresources

Paste the following configuration into it.

##############
# Xft settings
##############
Xft.dpi: 96
Xft.antialias: true
Xft.rgba: rgb
Xft.hinting: true
Xft.hintstyle: hintslight

Save and close.

As a result, you’ve created the “~/. Xresources” file to configure the default DPI, anti-aliasing, and hinting.

– Verify the VNC Server and XFCE Desktop Configuration

In this stage, you will verify all configurations of the XFCE Desktop with the VNC Server.

Verify the XFCE Desktop and VNC Server configuration using the following command.

vncserver

Now make sure there is no error, and you will be displayed the result as below.

New 'X' desktop is sysadminjournal:1

Starting applications specified in /home/chaewon/.vnc/xstartup
Log file is /home/chaewon/.vnc/sysadminjournal:1.log

As a result, the VNC Server is up and running with the XFCE as the default DE.

Setup VNC Server with XFCE Desktop Environment

Step 4 – Set up VNC Server as a Systemd Service on Ubuntu 20.04

After configuring the VNC Server, you will set up the VNC Server as a systemd service.

Log in to the user ‘chaewon’ using the following command.

su - chaewon

Allow the user ‘chaewon’ to run the Systemd services and create a new environment variable ‘XDG_RUNTIME_DIR’ using the following command.

loginctl enable-linger chaewon
export XDG_RUNTIME_DIR=/run/user/`id -u`

Now create a new systemd service directory and create a new service file ‘[email protected]’.

mkdir -p ~/.config/systemd/user/
vim ~/.config/systemd/user/[email protected]

Paste the following configuration into it.

[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver -geometry 1800x1000 -depth 16 -dpi 120 -alwaysshared -localhost %i
ExecStop=/usr/bin/vncserver -kill %i

[Install]
WantedBy=multi-user.target

Save and close.

Next, reload the systemd manager and make sure there is no error.

systemctl --user daemon-reload

Setup VNC Server as a Systemd Service

Then start the VNC Server service for Desktop session number ‘1’ and add it to the system boot. After that, check the vnc service status using the systemctl command below.

systemctl --user enable --now [email protected]:1.service
systemctl --user status [email protected]:1.service

Below is the result you will get.

Check VNC Server Service Status

As a result, the VNC Server with the XFCE desktop is up and running as a systemd service.

The default VNC Server is running on TCP port ‘5901’, check all available port using the ss command below.

ss -plnt4

You will get the TCP port ‘5901’ is used by the ‘Xvnc’ application.

Step 5 – Set up UFW Firewall

In this step, you will add the VNC Server port ‘5901’ to the ufw firewall configuration.

Back to your root user and add the ssh and VNC Server port ‘5901’ using the ufw command below.

for svc in ssh 5901/tcp
do
ufw allow $svc
done

Next, run and enable the ufw firewall using the following command.

ufw enable

Type ‘y‘ to enable the ufw firewall.

Below is the result you will get.

Add Port to UFW Firewall

As a result, you’ve added the ssh and VNC Server port ‘5901/tcp’ to the ufw firewall configuration.

And finally, you’re ready to connect to your VNC Server.

Step 6 – Install VNC Server on Client

To connect to the VNC Server, you must install the VNC Viewer software on your local computer. You can use UltraVNC, RealVNC, or TigerVNC Viewer.

For Windows users, download and install RealVNC Viewer from the following URL.

RealVNC Viewer for Windows

And cor Mac users, download and install RealVNC Viewer from the following URL.

RealVNC Viewer for Mac

For Ubuntu users, you can install the ‘TigerVNC Viewer’ from the Ubuntu repository using the apt command below.

apt install tigervnc-viewer

Optionally, you use RealVNC for Debian based operating system.

Download the RealVNC Package for Linux Debian from the URL below.

RealVNC Viewer for Linux Debian based OS

Now install the RealVNC Viewer using the “dpkg” command below.

sudo dpkg -i VNC-Viewer-*.deb

As a result, you’ve successfully installed the VNC Viewer software to your local computer.

RealVNC Viewer

Step 7 – Connect to VNC Server with SSH Tunneling

It’s important to connect to your VNC Server securely. To do that, you can use the special VPN connection installed on the server, or using the simple SSH tunneling.

For this step, you will learn how to connect to the VNC Server securely through the SSH Tunneling.

First, create a new SSH tunneling connection to the VNC Server port ‘5901‘ using the SSH command below.

ssh -L 5901:127.0.0.1:5901 -C -N -l chaewon 10.5.5.25

Change the ‘chaewon’ user with your own and type your password.

Next, open your VNC Viewer software (we’re using RealVNC Viewer) and type the localhost address ‘127.0.0.1‘ with default VNC Server port ‘5901‘ and press Enter to connect.

Now you will be prompted about the unsecure connection to ‘localhost’, it’s fine because you’re connected locally, click the ‘Continue‘ button.

Connect with SSH Tunneling

Now type your VNC Server password and click “OK” to continue.

Password Authentication

And you you will get the default XFCE Desktop.

VNC Server with XFCE Desktopn on Ubuntu 20.04

As a result, you’ve successfully connected to the VNC Server securely through the SSH Tunneling.

Finally, you’ve installed the VNC Server on Ubuntu 20.04 Server with the XFCE as a default Desktop Environment. Also, you’ve successfully connected securely to the VNC Server through SSH Tunneling.

2 Comments

  1. There is no title bar for the windows in vnc session. In your last screenshot of XFCE desktop, there is no title bar for the terminal either. How to fix it?

    • hakase Reply

      Open your terminal – remove the xfce cache session – log out and log in again.

      Run the following command to remove the XFCE cache session.

      rm ~/.cache/sessions/xfce4-session*

Write A Comment