Setting up VNC server on Raspberry Pi to autostart on reboot


First note down the IP address of your Pi

hostname -I

Make sure your packages are updated to latest version

sudo apt-get update
sudo apt-get install tightvncserver

Create a vnc server service description file

sudo vi /etc/init.d/tightvncserver

and the following code:

#!/bin/sh
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: vnc server
# Description:
#
### END INIT INFO
# /etc/init.d/tightvncserver
# Set the VNCUSER variable to the name of the user to start tightvncserver under
VNCUSER='pi'
case "$1" in
start)
#Change the display number below. The connection port will be 5900 + display #
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

If lines 2-11 are not added above you get the following error:

$ sudo update-rc.d tightvncserver defaults
insserv: warning: script 'tightvncserver' missing LSB tags and overrides

Modify the permissions on the file:

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

Setup a password for vnc:

$ vncpasswd
Using password file /home/pi/.vnc/passwd
Password: 
Verify:   
Would you like to enter a view-only password (y/n)? n

Test that your server works without any errors:

pi@myrpi:~ $ sudo /etc/init.d/tightvncserver start

New 'X' desktop is myrpi:1

Creating default startup script /home/pi/.vnc/xstartup
Starting applications specified in /home/pi/.vnc/xstartup
Log file is /home/pi/.vnc/myrpi:1.log

Now reboot your system and check if the vncserver starts automatically.

pi@myrpi:~ $ sudo reboot

I sourced most of the content from the following link but some of the steps there didn’t work out of the box so I modified them and wrote a new post for my knowledge.

Reference: http://raspberrypi.stackexchange.com/questions/27676/auto-start-tightvncserver-on-raspberry-pi-2

Advertisements