Running web-facing services from a Raspberry Pi at home is a cost effective way to avoid big tech surveillance, and take control of your ow digital life. One of the essential steps in making sure everything works as it should is to ensure that your Raspberry Pi has a static local IP address, so it's always at the same place on your network.
Why do you need a static IP address for your Raspberry Pi server?
Congratulations! You've decided to seize back a modicum of control by self-hosting alternatives to proprietary software such as Immich in place of Google Photos, Trilium as an alternative to Evernote, and Mealie for recipes.
Even better, you're doing it on a Raspberry Pi - a well-supported platform that offers stability, low cost, and a low power draw. You've bought a reasonably-priced domain name, and now everything from music and movies to office suites and self-contained WhatsApp alternatives is served from your favourite single board computer.
When you're accessing your services over the internet, your device sends a request to your domain, which after some DNS shennanigans, is forwarded to your domestic IP address and your router. Your router will then forward the request to your Raspberry Pi according to port forwarding rules - assuming they have the right local IP address.
When you first power up your Pi and attach to your local network, it will be assigned an IP address by your router. You may have the same IP address for days, weeks or months, and everything will tick along smoothly.
But the IP address of devices on a local network can change unless you take steps to prevent this. Your router could assign a new local IP address each time it reboots, or when your Raspberry Pi reboots, or after a specific time.
If this happens, you'll need to set up port forwarding again. And then again the next time it happens, and again.
You need to assign a static IP address to your Raspberry Pi server. Fortunately, this is easy to do.
Easily assign a static IP address for your Raspberry Pi server
There are three (technically only two if we're being strictly honest) pieces of information you need to force your Pi to always use the same IP address on your local network. These are:
Your current local IP address
Your Nameserver address or addresses
The IP address of your router
After you've connected to your Raspberry Pi server over SSH (secure shell) double check its current IP address with:
Take note of the first address on the list - it's the one you need.
To get your Nameserver address or addresses, enter:
Eache nameserver will be on a new line that begins, "nameserver"
For the IP address of your router, check the sticker on the box. The most common router address is 192.168.1.1.
With these three items in hand, use the nano text editor to edit your /etc/dhcpcd.conf file:
sudo nano /etc/dhcpcd.conf
At the bottom enter the information you gathered in the following format:
At the bottom of the file enter the information you just gathered:
interface [eth0 for an ethernet connection or wlan0 for wireless ]
static_routers=[Your router IP address]
static domain_name_servers=[Nameserver addresses]
static ip_address=[Your Pi IP address]/24
You can see the relevant section of our dhcpcd.conf file in the screenshot above this section.
Save the file with Ctrl + O then Ctrl + X, and reboot the Pi:
That's it. Your Raspberry Pi server will always be available at the same IP address on your local network.