Wednesday 14 October 2015

Setting Up a Static IP Address for the Raspberry Pi

Boot into Raspian and log in (Username. pi, Password. raspberry), this will all be command line stuff, so no need to log in to the GUI if X isn’t started automatically.
Have a pen and paper at the ready! . . . 
First, we need to list the network interface we currently have available:
cat /etc/network/interfaces
Static IP 01
The line . . . . 
iface eth0 inet dhcp
Implies that we’re currently getting the IP address via DHCP, meaning it’s being dynamically registered by the router. This is what we need to change!

Gathering Network Information:

Fist of all we need to retrieve some information from our router and the Raspberry Pi. First we need to run the following command to retrieve the first part of the information.
ifconfig
StaticIP-02
This reveals your router information, the bit you want is after eth0 (the ethernet connection). . . .
eth0      Link encap:Ethernet  HWaddr b8:27:eb:cc:03:0e
               inet addr:10.0.0.111  Bcast:10.0.0.255  Mask:255.255.255.0
Write down the following information. . .
  • inet addr – 10.0.0.111 (Pi’s Current IP Address)
  • Bcast –  10.0.0.255 (The Broadcast IP Range)
  • Mask –  255.255.255.0 (Subnet Mask Address)
We need a little more information before we proceed, so run the following command.
netstat -nr
Static IP 03
We need:
  • ‘Gateway’ Address – 10.0.0.138
  • ‘Destination’ Address – 10.0.0.0

Editing Network Configuration:

We now need to plug this information into the Raspberry Pi’s network configuration file using a text editor. For this use the nano text editor as follows. . .
sudo nano /etc/network/interfaces
Static IP 04
Simply change the line that reads:
iface eth0 inet dhcp
to
iface eth0 inet static
Then directly below this line enter the following (Please Note. You will need your own addresses we gathered in Part B, more details below). . . . 
address 10.0.0.11
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
10.0.0.138
To clarify what each part means. . . . 
address – The address you want to give your Pi, this can be any IP in the network range, but it’s usually advisable to go higher rather than lower, or you could end up logging different devices to the same IP! I’ve selected 10.0.0.11, as address (denoted by ‘inet addr‘), but this can be any IP address from the range10.0.0.1 to 10.0.0.255.
netmask – The ‘Mask‘ address we wrote down earlier.
network – The router IP address, this is the ‘Destination‘ Address was found earlier. You can also grab this off your router, it will say on the side somewhere.
broadcast – The ‘Bcast‘ address we wrote down earlier. 
gateway – This is the ‘Gateway‘ address we found earlier.
Static IP 05
So, it should look something like the above, but with your values!
Note that in the above example I also set the Wi-Fi IP to the same as the Ethernet for simplicity as this Raspberry Pi runs my BrickPi Interface Board normally.
Remember to save before exit, CTRL+X (exit) then yes to save changes!

Check the New Static IP Configuration:

We will now need to reboot the Raspberry Pi for the changes to take effect. 
sudo reboot
Log back into the Raspberry Pi and run 
ifconfig
Your new Network Setting be should revealed.
Static IP 06
To double checks all is working as it should, ping your ‘Gateway‘ Address. . . 
ping 10.0.0.11 -c 10
(the -c 10 command simply denotes that you want to ping it 10 times, if you forget to add this, it will ping the address continuously. To stop it press CTRL+C)
Static IP 07

No comments:

Post a Comment