Raspberry Pi: GUI from remote host

This article presents two approaches; a simple one and more advanced, both of which are relevant depending on your requirements. To wit:

The simple approach, using x11vnc on the Pi, connects to one session only. I use this when I can connect a wireless-enabled Pi to a keyboard and mouse but don't have an HDMI screen to attach. (Another device is then used to dislplay the desktop e.g. an Android phone or computer). From this perspective, the current session on the Pi is the only in town, and I want to connect to it - it wouldn't help getting a new session on the client device if I'm typing in a different session.

The more advanced approach uses tighvnc, and allows typing and mousing around on the client in a new session.

Single Session Setup

Install the X11VNC server (or through Ubuntu Software Center -> X11VNC Server)

sudo apt-get x11vnc

Create a VNC password file.

sudo x11vnc -storepasswd yourVNCpasswordHERE /etc/x11vnc.pass

Create configuration file...

sudo vi /etc/init/x11vnc.conf

...adding this:

start on login-session-start
/usr/bin/x11vnc -xkb -forever -auth /var/run/lightdm/root/:0 -display :0 -rfbauth /etc/x11vnc.pass -rfbport 5900 -bg -o /var/log/x11vnc.log

Restart Ubuntu.

Multiple Session Setup

Install TightVNC Server

sudo apt-get update
sudo apt-get install tightvncserver

Start up vnc server:

/usr/bin/tightvncserver
Virtual desktop number is displayed on server startup, e.g:

New 'X' desktop is raspberrypi:1
Your client may want the desktop specified as '1' or 'port 5901' or similar. Anyhow, :1 and port:5901 mean the same thing

Adding Tightvnc to the startup

To create an init file so that tightvnc starts up on boot by adding these lines (source: penguinator.com):
#!/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
# For more details see:
# http://www.penguintutor.com/linux/tightvnc
### Customize this entry
# Set the USER variable to the name of the user to start tightvncserver under
export USER='pi' # put your user name here!
### 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

 

 

You may need to change the variable on line 16. It is currently set to pi which is the default username on the Raspberry Pi Debian 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 (not strictly neccessary, but is the standard ownership for init files
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 at startup. 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.

Installing Tightvnc viewer (VNC client) on a Linux computer

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.

Login using
xtightvncviewer 192.168.41.1:1

Securing TightVNC

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 23 in the /etc/init.d/tightvncserver file to read:

    su $USER -c '/usr/bin/tightvncserver -localhost :1'

Then restart the server.

To login using ssh - first login to the host computer using ssh
ssh pi@192.168.1.6 -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
xtightvncviewer 127.0.0.1:1
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.

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.

Starting your window manager

The instructions above are sufficient for the Raspberry Pi running Debian. You may however 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.