A DHCP network makes network configuration easy. Any node (computer, laptop, or devices) connected to this network will automatically get IP address. A raspberry pi connected on DHCP network would also get the benefit. Using this method, we don’t need to set IP address manually.
Dynamic IP is good, but at some point we don’t need it. Even, it gives us troubles. In some case static IP address is convenient for deep inspection.
In this article we will discuss about how to set up static IP address for raspberry pi. The method we use is similar to Linux-based PC does, unless you use non Linux Operating System for your Pi.
In this article I use:
- Slackware64 14.0 for desktop OS
- Raspbian Wheezy for Raspberry Pi’s OS
Initial Connection, When and What
Where you want to connect or via what network. If you want to do the setting over network, this question must answered first before we proceed. There are two possibilities for doing static IP setup:
- DHCP network in which Raspberry Pi has been assigned with a dynamic IP address. In this scenario we want to set up the board to use a static IP (change from dynamic to static)
- Peer to peer or connecting your PC to Pi directly, in which raspberry pi request for dynamic IP but our desktop doesn’t provide any. This is difficult because your machine won’t be able to communicate to your pi
- peer to peer with DHCP server enabled on your machine. In this scenario the IP is provided by our machine but we need to know what IP it is.
- peer to peer, raspberry pi with static IP assigned before. This is the easiest method. In this scenario, we want to change the IP to something else.
If you don’t want to connect the Pi, we can do another thing. Insert your SD card to your machine host SD card reader. We then can configure the system from this.
Any method is fine and I assume you have known how to connect to your Pi. At least you can access the file system.
Now, here is the core for this article.
If you want to connect Raspberry Pi to internet via gateway (switch/router) then you should specify which router pi connect to. Find out what gateway address for your network by using route command. You should see a lines like this:
Kernel IP routing table
Destination Gaterway Genmask Flags Metric Ref Use Iface default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 loopback * 255.0.0.0 U 0 0 0 lo 192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
In this example, my default gateway for this network is 192.168.1.1. Save this information.
If your intention is only to set Pi’s IP for private connection from your machine to your Pi, you can pick any IP you like. However, make sure both Pi and your machine are on same network.
Now open /etc/network/interfaces file as super user. You can use text editos such as vi. Modify the file so it will similar to this one:
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or # /usr/share/doc/ifupdown/examples for more information. auto lo iface lo inet loopback # # New entries to support static IP follow: auto eth0 iface eth0 inet static hwaddress ether b8:27:eb:b5:e8:90 address 192.168.1.113 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.1
On above snippet, I set the Pi’s IP address to 192.168.1.113/24 with network 192.168.1.0.
Next, rewrite the content of /etc/resolv.conf so. Invoke following command:
cat 'nameserver 192.168.1.1' > /etc/resolv.conf
That line will set our DNS resolver to our gateway.
Reboot your Pi and check whether you get the right IP address by invoking ifconfig command.
If you want to change it back to DHCP aware, change the /etc/network/interfaces content:
# Used by ifup(8) and ifdown(8). See the interfaces(5) manpage or # /usr/share/doc/ifupdown/examples for more information. auto lo iface lo inet loopback # # New entries to support static IP follow: auto eth0 iface eth0 inet dhcp allow-hotplug wlan0 iface wlan0 inet manual wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcpembedded, rpi