Normally when devices connect to the network they are assigned IP address by the router which does so sequentially (in the order of devices being connected) or randomly. The same goes with your Raspberry Pi. But if you wish to use your Pi without a display (for e.g. via SSH, as a Web Server, or any remote project) then letting the router assign a IP address is generally not a good idea. In such cases you should give your Pi a static IP so that it is always accessible on the same address. That is what we are going to do in this Instructables.
Watch the video above or continue reading for Step-By-Step Instructions.
Watch The Video HERE
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.
Thanks for reading this Instructables!!
Take a look at my other Instructables.
Checkout the youtube video for clearer instructions here
You can follow me on Twitter
Also checkout my Youtube Channel
You can even follow me on Google+
For more detail:How To Assign A Static IP to The Raspberry Pi