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=8.8.8.8
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=8.8.8.8
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. 8.8.8.8 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:
sudo reboot
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:
https://www.reddit.com/r/raspberry_pi/
https://www.raspberrypi.org/forums/
Have fun.
The /24, as I understand it, is the subnet mask (netmask) in shorthand notation. It means the first 24 bits of the netmask IP are binary ‘1’s, and the rest zeros. Splitting this into bytes with 8 bits, we have 3 bytes of 255, creating a netmask of 255.255.255.0, meaning only the last octet of the IP address will work (i.e. 10.3.2.8 and 10.3.2.1 are on the same subnet, but 10.3.3.2 is not, and therefore will have trouble communicating).
LikeLiked by 2 people
Cool, that makes sense. Thanks!
LikeLike
The slash /24 is also known as CIDR format. https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
LikeLiked by 1 person
Bless You a thousand times! I have been searching the ‘net for ages and the main Raspberry pi forums for this answer but to no avail.
Following your oh-so-clear and brilliant instructions (inluding the commands for backup!), I had my Pi grabbing hold of a static IP address on wlan0 in less than 5 minutes!
Many Many Thanks!
ps: why is your post and instructions NOT on the main Pi forums? It is just so clear and well written!
LikeLiked by 1 person
How can multiple DNS servers be added? By adding addresses one after another separated by commas, or on multiple lines?
LikeLike
Good question. See if this StackOverflow post helps: http://serverfault.com/questions/415309/dhcpd-multiple-domain-name-servers
The idea seems to be that we should use a comma and a space to separate the values and end the line with a semi-colon, like this:
static domain_name_servers=8.8.8.8, 8.8.4.4;
// corrected 2nd IP to 8.8.4.4 – thanks Bill P.
LikeLike
I think the second should be 8.8.4.4
LikeLike
This is great, and tidily solves my problems today. Thank you for sharing!
LikeLiked by 1 person
I wish to add a second IP address to the PI3 using the same Ethernet cable & lan, as I have 2 subnets on the lan. One subnet is strictly inside, the other subnet communicates to the outside world.
I have tried the various methods found by googling, but can’t seem to make it play.
I am using Jessie lite & regular full Jessie builds on my Pi3.
Any ideas?
Thanks in Advance
LikeLike
Sorry, Bob, I’ve not had to solve that problem myself.
Is anyone else able to answer this for Bob?
LikeLike
Looking for the same thing, Trying to use eth0 for internal subnet and wlan as internet facing, I am using raspbian jessie
LikeLike
We finally got it to work in a roundabout way…
A co-worker & I did this method of getting the second IP on the PI.
A crude method to add a second IP address to an ethernet interface on Pi
In your /etc/rc.local file
add this line before the exit 0 command
to create an alias ethernet interface and assign different IP to it.
substitute your IP for the XXX’s shown. Change the netmask as needed.
sudo ifconfig eth0:1 XXX.XXX.XXX.XXX netmask 255.255.255.0 up
LikeLiked by 1 person
Thank you so much, you saved my ̶l̶i̶f̶e̶ day !
LikeLike
What is static domain_name_servers? is that needed for access outside the local network? could be any DNS service?
LikeLike
Hi – (I replaced your specific DNS provider’s name with “any DNS service, just FYI)
Yes, that IP address can point to any DNS service you like. Your own ISP may offer one, and Google offers theirs, which is the example I used in the article (8.8.8.8 and 8.8.4.4).
LikeLike
Thanks! I’m totally aware of this, however, I’m facing a different issue. I have internal DNS Servers at home and even though the file /etc/resolv.conf is showing my DNS servers I cannot resolve any internal IP hosts…. however, I can resolve external hosts like google.com or any other without a problem.. I’m guessing this new way of configuring the IPs is doing something odd. I added the DNS Servers as described in many forums and also in the comments here. I even tried jsut adding one DNS server to make sure I’m not Fing it up with the multiple DNS Servers way of adding them, bt still no luck…
LikeLike
You probably need the DNS server that the DHCP SERVER uses, as that is the one know the connection between name and A and AAAA records (IP addresses).
And most home routers have dnsmasq running, which means that it have a DHCP-server, DNS-server and TFTP-server. All used to help your local LAN.
So, set the DNS-server to the same as your router, and then it probably work better.
You could also use the mDNS protocol (install packages avahi i your linux machines). Then it will be added in the domain .local. That is probably the easier way to solve your problem.
LikeLike
I was having issues until I used this config for DNS (no comma no semi colon):
interface eth0
static ip_address=192.168.1.10/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8 8.4.4
LikeLike