Setting up a VNC server on Raspberry Pi using tightvncserver

This article covers a step by step how-to dealing with the setup of a remote desktop alternative on Raspberry Pi.

The complete setup is meant to be used as the user pi. Take care of it, if you like to set it up for another user.

Prerequisites

  • Running Raspian

How-to

Install tightvncserver:

sudo apt-get install tightvncserver

Start it right now for the first time to get asked for a VNC password:

tightvncserver

Then create an init script that allows specific command line control for the pi’s session and also enables starting up VNC service on boot time:

sudo nano /etc/init.d/vncstartup

The content looks like this (taken from here):

#!/bin/sh
# /etc/init.d/tightvncserver
# Customised by Stewart Watkiss
#http://www.penguintutor.com/linux/tightvnc
# Set the VNCUSER variable to the name of the user to start tightvncserver under
VNCUSER='pi'
eval cd ~$VNCUSER
case "$1" in
 start)
   su $VNCUSER -c '/usr/bin/tightvncserver :1'
   echo "Starting TightVNC server for $VNCUSER "
   ;;
 stop)
   pkill Xtightvnc
   echo "Tightvncserver stopped"
   ;;
 *)
   echo "Usage: /etc/init.d/tightvncserver {start|stop}"
   exit 1
   ;;
esac
exit 0

Change the scripts permissions and update config:

sudo chmod 755 /etc/init.d/vncstartup
sudo update-rc.d vncstartup defaults

Ensure your user’s config file has the correct permissions/ownership:

sudo chmod 755 xstartup
sudo chown pi:pi xstartup

The normal configuration for X should be working, if it doesn’t try

nano /home/pi/.vnc/xstartup

and replace the content with:

#!/bin/sh
xrdb $HOME/.Xresources
xsetroot -solid black
/usr/bin/lxsession -s LXDE &

Whenever you change something in this config, it is necessary to restart VNC service:

sudo /etc/init.d/vncstartup stop
sudo /etc/init.d/vncstartup start

To test, if the VNC server is running, check

sudo netstat –tulpn

for active ports 500x depending on your configuration.

To go beyond

VNC itself uses unencrypted connections. You should deny any incoming traffic on the VNC ports and use SSH with port tunneling instead.

Whenever something seems to go wrong, check the logs (commonly ~/.vnc/<hostname>:1.log).

Leave a Comment