1. Knowledge base
  2. Initial Setup
  3. Install VNC on Ubuntu 16.04

Install VNC on Ubuntu 16.04

Updated on 18 April 2023 5 min read Snel.com support team

Introduction

With VNC you can use a graphical desktop environment remotely. This article will help you install and configure the graphical desktop environment and VNC on your server.

Prerequisites

Log in to your server

Log in as an administrative non-root user

Update all packages

Code
sudo apt-get update && sudo apt-get -y dist-upgrade

Install the graphical desktop environment and VNC server

Ubuntu is installed without a graphical desktop environment by default. There are lots of desktop environments available, we will install Gnome:

Code
sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal tightvncserver

This will install a complete desktop environment including an office suite and web browser. If you prefer a minimal install using the following command:

Code
sudo apt-get install --no-install-recommends ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal tightvncserver

Configure VNC server

We start by setting a password for the VNC server. Therefore we need to start the VNC server with:

Code
vncserver

You will be asked to enter and verify a password. We would advise using the same password as for your user. The view-only password is optional and could be used to give someone access to your screen without allowing him to move your mouse and type with the keyboard. By starting VNC server default configurations are created. We want to update our configuration, therefore we first stop the VNC server by issuing the following command:

Code
vncserver -kill :1

Now it’s time to move the old startup configuration to a safe place and create an empty startup configuration:

Code
mv ~/.vnc/xstartup ~/.vnc/xstartup.old; touch ~/.vnc/xstartup

Next, open the startup configuration file with your favorite text editor:

Code
nano ~/.vnc/xstartup

Copy and paste the following so that the contents of that file match with the following:

Code
#!/bin/sh[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresourcesxsetroot -solid greyx-window-manager &gnome-session &gnome-panel &gnome-settings-daemon &metacity &nautilus &

We will now grant it the proper privileges to this file to ensure it will work correctly

Code
chmod +x ~/.vnc/xstartup

After you’re done editing the startup configuration file save and exit (CTRL+W for nano). Next, we configure the VNC server configuration file, open the configuration file:

Code
nano ~/.vnc/tightvncserver.conf

Copy and paste the following so that the contents of that file match with the following:

Code
$geometry = "1280x720";$depth = 24;

Replace 1280×720 with the resolution you would like to have like 1920×1080 for instance.

Setup VNC server to start at boot

We start VNC server at boot by creating a service file for it and enabling the service. Start by creating a new configuration file for the service:

Code
sudo nano /etc/systemd/system/[email protected]

The content should match the following. Remember to edit your username:

Code
[Unit]Description=Remote desktop service (VNC)After=syslog.target network.target [Service]Type=simpleUser=johnnyPAMName=loginPIDFile=/home/johnny/.vnc/%H:%i.pidExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :'ExecStart=/usr/bin/vncserver :%iExecStop=/usr/bin/vncserver -kill :%i [Install]WantedBy=multi-user.target

Save and exit (CTRL+W). Next reload the service definitions by issuing the following command:

Code
sudo systemctl daemon-reload

Next, we start the VNC server:

Code
sudo systemctl start vncserver@1

Next, we make sure the VNC server is started when the server is started:

Code
sudo systemctl enable vncserver@1

Connect with Windows

First, create a secure tunnel over SSH before connecting to the VNC server. You can create a tunnel by connecting to your server with PuTTY as follows. First setup the tunnel under “Connection” -> “SSH” -> “Tunnels” by entering “5901” in “Source port” and “x.x.x.x:5901” (replace x.x.x.x with the IP address of your server) in “Destination” (1) and then clicking “Add” (2):

VNC-tunnel-with-PuTTY-01.png

Next click on “Connection” -> “Data” and fill in your username (replace johnny with your username) under “Auto-login username”:

VNC-tunnel-with-PuTTY-02

Next click on “Session” and enter the IP address of your server under “Host Name (or IP address)” (1), enter a descriptive name under “Saved Sessions” (2) and click on “Save” (3).

VNC-tunnel-with-PuTTY-03

Whenever you want to connect with VNC to your computer we recommend you to start PuTTY and double click on “x.x.x.x VNC” in the main screen with the list with saved sessions under “Default Settings”. You will be asked for your SSH password, enter your password and hit “Enter”. At this moment the PuTTY prompt on your server should be shown:

VNC-tunnel-with-PuTTY-04

Don’t close PuTTY, you can minimize the window if you like. Next, it’s time to start your VNC client. Start TightVNC and on the main screen fill in “localhost::5901” (note the 2 colons!) next to “Remote Host:” and click on “Connect”:

VNC-connect-with-SSH-tunnel-01
VNC-connect-with-SSH-tunnel-02

You will be asked for a password, enter your VNC password and click on “OK”:

You are connected:

VNC-connect-with-SSH-tunnel-03

If you want to close the connection close the VNC client and disconnect from SSH by typing exit in PuTTY.

Connect with macOS and Linux

Again we setup the SSH tunnel first. Open up your terminal and connect with: ssh -L 5901:127.0.0.1:5901 [email protected]

VNC-tunnel-with-PuTTY-01

Replace johnny with your username and x.x.x.x with the IP address of your server. After hitting “Enter” you will be asked for your password, enter your SSH password and hit “Enter”, you will be connected with SSH. Leave this running. Next, start your VNC client (we start the native Screen Sharing app from macOS) and enter “localhost:5901” as the host to connect to and click on “Connect”:

VNC-tunnel-with-PuTTY-02

You will be asked for a password, type in your VNC password, tick “Remember this password in my keychain” if you prefer and click on “Connect”:

VNC-tunnel-with-PuTTY-03

You are connected:

If you want to close the connection close the VNC client and disconnect from SSH by typing exit in your terminal.

Conclusion

Congratulations! You have successfully installed VNC on Ubuntu 16.04.

Was this article helpful?

Spot a mistake in this article? Email us at support@snel.com

This article was last checked on 18 April 2023. · 83,504 views

Your own VPS online within 2 minutes

Windows or Linux, root access and hosted in Rotterdam.

View VPS from € 6.99

Support

Can't figure it out? We are happy to help

You always talk to a real engineer. We typically respond within 15 to 30 minutes, and 24/7/365 for outages.

Call us

+31 88 3 088 099

Create a ticket

Through the client area we immediately see which service it is about.

Email us

Send your question via the contact form, also outside office hours.