11

I have a Raspberry Pi 2 and have been following the post on Raspberrypi.org but Tight VNC Server does not start on reboot. There doesn't seem to be any error.

How do I get TightVncServer to start on Pi Reboot?

sarin
  • 393
  • 2
  • 6
  • 15
  • Can't post an answer yet, but there's a simple method that I got working on my pi 3 B documented at adafruit here: https://learn.adafruit.com/adafruit-raspberry-pi-lesson-7-remote-control-with-vnc/running-vncserver-at-startup – Steven Evers Mar 01 '18 at 06:33

2 Answers2

10

To do this you can use a little bit of Linux cleverness.

Our first task will be to edit the file /etc/rc.local. This file can contain commands that get run on start-up. If we look at the file we can see that there is already few entries in there;

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

exit 0

The first set of lines with a hash mark (#) in front of them are comments. These are just there to explain what is going on to someone reading the file.

The lines of code towards the bottom clearly have something to do with the IP address of the computer. In fact they are a short script that checks to see if the Raspberry Pi has an IP address and if it does, it prints it out. If you recall you can see the IP address printed out on the screen when the Pi boots up like so

My IP address is 10.1.1.8

Raspbian GNU/Linux 7 raspberrypi tty1

raspberrypi login:

This piece of script in rc.local is the code responsible for printing out the IP address!

We will add the following command into rc.local;

su - pi -c '/usr/bin/tightvncserver :1'

This command switches user to be the ‘pi’ user with su - pi. The su stands for ‘switch user’ the dash (-) makes sure that the user pi’s environment (like all their settings) are used correctly and pi is the user.

The -c option declares that the next piece of the line is going to be the command that will be run and the part inside the quote marks ('/usr/bin/tightvncserver :1') is the command.

The command in this case executes the file tightvncserver which is in the /usr/bin directory and it specifies that we should start desktop session 1 (:1).

To do this we will edit the rc.local file with the following command;

sudo nano /etc/rc.local

Add in our lines so that the file looks like the following;

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Start tightvncserver
su - pi -c '/usr/bin/tightvncserver :1'

exit 0

(We can also add our own comment into the file to let future readers know what’s going on)

That should be it. You should now be able to test that the service starts when the Pi boots by rebooting.

If the above sounds a little long winded, feel free to check out a more complete reasoning here.

Dmitry Grigoryev
  • 26,688
  • 4
  • 44
  • 133
d3noob
  • 1,826
  • 1
  • 13
  • 14
  • It does not work for me. – Chameleon Aug 19 '17 at 18:04
  • The answer was written for the 'Wheezy' version of Raspbian. The later version ('Jessie' and (I presume) 'Stretch') have a different default method. See [here](https://leanpub.com/jerpi/read#remote) for an alternative approach. – d3noob Aug 19 '17 at 20:33
5

Before starting ensure your Pi is connected to the internet\network via Ethernet\wifi.

Open the Terminal and enter the following commands:

Get Pi IP Address for remote connection (for use later)

hostname -I

Install TightVncServer

sudo apt-get update
sudo apt-get install tightvncserver
tightvncserver

The first time this is run you must enter a password and verify it. No need to enter a view only password.

To configure for auto start as a service when the Pi boots up:

Open nano (text editor) to create a file to auto start Tight VNC Server sudo nano /etc/init.d/tightvncserver

Type in the following (or copy and paste):

#!/bin/sh
# /etc/init.d/tightvncserver
# Set the VNCUSER variable to the name of the user to start tightvncserver under
VNCUSER='pi'
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

Press Ctrl+x, then y to save and Enter to keep the same file name.

Edit the permissions of this file to make it executable and active:

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

Reboot to test sudo reboot

Install the vnc client for your OS and try to connect once the Pi has rebooted!

For Windows: TightVNC Client for example . No need to install server.

Start The VNC Client Connect using the IP address from the top of this post. Change the IP address for yours. The port number in a number of internet posts is only listed as 2 digits. This is short hand and if you are using the above script your pi is running on port 1 then you should use 5901. If 2, then 5902 etc.

192.168.1.123:5901
sarin
  • 393
  • 2
  • 6
  • 15
  • Your solution is working perfectly for me except I am not getting full screen. How can I get full screen? (`vncserver -geometry 1366x768 -depth 24 -dpi 96`. I used that command before to get full screen. Is there any way to add the line in your code?) – opu 웃 Nov 17 '16 at 09:54
  • @opu웃 The line `su $VNCUSER -c '/usr/bin/tightvncserver :1'` is what runs the command you're referring to. Simply append the options to the end of that line, which will result in something like `su $VNCUSER -c '/usr/bin/tightvncserver :1' -geometry 1366x768 -depth 24 -dpi 96` – Trent Jun 23 '19 at 21:01
  • How do I debug if this does not work? If I run tightvncserver at the pi user command line, it starts up and I can remote in, but is not auto-starting using this or the /etc/rc.local solution. – Alan McDonley Sep 05 '19 at 16:26