How to Install VNC Server on Ubuntu

In this tutorial you will learn how to install VNC on Ubuntu and 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 graphical desktop over the network, Similar to Remote Desktop on Microsoft Windows.

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

  1. Open the Ubuntu terminal and use the following commands to install Xfce desktop session:

    sudo apt-get update
    sudo apt-get install xfce4 xfce4-session

    We are going to use Xfce desktop for the VNC session (Works Perfectly).

  2. Once Xfce is installed, Install the vnc4server package on Ubuntu:

    sudo apt-get install vnc4server -y
  3. When installation completes, start the VNC server for the first time:

    vncserver

    When you run vncserver command for the first time, You will prompt to create a password for the VNC connection. The command will also create necessary configuration files for the Ubuntu 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 "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 also needs a VNC Viewer to connect from their local computer. Ubuntu users can use the Vinagre remote desktop viewer. For Microsoft windows you can use RealVNC Viewer.

VNC 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, 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 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 Ubuntu system reboot. Following example will create a new systemd unit with desktop ID 1.

Example

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 Linux. 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.