The Raspberry Pi is automatically set to obtain an IP address from your wired or wireless network. Why does the Raspberry Pi need an IP address? This address is needed so that any traffic destined for your Raspberry Pi will be able to find it on the network.
This is method of obtaining an IP address is called DHCP or Dynamic Host Configuration Protocol. It is sometimes referred to as a dynamic IP. Your router will normally distribute these IP addresses but it isn’t guaranteed that it will get the same IP address every time. This can cause problems if you are trying to connect to your Raspberry Pi remotely.
So how do you set a static IP address?
Login and enter the following command
The line below is referring to the eth0 interface and that is should use DHCP.
iface eth0 inet dhcp
To change this line to a static IP address, enter the following command to retrieve your existing IP address and network information.
Make a note of your current IP address, the broadcast IP and the Mask. This is represented by the
inet addr: 192.168.0.6 [CHANGE]
Bcast: 192.168.0.255 [CHANGE]
One last thing that is required is the gateway. This is in most cases your routers internal IP address.
Enter the following
sudo route -nee
Make a note of Gateway address.
Next you need to edit the interfaces file to set this static information. Enter the following command.
sudo nano /etc/network/interfaces
Remove the line that reads
iface eth0 inet dhcp
Add the following
iface eth0 inet static
Save the file by pressing CTRL-X and select Y to save the changes.
When the raspberry Pi reads this file when booting up, it will look at the interface eth0 and set it to a static IP address. I will set the IP address to 192.168.0.6, the network to 192.168.0.0, the broadcast address and the gateway address so that it can find its way out of the network.
Reboot your Raspberry Pi with the following command
Login back in and enter the following.
ping 192.168.0.1 -c5
This will ping your gateway address or router and will return :
64 bytes from 192.168.1.254: icmp_req=1 ttl=255 time=2.18
64 bytes from 192.168.1.254: icmp_req=2 ttl=255 time=2.43
64 bytes from 192.168.1.254: icmp_req=3 ttl=255 time=3.24
64 bytes from 192.168.1.254: icmp_req=4 ttl=255 time=2.20
64 bytes from 192.168.1.254: icmp_req=5 ttl=255 time=3.37
One last thing that needs to be modified is the /etc/resolv.conf file. This file contains information of DNS name resolvers that allow your raspberry pi to resolve names to IP addresses. For example, if you ping www.suntimebox.com the raspberry pi will have to determine the IP address of www.suntimebox.com.
Enter the following command to edit the resolv.conf file.
sudo nano /etc/resolv.conf
Enter the follow Google public dns server IP address.
name server 18.104.22.168
Press CTRL-X to exit but remember to save the file by accepting the changes.
If you prefer, you can always make a reservation on your router or server. For this you will have to make a note of the hardware address when you enter the
command. The hardware address is represent as the HWaddr: 08:00:22:ef:65:bc. Note that your hardware address will be different.
Please let me know if I have missed anything or you want to add something that will benefit someone or a comment saying thank you would be nice. It makes it all worthwhile knowing that I helped someone and if I didn’t, let me know where I went wrong.
The information on this site has been created by volunteers in their own time because they have a passion to learn and to teach others. If you find this information useful please consider contributing something so that they know their efforts are appreciated. Why not buy us a coffee?
Next Raspberry Pi Week 4 – Overview >>