Setting Up A Multiple-Raspberry Pi Web Server: Part 3 (Static IP Addresses)
This is a continuation of Setting up a multiple-raspberry pi web server: Part 2 (Initial Setup).
By the end of this blog post, all of your raspberry pis will have static IP addresses on your local network, and you'll know how to log into them remotely.
Whether you're coming from Part 2, or jumping straight to this one, hopefully you have one or more working raspberry pis.
Step 3.0: What's the point of a static IP address?
Assigning static IP addresses to your raspberry pis means that you'll always know exactly "where" to find them on your network. This makes it easy to get multiple raspberry pis to "work together", as we'll see in later parts.
Quick note: it is *not* necessary to set up a static IP address in order to connect remotely to a raspberry pi. However, static IP addresses ensure that you will always be able to "find" your raspberry pi at the same IP address every time.
Step 3.1: Make sure you have the right linux distribution on your raspberry pi
If you arrived on this page after completing Part 2, then your raspberry pi should have the Raspbian Jessie operating system installed.
There are multiple other pages online that can help you set up a static IP address for other operating systems, or for older versions of Raspbian Jessie. As far as I know, however, this is the only tutorial to do this for the latest version of Raspbian Jessie.
To make sure you have the right version, type cat /etc/network/interfaces
in the terminal in your raspberry pi, and you should see something like the following output:
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet manual
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Note the line that says iface etho0 inet manual
(if that line isn't there, try this tutorial for the older versions of Raspbian),
Also note that it tells you:
For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
Step 3.2 Get some data about your network
Before we set up the static IP address for your raspberry pi, we just need to figure out what to change:
In a terminal, type
netstat -nr
- (Note: I borrowed this image from the excellent tutorial on how to set up static IP addresses for an older version of Raspbian
Then, write down the "Gateway" address, which is
192.168.1.254
in the example above.
Step 3.3: Update your raspberry pi(s) to have static IP addresses
Next, using your favorite terminal text editor (mine is vim*), open up /etc/dhcpcd.conf
, and add the following lines at the bottom:
interface eth0
static ip_address=192.168.1.200
static routers=192.168.1.254
static domain_name_servers=192.168.1.254
- The first line is the static IP address you want your raspberry pi to use. I'd recommend using
192.168.1.200
for the first pi,192.168.1.201
for the second, etc. - For the other two lines, just use the gateway address you wrote down earlier (all your pis will have the same info for the last 2 lines)
* Note that vim does not come pre-installed on raspberry pis (though vi
does), so you may have to use sudo apt-get install vim
.
Step 3.4: Restart your pi(s)
The easiest way to restart your raspberry pi is to unplug it, then re-plug it in. Another option is to type sudo reboot
in your raspberry pi terminal.
If all goes well, your raspberry pi should now be available at the ip address 192.168.1.200
.
Step 3.5: Connect to your Raspberry Pi(s) remotely
Test whether the earlier steps worked by attempting to connect remotely to your raspberry pi (either from another raspberry pi, or from a laptop on your network).
To do this, type: ssh pi@192.168.1.200
into your terminal.
If the earlier setup was successful, you should be prompted for a password, at which point you should type raspberry
. (The default username for all raspberry pis is pi
and the default password is raspberry
.)
(If you'd rather not have to type in a password each time, you can also follow the instructions here to set up an SSH key)
Assuming that also goes well, you should end up seeing something like the following:
~$ ssh pi@192.168.1.200
pi@192.168.1.200's password:
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jan 31 03:44:09 2016
pi@raspberrypi:~ $
Boom! You've now connected to your raspberry pi remotely from a different computer, and you connected to it via your newly-configured static IP address.
You can now repeat this process for all of your raspberry pis (don't forget to give each one a different IP address so they don't conflict)
Now that you can easily "find" your raspberry pi on your network (and you can be confident it will always be the same ip address because it is a "static" ip address), we're nearly ready to set up our multi-raspberry pi web server!
Coming up soon:
- Part 4: How to set up one or more raspberry pis to respond to web requests from another computer on your local network
- Part 5: How to use HAProxy to load-balance incoming requests to multiple raspberry pis.