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?
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?
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.
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
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
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