Objective

The objective is to setup VNC server on Ubuntu 18.04 Bionic Beaver Linux.

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Difficulty

MEDIUM

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Instructions

Ubuntu VNC server setup

Let's start by the installation of the VNC server and the Xfce desktop manager core files:
$ sudo apt install vnc4server xfce4 xfce4-goodies
Once the VNC server is installed we can begin the configuration by setting up a user password to be used by a VNC client when creating a remote connection:
$ vncpasswd
Next, create the ~/.vnc/xstartup file to start the Xfce4 desktop:
$ mkdir ~/.vnc
$ nano ~/.vnc/xstartup
Insert the the following content and save:
#!/bin/bash
startxfce4 &
Lastly, make the ~/.vnc/xstartup file executable:
$ chmod +x ~/.vnc/xstartup
At this stage we are ready to start the VNC server. For this simply run the vncserver command from your home directory:
$ vnc4server

New 'ubuntu:1 (linuxconfig)' desktop is ubuntu:1

Starting applications specified in /home/linuxconfig/.vnc/xstartup
Log file is /home/linuxconfig/.vnc/ubuntu:1.log
The VNC server will open a new port for every new VNC desktop you create. Your Ubuntu system should now be listening on the port 5901 for incoming VNC connections:
$ ss -ltn
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128    0.0.0.0:22                 0.0.0.0:* 
LISTEN      0      128    0.0.0.0:6001               0.0.0.0:* 
LISTEN      0      128       [::]:22                    [::]:*  
LISTEN      0      5            *:5901                     *:* 
In case you have the UFW firewall enabled, open the port 5901 for incoming connections or see below how to tunnel the VNC connections via the SSH protocol:
$ sudo ufw allow from any to any port 5901 proto tcp
Rule added
Rule added (v6)
If you wish to make your firewall rules more strict or allow range of ports for multiple VNC sessions visit our How to Open/Allow incoming firewall port guide for more information.


Connect to VNC server

You can now use any VNC client to connect to your new Ubuntu VNC server. This guide will use the xvnc4viewer client.

To install xvnc4viewer on your Ubuntu client system run:
$ sudo apt install xvnc4viewer
Once you have your VNC client installed you can establish a new connection to your VNC server with a host name eg. ubuntu-vnc-server using the following linux command:
$ vncviewer ubuntu-vnc-server:1
Enter your password created previously as part of the above "Ubuntu VNC server setup" section.

If all went well, you will be presented with a Xfce4 desktop session running on the remote VNC server Ubuntu system:
Ubuntu VNC server - 18.04 Bionic Beaver
Established VNC connection to Ubuntu 18.04 VNC server.
Note, it is also possible to run a secure VNC client/server connection via the SSH tunnel. Given that you have the SSH user access (in this case username linuxconfig is used) to your VNC server eg. ubuntu-vnc-server.

First, create an SSH tunnel on a local port 5901 leading to a remote port 5901 on your VNC server.

Example:
$ ssh -L 5901:127.0.0.1:5901 -N -f -l linuxconfig ubuntu-vnc-server
The above command will open a local port 5901 on a localhost loop-back network interface 127.0.0.1:
$ ss -ltn
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128       0.0.0.0:22                    0.0.0.0:*
LISTEN      0      5       127.0.0.1:631                   0.0.0.0:*
LISTEN      0      128     127.0.0.1:6010                  0.0.0.0:*
LISTEN      0      128     127.0.0.1:6011                  0.0.0.0:*
LISTEN      0      128       0.0.0.0:38299                 0.0.0.0:*
LISTEN      0      128     127.0.0.1:5901                  0.0.0.0:*
Next, use the local port 5901 to connect to a remote VNC server via the SSH tunnel:
$ vncviewer localhost:1


VNC server system startup script

Although the current configuration works, one might want to setup a systemd startup script in order to easily manage multiple VNC desktop sessions.

Create a new file /etc/systemd/system/vncserver@.service using your favorite text editor eg. nano:
$ sudo nano /etc/systemd/system/vncserver@.service
Once you have the file opened insert the following lines while replacing the linuxconfig username with username of your VNC user on Line 7 and Line 10. Optionally, change screen resolution settings and apply other vncserver options or arguments:
[Unit]
Description=Systemd VNC server startup script for Ubuntu 18.04
After=syslog.target network.target

[Service]
Type=forking
User=linuxconfig
ExecStartPre=-/usr/bin/vncserver -kill :%i &> /dev/null
ExecStart=/usr/bin/vncserver -depth 24 -geometry 800x600 :%i
PIDFile=/home/linuxconfig/.vnc/%H:%i.pid
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target
Next, reload the Systemd:
$ sudo systemctl daemon-reload
To start VNC desktop 1 enter:
$ sudo service vncserver@1 start
To stop VNC desktop 1 execute:
$ sudo service vncserver@1 stop
The following linux command will enable the VNC desktop 1 to start after reboot:
$ sudo systemctl enable vncserver@1
To start VNC desktop 2 enter:
$ sudo service vncserver@2 start
and so on..
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.