How to Install VNC Server on Ubuntu

In this tutorial, you will learn how to install VNC on Ubuntu to access the graphical desktop from a remote computer.

VNC (Virtual Network Computing) is a cross-platform remote control utility that uses the Remote Frame Buffer (RFB) protocol, is a way to share a graphical desktop over the network, similar to Remote Desktop on Microsoft Windows.

You can use the following guide to install the VNC server on both Ubuntu 22.04 LTS and Ubuntu 20.04 LTS.

Automatically start the VNC server on startup

To Install and Configure VNC Server on Ubuntu 20.04, perform the following steps:

  1. Open the Ubuntu terminal and use the following commands to install the Xfce desktop session:
    sudo apt-get update
    sudo apt-get install xfce4 xfce4-session
    We will use the Xfce desktop for the VNC session (which works perfectly with Ubuntu Server). Select gdm3 if you are asked to set the display manager..
  2. Once Xfce is installed, install the tightvncserver package on Ubuntu:
    sudo apt-get install tightvncserver -y
  3. When the installation completes, start the VNC server for the first time (IMPORTANT: you must start the VNC server using the user account you will use to sign in to VNC, not from the root account. So do not run the following command as the root user):
    vncserver
    When you run the vncserver command for the first time, you will be prompted to create a password for the VNC connection. It will also create the necessary configuration files required for the VNC server.
  4. Next, we need to edit the startup configuration file. First, stop the VNC Server with the kill command:
    vncserver -kill :1
  5. Open the ~/.vnc/xstartup file:
    nano ~/.vnc/xstartup
    And make sure that the xstartup file is similar to the following configuration:
    #!/bin/sh
    unset SESSION_MANAGER
    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    startxfce4 &
  6. Save the configuration file and start the VNC Server:
    vncserver :1 -geometry 1366x768 -depth 24
    The remote session will use the 1366x768 resolution, and the desktop ID is 1.

The user needs a VNC Viewer to connect to Ubuntu from their local computer. Ubuntu users can use the tigervnc-viewer or Vinagre remote desktop viewer. For Microsoft Windows, you can use the RealVNC Viewer.

tigervnc viewer VNC Client

Enter your Ubuntu server IP address and the VNC desktop number to be connected.

Install VNC Server on Ubuntu

Multiple users can connect the Ubuntu system and work simultaneously, But you have to start multiple VNC sessions with different desktop IDs. For example, the following command will start a VNC session with desktop ID 2:

vncserver :2 -geometry 1366x768 -depth 24

To manually stop the VNC Server on Ubuntu, run the kill command followed by the desktop ID:

vncserver -kill :1

To change the VNC password, run the following command:

vncpasswd

Automatically Start the VNC Server on Startup

We need to create a systemd unit file to start the VNC server automatically when the Ubuntu server reboot. The following example will create a new systemd unit with desktop ID 1.

To create a systemd unit, create a file named vncserver@1.service inside the /etc/systemd/system directory and add the following configuration (Change User=your_username to your Linux username).

Unit]
Description=Start VNC Server at startup With Desktop ID 1.
After=multi-user.target network.target

[Service]
Type=forking
User=your_username
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver :%i -geometry 1366x768 -depth 24
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

Then reload the systemd manager and enable the vncserver@1.service:

sudo systemctl daemon-reload
sudo systemctl enable vncserver@1.service

Summary

In this tutorial, we learned how to install VNC on Ubuntu 20.04/18.04 LTS. VNC is a cross-platform Remote Desktop Server implementation that uses the Remote Frame Buffer (RFB) protocol.

The way it works, The VNC Server runs on your Ubuntu Server/Desktop, and the VNC client application runs on the computer from which you want to access the remote desktop.