Tuesday, 29 December 2015

Assign A Static IP to The Raspberry Pi

 

Assign A Static IP to The Raspberry Pi


 gkarthik

 

Step 1: Getting Ready

What You Will Need:
- Raspberry Pi
- A MicroSD card with Raspbian installed.
 To see how to install Raspbian read This Instructables or watch This Video
- Connection to the router (The router doesn't need to have a internet connection).
- A USB keyboard and Mouse (not needed if you want to SSH into the pi)
- A Display (not needed if you want to SSH into the pi)
- A power adapter.
Setting Up The Pi:
- Connect all the peripherals to the Pi and connect power.
- Log in and open command line or the terminal (if you are in the desktop enviornment).
- Proceed to the next step.

Step 2: Gathering Intelligence

Before we change any settings on the pi, we need to collect some information regarding our network.
- In the command line type the following command and hit enter.
ifconfig
- From the info that comes up, note down the following things:
-> inet addr
-> Bcast
-> Mask
- When you have got all this information, you need even more information. Enter the following command and hit enter
netstat -nr
- From the table that comes up note down the following:
-> Destination
-> Gateway
This is what I got:
  • inet addr : 192.168.137.82
  • Bcast : 192.168.137.255
  • Mask : 255.255.255.0
  • Destination : 192.168.137.0
  • Gateway : 192.168.137.1
These numbers will most probabaly will be different for you so don't copy mine by mistake.
After you have got all this, you can proceed to the next step.

Step 3: Execution

Now we can finally get a Static IP.
Enter the following command and hit enter.
sudo nano /etc/network/interfaces
A file editor will open up.
- In the third line, 'dhcp' means that we are currently getting our IP address from the router.
iface eth0 inet dhcp
- In this line replace 'dhcp' with 'static'
iface eth0 inet static
- Now in the line below this you need to write type the following
<p>address [your inet addr]<br>netmask [your mask address]
network [your destination address]
broadcast [your bcast address]
gateway [your gateway address]</p>
Finally your whole document will look something like this:
<p>auto lo</p><p>iface lo inet loopback
iface etho inet static
address 192.168.137.82
netmask 255.255.255.0
network 192.168.137.0
broadcast 192.168.137.255
gateway 192.168.137.1</p><p>allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp</p>
Be careful to enter YOUR addresses, don't use my numbers by mistake
- Save the file by pressing cntrl + X , then Y , then enter.
- Restart the pi by running the command.
sudo reboot
- After the reboot is complete, you should have a static IP address!!!!
Your Static IP will be the inet address that you noted down earlier.

Step 4: Alternative

While the above mentioned method will certainly work in almost all scenarios irrespective of your router and network, a better method to assign a static IP would be to do it through your router settings.
All the routers, even of different models from the same company, have very different setting layout and option availability. Thus there cannot be a unified guide for all the routers. You should check out the user manual that came with your router to see how to assign a static IP through your router. Doing so will require you to know the MAC Address of your Pi. You can know the MAC address by running the command "ifconfig", in the first line, the HWaddr if your MAC address.
You need to assign the static IP to the MAC address of your Pi.

2 comments: