Raspberry Pi — Static IP address quick tutorial
This blog post is part of my larger series related to different tools and manipulation that I have used and applied while tinkering with my Raspberry Pi .
This post will focus on setting up an STATIC IP ADDRESS for my Raspberry Pi. This step-up is very important if your are intending to connect to your Raspberry Pi on your local network, as well as across the internet using Port Forwarding. Port Forwarding will actually be one of my upcoming blog posts so keep an eye out … and I hope this will be helpful to you.
There are two places you can accomplish this goal:
1) if by editing the internet configuration file on your Raspberry Pi and requesting a particular IP address from your router
2) is by going into your router configuration and assigning a static IP address by going into your DHCP setup (on the router) and assigning a static Ip address to the MAC address of the Raspberry Pi
Let dive right into the first method of accomplishing this goal:
We will first go into a little hunt for information within your Raspberry Pi.
The two location that we will go to are the following:
1- ifconfig — which is a system administration utility in Unix-like operating systems for network configuration.
2- netstat -nr or route -n — (network statistics) which is a command-line network utility tool that displays network connections for the Transmission Control Protocol (TCP — Transmission Control Protocol (TCP), one of the main protocols of the Internet protocol suite)
So make sure to take notes of each piece of information once we find it and keep it for future reference.
Firstly, within your terminal use the command ifconfig
Within ifconfig we will gather three piece of information:
- your current IP address → under wlaninet address
- the broadcast range (or Bcast)
- the Subnet Mask (mask)
(See below @ within wlan0: inet address ; Bcast ; Mask)
Secondly, using the command netstat -nr
Within the output of the command netstat -nr we will gather two more pieces of information:
- the Gateway address, i.e the router’s network address, which usually looks like 192.168.x.1
- the Destination address, which is the network address (example: 192.168.0.0)
(see below under Gateway & Destination)
Once you have found these five pieces of information, you can go into the interface file and edit the setting using the gathered information.
We do this by entering into your terminal the following command: (we will use sudo to make sure we are accessing the file with the right permissions and then nano to pick what editor we will use. You could choose to open this with any text editor of your choice.
sudo nano /etc/network/interfaces
Within this file we will now enter the following to make our device connect to our network with a static IP address.
Remember that the information I encapsulate within the [.] below is suppose to be yours and brackets should be omitted.
Disclaimer: Medium represents zero’s poorly so on the first line below after eth or wlan those are ZEROS (0) — weak, I know — for correct syntax check at the image below
iface eth0 inet static // using eth0 if you are connected via Ethernet and wlan0 if you are connected over Wifi
address [the IP address you want]
netmask [your netmask]
network [your destination]
broadcast [you broadcast range] //usually the network range ends with .255
gateway [your gateway] //this is the address of your router on the network. It is usually some iteration of 192.168.x.1
Make sure to not forget to save the file when you exit your editor. The end result should look something like this :
After following the instructions above, don’t forget to reboot your Raspberry Pi with the command: sudo reboot
I hope this tutorial was helpful and wish you a great rest of your day!