How to Install and Configure VNC on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install and configure VNC on Ubuntu 20.04, let's briefly understand - What is VNC?

VNC (Virtual Network Computing) is a technology that allows you to remotely access and control a computer from another device. It enables you to view the desktop of a remote computer and interact with it as if you were sitting in front of it.

With VNC, you can troubleshoot issues, provide technical support, or access files from anywhere with an internet connection. It offers a convenient and efficient way to manage computers remotely, making it popular among businesses and individuals.

In this tutorial, you will install and configure a VNC server on Ubuntu 20.04.

Advantages of VNC

  1. Remote Access: VNC allows you to access and control your computer from anywhere, providing flexibility and convenience.
  2. Technical Support: With VNC, IT professionals can troubleshoot and resolve issues on remote computers, saving time and resources.
  3. Collaboration: VNC enables seamless collaboration by allowing multiple users to view and control a shared desktop.
  4. File Transfer: VNC allows you to transfer files between local and remote computers securely and efficiently.
  5. Cross-Platform Compatibility: VNC works across different operating systems, allowing you to connect and control computers regardless of their platform.

Installing Desktop Environment

Ubuntu servers are handled via the command line and do not come pre-installed with a graphical environment. If you're using Ubuntu on your desktop, you may skip this step.

In the Ubuntu repository, you may get a variety of desktop environments. Installing Gnome, which is Ubuntu 20.04's default desktop environment, is one option. Installing Xfce is another alternative. It's a lightweight, fast, and stable desktop environment that's ideal for use on a remote server.

We'll install Xfce in this tutorial. As a user with sudo capabilities, run the following commands:

sudo apt update
sudo apt install xfce4 xfce4-goodies

Downloading and installing Xfce packages may take some time depending on your system.

Install VNC Server

TightVNC, TigerVNC, and x11vnc are just a few of the VNC servers accessible in the Ubuntu repository. In terms of speed and security, each VNC server has its own set of strengths and weaknesses.

TigerVNC will be installed. It is a high-performance VNC server that is actively maintained. To install the package, use the following command:

sudo apt install tigervnc-standalone-server

Configure VNC Access

The next step is to build the initial user configuration and configure the password after the VNC server has been installed.

The vncpasswd command is used to change the user's password. When running the following command, do not use sudo:

vncpasswd

You'll be asked to enter and confirm your password, as well as whether you want it to be view-only. The user will not be able to interact with the VNC instance with the mouse or keyboard if you opt to set up a view-only password.

Output

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

The password file is kept in the /.vnc directory, which is generated if it doesn't exist already.

The next step is to set up TigerVNC to use Xfce. Create the following file to do so:

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

The file should be saved and closed. When you start or restart the TigerVNC server, the commands above are automatically executed.

Execute permissions are also required for the /.vnc/xstartup file. To change the permissions of a file, use the chmod command:

chmod u+x ~/.vnc/xstartup

Create a file named config and add one option per line if you need to send more options to the VNC server. Here's an illustration:

geometry=1920x1080
dpi=96

The vncserver command can now be used to start the VNC server:

vncserver
Output

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

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

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

In the output above, take note of the :1 after the hostname. The number of the display port on which the vnc server is executing is displayed here. The server is listening on TCP port 5901 (5900+1) in this example. If you use vncserver to create a second instance, it will run on the next available port, which is :2, which means the server will be running on port 5902 (5900+2).

It's vital to understand that while working with VNC servers, :X refers to 5900+X as a display port.

You may receive a list of all the VNC sessions that are currently active by typing:

vncserver -list
Output

TigerVNC server sessions:

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

Stop the VNC instance using the vncserver command with the -kill option and the server number as an argument before moving on to the next step. Because the server is listening on port 5901 :1 in this example, we'll shut it down with:

vncserver -kill :1
Output

Killing Xtigervnc process ID 5710... success!

Create a Systemd Unit File

Rather than beginning the VNC session manually, let's develop a systemd unit file that will start, stop, and restart the VNC service as needed.

Copy the following settings into your text editor and paste it. Make sure that the username on line 7 is the same as yours.

sudo nano /etc/systemd/system/vncserver@.service
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple
User=vegastack
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

The file should be saved and closed.

Systemd should be notified that a new unit file has been created:

sudo systemctl daemon-reload

Allow the service to start automatically when the computer boots up:

sudo systemctl enable vncserver@1.service

The display port on which the VNC service will run is defined by the number 1 after the @ symbol. This means that, as we described in the previous section, the VNC server will listen on port 5901.

Run the following command to start the VNC service:

sudo systemctl start vncserver@1.service

Verify that the service has been successfully started by performing the following steps:

sudo systemctl status vncserver@1.service
Output

● vncserver@1.service - Remote desktop service (VNC)
     Loaded: loaded (/etc/systemd/system/vncserver@.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-03-26 20:00:59 UTC; 3s ago
...

Connect to VNC server

VNC is not an encrypted protocol, therefore packet sniffing is possible. Creating an SSH tunnel and securely forwarding traffic from your local machine on port 5901 to the server on the same port is the preferred way.

Set Up SSH Tunneling on Linux and macOS

If your machine runs Linux, macOS, or any Unix-based operating system, you can quickly set up an SSH tunnel using the command:

ssh -L 5901:127.0.0.1:5901 -N -f -l vagrant 192.168.33.10

You will be asked for your user password.

Replace username and server_ip_address with your username and your server's IP address.

Set Up SSH Tunneling on Windows

If you're using Windows, you can use the PuTTY SSH client to set up SSH Tunneling.

In the Host name or IP address field of Putty, type your server's IP address.

Expand SSH and select Tunnels from the Connection menu box. In the Source Port field, type 5901, and in the Destination field, type server_ip_address:5901, then click the Add button.

Return to the Session page to save your options, so you don't have to type them in every time. Select the saved session on the remote server and click the Open button.

Connect using Vncviewer

Now that the SSH tunnel has been established, you can start your Vncviewer and connect to the VNC Server at localhost:5901.

Any VNC viewer, including TigerVNC, TightVNC, RealVNC, UltraVNC, Vinagre, and VNC Viewer for Google Chrome, can be used.

TigerVNC will be used. Open the viewer, type localhost:5901 into the address bar, then hit the Connect button.

When prompted, enter your user password, and the default Xfce desktop should appear.

You can use your keyboard and mouse to interact with the remote XFCE desktop from your local workstation.

FAQs to Install and Configure VNC on Ubuntu 20.04

How do I configure VNC on Ubuntu 20.04?

Configure VNC on Ubuntu 20.04 by creating a VNC configuration file, setting up a password, and specifying the display resolution.

How do I start the VNC server on Ubuntu 20.04?

Start the VNC server on Ubuntu 20.04 by executing the command vncserver in the terminal. It will generate a unique display number.

How do I connect to the VNC server on Ubuntu 20.04?

Connect to the VNC server on Ubuntu 20.04 using a VNC viewer application. Enter the server's IP address and display number to establish a connection.

How do I set up a secure VNC connection on Ubuntu 20.04?

Secure your VNC connection on Ubuntu 20.04 by tunneling VNC traffic through SSH. Use the command ssh -L 5901:localhost:5901 -N -f -l [username] [server_ip] before connecting with a VNC viewer.

Can I use VNC to share my desktop with others on Ubuntu 20.04?

Yes, you can share your Ubuntu 20.04 desktop with others using VNC. Just provide them with the IP address and display number to connect.

How can I transfer files between my local machine and the VNC server on Ubuntu 20.04?

Use a file transfer tool like scp or sftp to transfer files between your local machine and the VNC server on Ubuntu 20.04.

How do I change the VNC server settings on Ubuntu 20.04?

Modify the VNC server settings on Ubuntu 20.04 by editing the VNC configuration file located in the user's home directory (~/.vnc/xstartup).

Can I use VNC to access my Ubuntu 20.04 machine remotely from a different operating system?

Yes, VNC is cross-platform compatible, allowing you to access your Ubuntu 20.04 machine remotely from different operating systems like Windows, macOS, or Linux.

Conclusion

We hope this detailed tutorial helped you to install VNC on Ubuntu 20.04.

Create the basic setup and set up the password using the vncpasswd command to configure your VNC server to start a display for more than one user. A new service file with a different port must also be created.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.