This post’s alternate title is:
Why Raspberry Pi Engineers? WHY???
I just setup a new Raspberry Pi in my home this week and I used the latest NOOBS download and so I installed Raspbian as part of that process. Having done this all a few times before, I thought, awesome – I know how to set everything up already. But no.
The Raspbian Jessie images have changed the way we go about setting up a static ip address on our headless servers so if you are like me and have read many older pages telling you to edit the /etc/network/interfaces file, but that’s not getting you a static IP, then read on, my beleaguered brethren…
Does this apply to you?
If you have a Raspbian OS image from any time after the end of 2015, and if you are trying to setup a static IP address for your Pi, then yes, yes it does.
Before you start
If you’ve already been editing your /etc/network/interfaces file, then I recommend you revert that back to its original definition before you proceed with the new steps below. This is because the file includes settings which the system will still use under certain conditions (and with older apps which don’t yet use dchpcd.conf).
If you are a bad person** and didn’t back up your interfaces file before you started editing it, here’s what my original file looked like as of Feb 2016:
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d
auto lo iface lo inet loopback
iface eth0 inet manual
allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
** You’re not a bad person, just misunderstood.
Note those comments near the top of that file that tell you to go find the dchpcd.conf file. The engineers put those in there as a tip to us luckless losers who were still trying to edit this file in order to get a static IP.
OK, The new way to set a Static IP in Debian Jessie / Raspbian 2016:
Make a backup copy of the /etc/dhcpcd.conf settings file:
# sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.backup
Edit the file in your favorite text editor:
# sudo nano /etc/dhcpcd.conf
If you want the static IP to happen on your Ethernet (hard-wired) network connection, then add lines like this to the top of the file – before the first comment:
interface eth0 static ip_address=10.3.2.8/24 static routers=10.3.2.1 static domain_name_servers=184.108.40.206
OR if you want the static IP to happen on your WiFi (wireless) network connection, then add lines like this to the top of the file – before the first comment:
interface wlan0 static ip_address=10.3.2.8/24 static routers=10.3.2.1 static domain_name_servers=220.127.116.11
Important note: The numbers above are examples only. Which numbers you should use depends on a lot of things I don’t know, like how your LAN is setup, how your Pi will connect to your router and what you want to use as a DNS server. I hope you can find the answers to those things elsewhere if this is all new to you.
Explanation of each line:
- interface = the name of the network connection; this should be either eth0 or wlan0 for 99% of us home users
- static ip_address = the address you want to force your Pi to use. It is unknown to me why we put the /24 at the end there. Maybe some kind soul can ELI5 in the comments here.
- static routers = your gateway / router on the LAN the Pi will connect to.
- static domain_name_servers = the DNS server you want the Pi to connect to when it can’t resolve a network address. 18.104.22.168 is the free Google DNS server but use whatever you prefer there.
Save the file in the text editor and exit. In Nano do this:
<Ctrl-X> Y (for yes, I want to save this file) <Enter> (to keep the same file name)
Reboot the Pi to have the new settings take effect:
If you did that correctly, the Pi should reboot, use its new static IP address and you’re all set.
If you have any problems at that point, double-check how you edited the /etc/dhcpcd.conf file and check these other pages for more info: