Setup VNC Server for Remote Access (Headless)

Most of this documentation was taken from here.

## Installing TightVNC

We'll install TightVNC as our VNC software. If you haven't already, it's a good idea to update your package management tool by running

```
sudo apt-get update

```

This updates a huge list of software that can be installed or updated using the Advanced Package Tool (APT). We can now install TightVNC by issuing the command

```
sudo apt-get install tightvncserver

```

Just type "y" (no quotes) when it asks if you want to continue.

## Testing the Installation and Running the VNC Server

You can run TightVNC for the first time by sending the command

```
tightvncserver -geometry 1024x768 -depth 24

```

where the "geometry" and "depth" tags set the display format of the server window when viewed on another machine. If successful, the shell returns something like

```
New 'X' desktop is raspberrypi:1

```

It's important to note the "1" here is the screen number, and the corresponding port is 5901. You can chose the screen number 2 by modifying the command to

```
tightvncserver :2 -geometry 1024x768 -depth 24

```

In this case, the port number is 5902. The port number and IP address (or host name) are required when using the TightVNC Viewer.

## Start TightVNC Server on Boot

I found a lot of wrong ways to do this, but this worked for me. Create a new file in the init.d directory:

```
sudo nano /etc/init.d/tightvncserver

```

Enter the following:

```
# Description: Start Tight VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/tightvncserver

USER=pi
HOME=/home

export USER HOME

case "$1" in
 start)
 echo "Starting Tight VNC Server"
 #Insert your favoured settings for a VNC session
 /usr/bin/tightvncserver :1 -geometry 1024x768 -depth 24
 ;;

stop)
 echo "Stopping Tight VNC Server"
 /usr/bin/tightvncserver -kill :1
 ;;

*)
 echo "Usage: /etc/init.d/tightvncserver {start|stop}"
 exit 1
 ;;
esac

exit 0

```

Give the startup script executable permission:

```
sudo chmod 755 /etc/init.d/tightvncserver

```

We can now start or stop the service manually. Make sure you do this at least once and add the password as before if asked:

```
sudo /etc/init.d/tightvncserver start
sudo /etc/init.d/tightvncserver stop

```

In order to add the script to the startup routine, finally send the following command:

```
sudo update-rc.d tightvncserver defaults

```

When the Pi reboots, if as long as the wireless settings are correct, you should be good to go headless!

## Opening VNC View on Other Computer

You also have to download the TightVNC software on the viewer computer. I use Windows, and you can download the software here. You should have a start menu folder for TightVNC with a TightVNC Viewer program. You can use either the host name (which you set up after running raspi-config) or the IP address of the Pi (found by running ifconfig). Follow the IP/hostname with two colons and then the port number (5901 if you ran "tightvncserver :1 ..." or 5902 if you ran "tightvncserver :2 ...", etc.

BEWARE: When using host name (which is REALLY handy when using DNS), the DNS cache may need to be flushed if the host name does not resolve to the correct IP address. In Windows, at the command line, run

```
ipconfig /flushdns

```

NOTE: If you get an error saying something like "No connection could be made because the target machine actively refused it", your firewall may be blocking the port. To fix this, you need to add an "Inbound" exception to allow the incoming TCP port (5901). See here for more instructions.