Install and Configure VNC on Ubuntu 20.04

install and configure vnc on Ubuntu 20.04

VNC stands for Virtual Network Computing. It helps you to control your Ubuntu machine remotely. VNC server uses the Remote Frame Buffer protocol (RFB) to remotely control another computer.

When a technical support staff use keyboard or mouse, VNC will transmit the signals from keyboard and mouse to the remote server. It makes the administration works more easy and convenient with a graphical user interface. Moreover, VNC is more reliable than graphical SSH and RDP connections.

This tutorial will go through the steps of setting up and configuring VNC on Ubuntu 20.04 using TightVNC. If you are running a VNC on the previous version of Ubuntu 16.04, there is a guide for it.

Install Desktop Environment and VNC server

Firstly, let's install a Desktop Environment on your Ubuntu server because it's not installed by default. There are many choices: GNOME, MATE, XFCE, and so on.

In this tutorial, we will choose the XFCE. It is a lightweight desktop environment and it help the VNC connection will be stable and smooth.

On your Ubuntu server, run the following command to update the packages list:

$ sudo apt update

Then, let's install the XFCE desktop environment:

$ sudo apt install xfce4 xfce4-goodies gnome-icon-theme

Here I am using TightVNC server, other popular VNC servers are TigerVNC and x11vnc.

Install tightvnc:

$ sudo apt install tightvncserver

Next, let's run a VNC server instance and you have to set a password for the installed that server:

$ vncserver

As we have are starting server first time after installation it will ask us to set a password that client use to connect. It will also ask to set view-only password which will allow the user to see the screen but not interact with it.

start the vncserver

start the vncserver

Noted that the password length is between 6 and 8 characters. If you enter the longer password, it will be automatically trim off.

If you want to change the password of the VNC server, let's run:

$ vncpasswd

Configure the VNC server

When you start VNC server instance for the first time, the initial configuration file will be created: '~/.vnc/xstartup' and the VNC server listens on port 5901. We call this port as a display port. We can launch many instances of the VNC server and each instance has its own display ports: the first display listens on port 5901, the second display listens on port 5902 and so forth.

It's necessary to configure the VNC server to run along with the Ubuntu server startup. Besides, you have to explicitly indicate which Desktop Environment the VNC server will connect to.

Firstly, let's stop the current VNC server instance which is running on TCP port 5901:

$ vncserver -kill :1

vncserver -kill :1

Then backup the current configuration file:

$ cp ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now, modify the 'xstartup' file with your favorite editor as below:

#!/bin/sh

xrdb $HOME/.Xresources
startxfce4 &

Restart the VNC server:

$ vncserver -localhost
vncserver -localhost

vncserver -localhost

Connect to the VNC server

By default, the VNC server doesn't use the secure protocol. In order to secure the connection to the remotely Ubuntu server, you have to create a SSH tunnel and indicate that the VNC client connect to VNC server through that tunnel.

Let's establish a SSH connection on the local machine that forwards traffic to the 'localhost' for VNC. Launch the terminal on your local machine (macOS or Linux), run the following command:

$ ssh -L 59000:localhost:5901 -C -l <user> <server_ip_address>

Where:

-L 59000:localhost:5901: the port on the local machine is 59000 will be forwarded to the destination server: localhost:5901

-C: compress the data to speed up the connection

-l <user> <server_ip_address>: indicate the user that you will login to and the IP address of the Ubuntu 20.04 server

Set up SSH Tunneling on Windows

In case of running a Windows OS, you can setup the SSH tunnel by using PuTTY SSH client.

Let's open the PuTTY then enter the IP address of your Ubuntu server:

Ip address of on Putty

enter Ip address of Ubuntu server on Putty

Next, expand the submenu Connection > SSH > Tunnels, enter '5901' in the 'Source port' and '<server_ip_address>:5901' in the 'Destination'. Then, click on 'Add' button as the below:

create ssh tunnel on putty

ssh tunnel on putty

By using the SSH tunnel, the VNC connection to your Ubuntu 20.04 server only allow machines which can ssh to the server. It doesn't allow every machine just connects to port 5901.

Now, you can connect to the remotely Ubuntu server using the a VNC client. In this tutorial, we use the VNC viewer. Open the VNC client then enter the server IP address and the port 5901:

Enter the Ubuntu server IP address and port 5901

Enter the Ubuntu server IP address and port 5901

You have successfully connected to the XFCE desktop of your Ubuntu server:

vnc viewer

vnc viewer

You can control the server as if you're working with a physical server in your office. For example, you can access the file manager, launch a program or open the terminal:

Control the Ubuntu server

Control the Ubuntu server

Setup the VNC server as a systemd service

By running the VNC server as a system service, you can easily start, stop or restart it whenever you want.

Let's create a new file called 'vncserver@.service' in the directory '/etc/systemd/system/' on your Ubuntu server:

$ sudo vim /etc/systemd/system/vncserver@.service

Add the following content to file 'vncserver@.service' and make sure to edit the values in line 7, 8, 9 and 13 match your environment.

vncserver@.service

vncserver@.service

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

[Service]
Type=forking
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu

PIDFile=/home/ubuntu/.vnc/%H:%i.pid
ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Then, reload the system daemon:

$ sudo systemctl daemon-reload

Enable the service:

$ sudo systemctl enable vncserver@1.service

Where:

@1.service: the first display associate with port 5901.

Output:

Created symlink /etc/systemd/system/multi-user.target.wants/vncserver@1.service → /etc/systemd/system/vncserver@.service.

Kill the instance is running:

$ vncserver -kill :1

Killing Xtightvnc process ID 3159

Then start that systemd service again:

$ sudo systemctl start vncserver@1

Now, you can verify whether the service runs correctly:

$ sudo systemctl status vncserver@1

Output:

● vncserver@1.service - Start TightVNC server at startup
     Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-08-16 17:37:51 UTC; 2min 2s ago
    Process: 4611 ExecStartPre=/usr/bin/vncserver -kill :1 > /dev/null 2>&1 (code=exited, status=2)
    Process: 4615 ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 -localhost :1 (code=exited, status>
   Main PID: 4623 (Xtightvnc)
      Tasks: 129 (limit: 7125)
     Memory: 187.5M
...

Conclusion

In this tutorial, we learned how to install and configure VNC server on Ubuntu 20.04. Now, you can manage your remotely Ubuntu server with a graphical user interface.

Thanks for reading and please leave your suggestion in the below comment section.

Bobbin Zachariah 1:20 pm

Comments

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