Install and Configure VNC Server on Ubuntu 16.04 | 18.04

This brief tutorial shows students and new users how to install Virtual Network Computing (VNC) server on Ubuntu 16.04 | 18.04 servers to allow remote connection via VNC protocol…

VNC is a graphical desktop sharing system that allows users to connect remotely to a system with keyboard and mouse access… This is an open source alternative to Microsoft Remote Desktop Protocol…

When you’re ready to get started with installing and configure VNC server on Ubuntu, follow the steps below:

Step 1: Install Desktop Environment

By default Ubuntu servers don’t come with a desktop environment and VNC requires desktop environment to function… Since we don’t want to install a full-blown desktop environment, a better alternative is a lightweight environment called Xfce..

To intall Xfce, run the commands below

sudo apt update
sudo apt install xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils

Step 2: Install VNC Server

Now that a lightweight desktop environment is installed, run the commands below to install VNC server… There many VNC servers available, including TightVNC, x11vnc and TigerVNC…. for this post, we’re going to be using TigerVNC...

To install TigerVNC, run the commands below:

sudo apt install tigervnc-standalone-server tigervnc-common

After installing TigerVNC, run the commands below to setup an initial setup including creating a password to access the desktops…

vncserver

That should output similar message as below:

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? n

New 'ubuntu1804:1 (richrd)' desktop at :1 on machine ubuntu1804

Starting applications specified in /etc/X11/Xvnc-session
Log file is /home/richrd/.vnc/ubuntu1804:1.log

Use xtigervncviewer -SecurityTypes VncAuth -passwd /home/richrd/.vnc/passwd :1 to connect to the VNC server.

vncserver: Failed command '/etc/X11/Xvnc-session': 256!

=================== tail -15 /home/richrd/.vnc/ubuntu1804:1.log ===================
Starting applications specified in /etc/X11/Xvnc-session has failed.
Maybe try something simple first, e.g.,
    tigervncserver -xstartup /usr/bin/xterm

After that, vncserver command will create a password file and store it in your ~/.vnc folder…

Step 3: Configure VNC Server

Now that VNC server is installed, run the commands below to configure it…

vncserver -kill :1

Then create a startup config file..

nano ~/.vnc/xstartup

add the lines into the file and save..

#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4

Run the commands below after the above..

chmod u+x ~/.vnc/xstartup

Next, create a systemd file..

sudo nano /etc/systemd/system/vncserver@.service

Then add the lines below into the file and save..

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple
User=richard
PAMName=login
PIDFile=/home/%u/.vnc/%H%i.pid
ExecStartPre=/bin/sh -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

After saving the file run the commands below to reload systemd config…

sudo systemctl daemon-reload

Start and enable VNC server

sudo systemctl start vncserver@1.service
sudo systemctl enable vncserver@1.service

After check to see if VNC is started and running…

sudo systemctl status vncserver@1.service

You should see something similar to the one below

● vncserver@1.service - Remote desktop service (VNC)
   Loaded: loaded (/etc/systemd/system/vncserver@.service; indirect; vendor preset: enabled)
   Active: active (running) since Mon 2019-02-04 15:22:07 CST; 17s ago
 Main PID: 14025 (vncserver)
    Tasks: 0 (limit: 4663)
   CGroup: /system.slice/system-vncserver.slice/vncserver@1.service
           ‣ 14025 /usr/bin/perl /usr/bin/vncserver :1 -geometry 1440x900 -alwaysshared -fg

Feb 04 15:22:06 ubuntu1804 systemd[1]: Starting Remote desktop service (VNC)...
Feb 04 15:22:07 ubuntu1804 systemd[14011]: pam_unix(login:session): session opened for user richrd by (uid=0)
Feb 04 15:22:07 ubuntu1804 systemd[1]: Started Remote desktop service (VNC).
Feb 04 15:22:07 ubuntu1804 systemd[14025]: pam_unix(login:session): session opened for user richrd by (uid=0)

Now you’re all set… Find a VNC client and attempt to the server…

A good VNC to use it VNC Viewer from RealVNC… Install it and connect to the server…

server_name:5901

Ubuntu VNC server

Type in the password you created above and connect

Ubuntu vnc server

Enjoy!

ubuntu install

Congratulations! You’ve successfully installed, configure and connected to a VNC server on Ubuntu 16.04 | 18.04

You may also like the post below:

Install CMSUno on Ubuntu 16.04 / 18.04 with Apache2 and PHP 7.2

One Reply to “Install and Configure VNC Server on Ubuntu 16.04 | 18.04”

  1. Everything went well until the end when the VNC viewer errored with “The connection was refused by the computer”, I tried KRDC in VNC mode and the same thing happened, ufw Status:inactive, any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.