Sign In
Sign In

How to Install VNC on Ubuntu

How to Install VNC on Ubuntu
Hostman Team
Technical writer
Ubuntu Servers
15.11.2024
Reading time: 8 min

If you need to interact with a remote server through a graphical interface, you can use VNC technology.
VNC (Virtual Network Computing) allows users to establish a remote connection to a server over a network. It operates on a client-server architecture and uses the RFB protocol to transmit screen images and input data from various devices (such as keyboards or mice). VNC supports multiple operating systems, including Ubuntu, Windows, macOS, and others. Another advantage of VNC is that it allows multiple users to connect simultaneously, which can be useful for collaborative work on projects or training sessions.

In this guide, we will describe how to install VNC on Ubuntu, using a Hostman cloud server with Ubuntu 22.04 as an example.

Step 1: Preparing to Install VNC

Before starting the installation process on both the server and the local machine, there are a few prerequisites to review. 

Here is a list of what you’ll need to complete the installation:

  1. A Server Running Ubuntu 22.04. In this guide, we will use a cloud server from Hostman with minimal hardware configuration.

Image3

  1. A User with sudo Privileges. You should perform the installation as a regular user with administrative privileges.

  2. Select a Graphical Interface. You’ll need to choose a desktop environment that you will use to interact with the remote server after installing the system on both the server and the local machine.

  3. A Computer with a VNC Client Installed

Currently, the only way to communicate with a rented server running Ubuntu 22.04 is through the console. To enable remote management via a graphical interface, you’ll need to install a desktop environment along with VNC on the server. Below are lists of available VNC servers and desktop environments that can be installed on an Ubuntu server.

VNC Servers:

  • TightVNC Server. One of the most popular VNC servers for Ubuntu. It is easy to set up and offers good performance.

  • RealVNC Server. RealVNC provides a commercial solution for remote access to servers across various Linux distributions, including Ubuntu, Debian, Fedora, Arch Linux, and others.

Desktop Environments:

  • Xfce. A lightweight and fast desktop environment, ideal for remote sessions over VNC. It uses fewer resources than heavier desktop environments, making it an excellent choice for servers and virtual machines.

  • GNOME. The default Ubuntu desktop environment, offering a modern and user-friendly interface. It can be used with VNC but will consume more resources than Xfce.

  • KDE Plasma. Another popular desktop environment that provides a wide range of features and a beautiful design.

The choice of VNC server and desktop environment depends on the user’s specific needs and available resources. TightVNC and Xfce are excellent options for stable remote sessions on Ubuntu, as they do not require high resources. In the next step, we will describe how to install them on the server in detail.

Step 2: Installing the Desktop Environment and VNC Server

To install the VNC server on Ubuntu along with the desktop environment, connect to the server and log in as a regular user with administrative rights.

  1. Update the Package List 

After logging into the server, run the following command to update the packages from the connected repositories:

sudo apt update
  1. Install the Desktop Environment 

Next, install the previously selected desktop environment. To install Xfce, enter:

sudo apt install xfce4 xfce4-goodies

Here, the first package provides the basic Xfce desktop environment, while the second includes additional applications and plugins for Xfce, which are optional.

  1. Install the TightVNC Server 

To install TightVNC, enter:

sudo apt install tightvncserver
  1. Start the VNC Server 

Once the installation is complete, initialize the VNC server by typing:

vncserver

This command creates a new VNC session with a specific session number, such as :1 for the first session, :2 for the second, and so on. This session number corresponds to a display port (for example, port 5901 corresponds to :1). This allows multiple VNC sessions to run on the same machine, each using a different display port.

During the first-time setup, this command will prompt you to set a password, which will be required for users to connect to the server’s graphical interface.

Image6

  1. Set the View-Only Password (Optional) 

After setting the main password, you’ll be prompted to set a password for view-only mode. View-only mode allows users to view the remote desktop without making any changes, which is helpful for demonstrations or when limited access is needed.

If you need to change the passwords set above, use the following command:

vncpasswd

Now you have a VNC session.

Image4

In the next step, we will set up VNC to launch the Ubuntu server with the installed desktop environment.

