Set up remote access and a static IP address on your Raspberry Pi

Sometimes it is not practical or possible to takeover the TV to use your Raspberry Pi. However, all is not lost. As long as the following is true there is a way round it:

  • Your Raspberry Pi is connected through your router in house in order to access the Internet – this may be either via a wired link using a network LAN cable or through a wireless link using a USB wireless adapter.
  • You have a laptop or PC which is also connected through your home router again through a wired network cable or through a wireless link.

If the above is true you can set up your Raspberry Pi so that you can remotely access it from your laptop or PC. In order to achieve this you need to do two main things:

  1. Give your Raspberry Pi a static IP address
  2. Set up something called VNC Server on your Raspberry Pi
  3. Access your Raspberry Pi with VNC Viewer from your laptop or PC

Give your Raspberry Pi a static IP address

In order to connect to your Raspberry Pi for remote access it is much easier if you have what is called a static IP address. This means that every time the Raspberry Pi boots up it will have the same IP address.

This is important as if the IP address is not static, when you come to try and get remote access to it you will need to try and work out what IP address it has been given (although  we do cover a number of ways in which this can be done in another post – click here.)

You may be using a wired connection – using a network cable plugged into the slot on the Raspberry Pi – or a wireless connection with a USB wifi adapter plugged in. The following information provides a file that will work for both.

If you want information on how to set up your wireless connection refer to this post.

In order to set the IP address open a terminal and type the following command:

sudo nano /etc/network/interfaces

This opens a text editor and the file that the Raspberry Pi looks at to get information on what type of IP address it should gain. This file will currently have some information in it but you need to change it to the following:

auto lo

iface lo inet loopback

#wired connection
#iface eth0 inet dhcp
iface eth0 inet static

#wireless connection
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface wlan0 inet dhcp
iface wlan0 inet static
#iface default inet dhcp

Click CTR + X and then choose Y to save and close the file. You then need to reboot the Raspberry Pi for the changes to take affect.

sudo reboot

Note that wherever the ‘#’ character is will cause the Raspberry Pi to ignore the rest of the line. You can see that the two parts that have been added are the parts that have the word ‘static’ in and then the address, netmask and gateway lines. This is the information that you specify.

You need to choose an IP address that is free and available. It will be your router that hands out IP addresses so you need to check that you are not choosing an address that it may hand out. Routers such as the Linksys will only hand out in the range – 199 so I have chosed IP addresses and above.

The netmask will generally be the same but the gateway may need to be changed – it is the IP address of your router.

Also note that the IP address of the wired and wireless connection is different, if it is the same and you plug in both a wired and wireless connection there will be a conflict.

Setup VNC

First you will have to be connected to the Internet as you will need to install VNC. You can do this by opening a terminal and typing:

sudo apt-get install tightvncserver

Choose ‘Y’ when prompted and press enter and wait for it to finish installing. This may take a few minutes.

You now need to create a script that will start the VNC server:

sudo nano /etc/init.d/tightvncserver

Paste in the following text:

# /etc/init.d/tightvncserver
case "$1" in
        su $VNCUSER -c '/usr/bin/tightvncserver :1'
        echo "Starting TightVNC Server for $VNCUSER "
        pkill Xtightvnc
        echo "TightVNC Server stopped"
        echo "Usage: /etc/init.d/tightvncserver {start|stop}"
        exit 1
exit 0

Click CTR and X and press ‘Y’ followed by enter.

You now need to make it so the script can be run. You can do this by typing the following:

sudo chmod 755 /etc/init.d/tightvncserver

To set it up you need to run it – this helps you setup the password:

sudo /etc/init.d/tightvncserver start

This will ask you to set a password so that not everyone can access your device remotely, only the ones who know the password.

Choose ‘N’ for setting a view only password.

The last thing you have to do is make it start on startup. To do this type the following command and press enter:

sudo update-rc.d tightvncserver defaults

Don’t worry about some of the warnings that will show. Once you have rebooted your Raspberry Pi you should be able to connect.

Connecting remotely to your Raspberry Pi with VNC viewer

You will need to download and install VNC Viewer on a laptop. You can get VNC Viewer from this link

Once installed you should be able to run the program and type in the IP address of your Raspberry Pi followed by a ‘:1’. This is the port you will connect on:

Connect prompt with VNC viewer

The prompt that pops up when you run VNC viewer

Click connect and you will be asked to enter the password when you ran VNC for the first time.

Password prompt for VNC viewer

Tight VNC viewer password prompt.

Enter your password and press OK. You should then see the desktop of the Raspberry Pi and you are now able to use the Raspberry Pi as if you had a monitor and keyboard connected through your laptop or tablet:

Connected using Tight VNC Viewer to the Raspberry Pi

The desktop that appears on your laptop or tablet you can now interact with it.


About Barnaby Kent
This entry was posted in Under the bonnet and tagged , , , . Bookmark the permalink.

2 Responses to Set up remote access and a static IP address on your Raspberry Pi

  1. Pingback: Set up your Raspberry Pi | Pi-Cars

  2. Pingback: Setting up a wireless connection on the Raspberry Pi | Pi-Cars

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s