Install VNC on Ubuntu 16.04

Estimated reading time: 3 min

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

  1. Login to your server
    Login as an administrative non-root user
  2. Update all packages
    sudo apt-get update && sudo apt-get -y dist-upgrade
  3. Install 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:
    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 webbrowser. If you prefer a minimal install use the following command:
    sudo apt-get install --no-install-recommends ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal tightvncserver
  4. Configure VNC server
    We start with setting a password for the VNC server. Therefore we need to start the VNC server with:
    vncserver
    You will be asked to enter and verify a password. We would advise to use 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:
    vncserver -kill :1
    Now it's time to move the old startup configuration to a safe place and create an empty startup configuration:
    mv ~/.vnc/xstartup ~/.vnc/xstartup.old; touch ~/.vnc/xstartup
    Next open the startup configuration file with your favourite text-editor:
    nano ~/.vnc/xstartup
    Copy and paste the following so that the contents of that file match with the following:
    #!/bin/sh
    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    xsetroot -solid grey
    x-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
    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:
    nano ~/.vnc/tightvncserver.conf
    Copy and paste the following so that the contents of that file match with the following:
    $geometry = "1280x720";
    $depth = 24;

    Replace 1280x720 with the resolution you would like to have like 1920x1080 for instance.
  5. 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:
    sudo nano /etc/systemd/system/vncserver@1.service
    The content should match the following. Remember to edit your username:
    [Unit]
    Description=Remote desktop service (VNC)
    After=syslog.target network.target

    [Service]
    Type=simple
    User=johnny
    PAMName=login
    PIDFile=/home/johnny/.vnc/%H:%i.pid
    ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill :%i > /dev/null 2>&1 || :'
    ExecStart=/usr/bin/vncserver :%i
    ExecStop=/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:
    sudo systemctl daemon-reload
    Next we start the VNC server:
    sudo systemctl start vncserver@1
    Next we make sure the VNC server is started when the server is started:
    sudo systemctl enable vncserver@1

  6. 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
    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
    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
    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
    VNC-tunnel-with-PuTTY-04

    Don't close PuTTY, you can minimise 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-01
    VNC-connect-with-SSH-tunnel-02
    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
    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.

  7. 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 johnny@x.x.x.x
    PuTTY Configuration
    PuTTY Configuration

    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":

    PuTTY Configuration
    PuTTY Configuration

    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":

    PuTTY Configuration
    PuTTY Configuration

    You are connected:

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

Was this article helpful?
Dislike 1
Views: 4245

Reader Interactions

Leave a Reply

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