- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
The command line is a great way to manage a remote Linux computer if you don't mind typing in commands, but sometimes you need to be able to view a GUI. Short of a very long cable one of the best ways of doing this is using the VNC protocol (Virtual Network Computing).
The following are instructions on how to configure this on the Raspberry Pi (Debian based distribution). It should be similar for any other Debian based distribution (eg. Ubuntu). Some other distributions use different software install tools and possibly different init setup tools.
Note: This guide has been updated for the systemd startup system which is included in Debian / Rasbian Jesse. This will only work with the Raspbian / NOOBs images dated September 2015 and later. For the older version see: Guide for installing TightVNC server for automatic startup on older SysV based Linux distributions.
If this is used in combination with the guide to running the Raspberry Pi headless with Debian Linux then it can be used to setup a Raspberry Pi for remote access without ever needing to connect it to a monitor.
There are several software projects that support VNC. I have chosen to use TightVNC because it is free open source software (GPL2), provides compression to allow it to work over a slow network (Internet) and is readily available in the software repositories.
VNC follows the traditional client sever model. The server software runs on the host to be accessed, and 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.
First refresh the software repository information using
sudo apt-get update
This is particularly important if you have just installed the operating system as the repository information may be incomplete.
Install the server software from the repositories:
sudo apt-get install tightvncserver
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.
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
says that it's virtual desktop 1. You would connect to this using :1 at the end of the IP address in the client, this is the one we will setup to start automatically 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.
To have Tightvnc startup automatically we need to create a new startup file, which needs to be stored in the /etc/systemd/system/ directory and end with the suffix ".service". I called this tightvncserver.service. This needs to be created as the root user so using sudo
sudo nano /etc/systemd/system/tightvncserver.service
Description=TightVNC remote desktop server
You may need to change the user name on line 8. It is currently set to pi which is the default username on the Raspberry Pi Raspbian image. Set this to whatever username you want to login to TightVNC as. Do not put root as the user.
Change the file so it is owned by root
sudo chown root:root /etc/systemd/system/tightvncserver.service
Make the file executable by running
sudo chmod 755 /etc/systemd/system/tightvncserver.service
It's a good idea to test the script at this point rather than waiting until after a reboot. This is done using:
sudo systemctl start tightvncserver.service
Don't worry if you get the following error message, which is because you've already started tightvnc
A VNC server is already running as :1
Enable startup at boot using
sudo systemctl enable tightvncserver.service
TightVNC will now start at startup. I'd recommend you reboot at this point to make sure it's working but youc an just stop and start the service using the systemctl command instead.
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.
On a Linux computer you can install the viewer with
sudo apt-get install xtightvncviewer
or if that is not in the repositories try:
sudo apt-get install ssvnc
This will work with Debian / Ubuntu based distributions only, for other distributions use the normal package manager.
Note that ssvnc - which is the version I installed on my Kubuntu computer is based on tightvnc, but also has the ability to configure the ssh tunnelling (see later) through the GUI.
(replace with your own IP address as appropriate)
To access from Windows or using the Java Client download from TightVNC.com
TightVNC allows anyone that knows the password to login as the user that it's running under. It also does this without encryption so anyone can snoop in on the session. Fortunately there is a secure solution, which is to tunnel the connection over an ssh (secure shell) login.
First step is to restrict Tightvnc to the local computer only. This is done by adding the option -localhost when starting the tightvncserver. To do this edit line 7 in the /etc/systemd/system/tightvncserver.service file to read:>
ExecStart=/usr/bin/tightvncserver -localhost :1
Then restart the server.
To login using ssh - first login to the host computer using ssh
ssh firstname.lastname@example.org -L 5901:localhost:5901
The -L option creates the tunnel, from the current computer (default) port 5901 to the host computer (as localhost) port 5901. Port 5901 is the default port for display 1, 5902 for display 2 etc.
To connect using tightvnc now use the command
Note that your previous terminal will now be logged into the Raspberry Pi so you will need to start a new terminal or tab to run the viewer.
If using Windows then the ssh tunnel can be created from within PuTTY
Then connect using the vnc client using 127.0.0.1:1.
You can now use the GUI to run applications on the Raspberry Pi without needing a monitor or keyboard. If you have port forwarding set on your router you can do this over the Internet.
The instructions above are sufficient for the Raspberry Pi running Raspbian (Debian). On other systems you may need to make a further configuration change to start your normal window manager / application launcher. If you get a blank screen when connecting edit the file ~/.vnc/xstartup and replace the line /etc/X11/Xsession with one of the following as appropriate.
exec ck-launch-session startlxde #for an lxde session exec ck-launch-session gnome-session #for a gnome-session exec ck-launch-session openbox-session #for an openbox desktop exec ck-launch-session startxfce4 #for an xfce desktop exec ck-launch-session icewm #for an icewm desktop exec ck-launch-session startkde #for a kde desktop
For instance with Ubuntu you would use the entry for gnome, for xubuntu you would use xfce and for kubuntu use kde. The Raspberry Pi uses lxde, but this is started automatically on the Raspberry Pi there is no need to add it to the xstartup file.