Step 3: Configuring the VNC Server

The VNC server needs to know which desktop environment it should connect to. To set this up, we’ll need to edit a specific configuration file.

  1. Stop Active VNC Instances 

Before making any configurations, stop any active VNC server instances. In this guide, we’ll stop the instance running on display port 5901. To do this, enter:

vncserver -kill :1

Image1

Here, :1 is the session number associated with display port 5901, which we want to stop.

  1. Create a Backup of the Configuration File 

Before editing, it’s a good idea to back up the original configuration file. Run:

mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
  1. Edit the Configuration File 

Now, open the configuration file in a text editor:

nano ~/.vnc/xstartup

Replace the contents with the following:

#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &
    • #!/bin/bash – This line is called a "shebang," and it specifies that the script should be executed using the Bash shell.
    • xrdb $HOME/.Xresources – This line reads settings from the .Xresources file, where desktop preferences like colors, fonts, cursors, and keyboard options are stored.
    • startxfce4 & – This line starts the Xfce desktop environment on the server.
  1. Make the Configuration File Executable

To allow the configuration file to be executed, use:

chmod +x ~/.vnc/xstartup
  1. Start the VNC Server with Localhost Restriction

Now that the configuration is updated, start the VNC server with the following command:

vncserver -localhost

The -localhost option restricts connections to the VNC server to the local host (the server itself), preventing remote connections from other machines. You will still be able to connect from your computer, as we’ll set up an SSH tunnel between it and the server. These connections will also be treated as local by the VNC server.

The VNC server configuration is now complete.

Step 4: Installing the VNC Client and Connecting to the Server

Now, let’s proceed with installing a VNC client. In this example, we’ll install the client on a Windows 11 computer.

Several VNC clients support different operating systems. Here are a few options: 

  • RealVNC Viewer. The official client from RealVNC, compatible with Windows, macOS, and Linux.
  • TightVNC Viewer. A free and straightforward VNC client that supports Windows and Linux.
  • UltraVNC. Another free VNC client for Windows with advanced remote management features.

For this guide, we’ll use the free TightVNC Viewer.

  1. Download and Install TightVNC Viewer

Visit the official TightVNC website, download the installer, and run it.

Image5

In the installation window, click Next and accept the license agreement.

Then, select the custom installation mode and disable the VNC server installation, as shown in the image below.

Image2

Click Next twice and complete the installation of the VNC client on your local machine.

  1. Set Up an SSH Tunnel for Secure Connection

To encrypt your remote access to the VNC server, use SSH to create a secure tunnel. On your Windows 11 computer, open PowerShell and enter the following command:

ssh -L 56789:localhost:5901 -C -N -l username server_IP_address

Make sure that OpenSSH is installed on your local machine; if not, refer to Microsoft’s documentation to install it.

This command configures an SSH tunnel that forwards the connection from your local computer to the remote server over a secure connection, making VNC believe the connection originates from the server itself. Here’s a breakdown of the flags used:

    • -L sets up SSH port forwarding, redirecting the local computer’s port to the specified host and server port. Here, we choose port 56789 because it is not bound to any service.
    • -C enables compression of data before transmitting over SSH.
    • -N tells SSH not to execute any commands after establishing the connection.
    • -l specifies the username for connecting to the server.
  1. Connect with TightVNC Viewer

After creating the SSH tunnel, open the TightVNC Viewer and enter the following in the connection field:

localhost:56789

You’ll be prompted to enter the password created during the initial setup of the VNC server.

Once you enter the password, you’ll be connected to the VNC server, and the Xfce desktop environment should appear.

  1. Stop the SSH Tunnel

To close the SSH tunnel, return to the PowerShell or command line on your local computer and press CTRL+C.

Conclusion

This guide has walked you through the step-by-step process of setting up VNC on Ubuntu 22.04. We used TightVNC Server as the VNC server, TightVNC Viewer as the client, and Xfce as the desktop environment for user interaction with the server.

We hope that using VNC technology helps streamline your server administration, making the process easier and more efficient.

Ubuntu Servers
15.11.2024
Reading time: 8 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us
Hostman's Support