Raspberry Pi: Setting Up VNC On Raspbian Pt1

    4 minutes

The Raspberry Pi is a great affordable Pc with a lot of power for its size. This is what makes it great for so many projects in so many settings. However, it’s not always easy to find accurate information in full. At Code Clinic we’ve spent many an hour trying to set-up a working VNC server to allow us to run the Pi headerless (eg without a keyboard and mouse attached). We’ve seen and tried so many tutorials, and scoured seemingly endless forums trying to get VNC to work but without success.

So now we’ve got a nice reliable set-up running we thought we’d share our knowledge.

Choosing the right VNC Server

There are several software projects that support VNC. But the best one for your needs is highly dependent on what you need to do via the VNC and ultimately with your Pi. If you need to work specifically with the same desktop as that being shown on a monitor attached to the Pi, then you’re probably going to struggle to find accurate info on how to do it. Luckily, we’ve created a guide for you.

If you simply need a connection into the Pi to do general task and run completely headerless we recommend you to use TightVNC because it is free open source software, provides compression to allow it to work over a slow network (Internet) and is readily available in the software repositories. Plus it seems to be the most reliable of the lot. To install TightVNC continue reading.

Client server model

VNC follows the traditional client sever model. The server software runs on the Pi to be accessed, and the client software runs on the local machine that wishes to control the host. The server and client do not have to be the same (eg. the client can be RealVNC when the server is TightVNC), but some features may only work when the same client and server are used.

Install the TightVNC server software

First refresh the software repository information using:

sudo apt-get update && apt-get upgrade

This will update your repository data and upgrade any software already installed. This is particularly important if you have just installed the operating system as the repository information may be incomplete.

Next we need to install the TightVNC from the repositories:

sudo apt-get install tightvncserver

Start the VNC server and configure a password

The first time you run the server it will prompt you to set a password. This is the password that you use when connecting remotely.

/usr/bin/tightvncserver

There is no need to create a view only password, unless you have a specific need. When the server starts it will tell you which virtual desktop has been setup. In this case:

  • New ‘X’ desktop is raspberrypi:1

The appended ‘indicates that it’s virtual desktop 1. You would connect to this using :1 at the end of the IP address in the client, or the equivalent port number of 5901. We’ll explain this in more detail later. You can run multiple instances. Each time you start tightvncserver it will use the next available desktop, but in most cases you will just need one.

Adding Tightvnc to the startup

In most cases you’ll likely want the VNC Server to start up automatically when the Pi boots up. Whilst this should be fairly straight forward, this can prove to be the main stumbling block for most people trying to get this set-up. There are numerous tutorials around with each giving a slightly different way to do it. But in each case the info given is either wrong or can doesn’t work for everyone.

In most causes the first thing to check is that you are using the correct terminal window. Since we are typically going to be logging in automatically to the default ‘Pi’ user its important to run commands via the standard Terminal and not RootTerminal. Fortunately, the following instructions work regardless of which Terminal you use.

To have Tightvnc startup automatically we need to create a new init file. Below is the code we will be adding:

    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides:          tightvncserver
    # Required-Start:    $local_fs
    # Required-Stop:     $local_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start/stop tightvncserver
    ### END INIT INFO

    # More details see:
    # https://codeclinic.de/2014/01/raspberry-pi-tutorial-setting-vnc-default-desktop

    ### Customize this entry
    # Set the USER variable to the name of the user to start tightvncserver under
    export USER='pi'
    ### End customization required

    eval cd ~$USER
    case "$1" in
      start)
        su $USER -c '/usr/bin/tightvncserver :1'
        echo "Starting TightVNC server for $USER "
        ;;

      stop)
        pkill Xtightvnc
        echo "Tightvncserver stopped"
        ;;
      *)
        echo "Usage: /etc/init.d/tightvncserver {start|stop}"
        exit 1
        ;;
    esac
    exit 0

To download this code in full use the following commands to get the code via Penguin Tutors Repository:

wget http://www.penguintutor.com/otherfiles/tightvncserver-init.txt
sudo mv tightvncserver-init.txt /etc/init.d/tightvncserver

Line 16 is currently set to the default username; ‘pi’. You should change this if necessary, but in most cases you’ll be logging in as the default user. Do not put root as the user.

Change the file so it is owned by root (not strictly neccessary, but is the standard ownership for init files. This is the one step that we’ve found missing from most of the tutorials available, and none of those worked for us. So we’d strongly recommend running this command:

sudo chown root:root /etc/init.d/tightvncserver

Make the file executable by running:

sudo chmod 755 /etc/init.d/tightvncserver

Add the script to the default runlevels with the command:

sudo update-rc.d tightvncserver defaults

TightVNC will now start when your (LXDE) desktop starts. I’d recommend you reboot at this point to make sure it’s working, but you can just stop and start if you don’t want to reboot. You can manually start and stop it using:

sudo /etc/init.d/tightvncserver start

and

sudo /etc/init.d/tightvncserver stop

Note that this is designed for running only one instance of TightVNC on the computer. If you want to be able to start and stop specific instances of TightVNC then you will need to tweak the startup script appropriately. For info on how to run VNC on Raspbian Desktop see part 2.

Phill Healey on BehancePhill Healey on EmailPhill Healey on FacebookPhill Healey on FlickrPhill Healey on InstagramPhill Healey on Linkedin
Phill Healey
[Technical Director]
Phill has been designing and developing websites since 1986 in the early days of the web. As a self taught teenager, he built one of the world’s first Internet radio stations. Since then, he's gained numerous qualifications and several awards for his work in the industry. As a result he has had the pleasure of working in agencies in Manchester & London UK, and now Bavaria, Germany.

3 comments

  1. Vielen Dank Phill,
    Du hast meinen Tag gerettet!!!
    Läuft prima auf meinem Raspberry Pi 2.
    Danke & Grüße
    Peter Friedrich

Leave a Reply

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