How to Install and Configure VNC on Ubuntu 22.04

Choose a different version or distribution

Introduction

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

VNC, or Virtual Network Computing, is a remote desktop software that allows users to control and access another computer from a distance. It enables individuals to view and interact with the desktop of a remote computer as if they were physically present at that location. VNC is commonly used for technical support, collaboration, and accessing files on remote computers.

With its user-friendly interface and secure connection, VNC offers a convenient solution for remote access needs. Whether you are a business professional, an IT technician, or a home user, VNC simplifies remote computer control, making it an essential tool in today's interconnected world.

In this tutorial, you will install and configure a VNC server on Ubuntu 22.04. We will also address a few FAQs on how to install VNC on Ubuntu 22.04.

Advantages of VNC

  1. Remote Access: VNC enables users to access and control a remote computer from anywhere, facilitating efficient troubleshooting and remote collaboration.
  2. Cross-Platform Compatibility: VNC works seamlessly across different operating systems, allowing users to connect and control computers regardless of their platform.
  3. File Transfer: VNC provides the ability to transfer files between the local and remote computers, simplifying data sharing and collaboration.
  4. Screen Sharing: With VNC, users can share their screens with others, facilitating presentations, demonstrations, and team collaboration.
  5. Security: VNC offers secure encrypted connections, ensuring the privacy and protection of remote access sessions and data transfers.

Step 1 - Install 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 22.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.

Step 2 - 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

Step 3 - 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!

Step 4 - 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/bash -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

Make sure to change the User value with yours and then 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
...

Step 5 - Connect to the 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 22.04

How do I start VNC server on Ubuntu 22.04?

To start the VNC server, run the command tightvncserver :1 in the terminal. :1 represents the display number.

How do I connect to the VNC server from a remote computer?

Use a VNC client like RealVNC or TigerVNC on your remote computer and enter the IP address followed by the display number (e.g., 192.168.1.10:1) to connect.

Can I use VNC with a different desktop environment on Ubuntu 22.04?

Yes, VNC works with different desktop environments. You can install your preferred desktop environment and use it with VNC.

How do I set up VNC to start automatically on system boot?

Edit the file ~/.vnc/xstartup and uncomment the line that starts with exec and points to your preferred desktop environment.

How do I change the VNC server resolution on Ubuntu 22.04?

Modify the geometry parameter in the ~/.vnc/xstartup file to set the desired resolution (e.g., geometry 1920x1080).

Can I secure my VNC connection on Ubuntu 22.04?

Yes, you can secure your VNC connection by tunneling it through SSH or setting up a VNC server with SSL/TLS encryption.

How do I uninstall VNC from Ubuntu 22.04?

To uninstall VNC, run the command sudo apt-get remove tightvncserver in the terminal. This will remove the VNC server from your system.

Conclusion

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

Create the basic setup and set 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.