If you’re using your Raspberry Pi as a server of some sort, it might be useful to give it a static IP address — you’ll probably be able to go without this for a long time (years, in my case!), but one day you’ll have a power outage, and when your router comes back up, you’ll find that… well, you’ll find that you can’t find anything! This is where a static IP address can come in handy.
A few notes before we get started:
- There are alternatives to using static IP’s that you might want to look into, including:
- Not actually caring — DHCP-assigned IP addresses might be just fine for what you’re doing. If other devices on your network aren’t referring to the IP address of your Pi, you probably don’t need to worry about it
- Local DNS — if you’re savvy enough to run a local DNS server, you can refer to the Pi by name – i.e., my-awesome-raspberry-pi.my-network.local. This is certainly preferable to static IP’s, but not everyone will be equipped to do this
- These instructions can change with the version of Raspian (or whatever OS) you’re running — the instructions below work on the Stretch version of Raspian
- You’ll need to make sure whatever IP you assign is off limits in your DHCP server. This is most likely going to be your router, and most routers have a way to set aside a range of IP’s that it will not hand out via DHCP – unfortunately, every router is configured differently, so you’ll need to figure that out first.
- Warning: Not doing this first means you’re running the risk of having two devices on the network with the same IP, which will cause…. problems. Make sure you don’t skip this step!
- Just in case you should be ready with a keyboard and a display – whenever you change the network configuration of your Pi, it’s possible that you’ll break it, leaving a direct login the only way of getting in!
Ready? Ok, let’s go!
What You’ll Need
A few things you’ll need first:
- A specific IP selected for your Pi that your DHCP server will not assign to another device (see above)
- The ‘Interface Name’ of the network interface you plan to assign the static IP to — if you’re using Ethernet, this will likely start with ‘eth’, while your WiFi interface will likely start with ‘wlan’.
- The IP address of your router
- The IP address of the DNS server your Pi uses (probably the same as your router)
- SSH access to your Pi
Let’s Do This!
For the purposes of these instructions, we’re going to assume the following:
- Static IP address to assign: 192.168.0.220
- Interface Name: wlan0
- IP address of our router: 192.168.0.1
- IP address of our DNS Server: 192.168.0.1
- Username and password for our Pi: Yeah, sorry, no…
SSH into your Pi, and run:
sudo nano /etc/dhcpcd.conf
This file configures DHCP on our Pi, and toward the bottom you should find a section with an
Example static IP configuration, and a few commented out lines.
Uncomment the lines, and configure them like so:
Reboot the PI with
sudo reboot, and verify that it worked – now all you need to do is update all of your other devices to point to your new-and-improved IP address!