How to make sure the Raspberry Pi has always the same IP address
A step by step guide to configuring a router to assign a static DHCP IP address in your LAN to the Raspberry Pi
I recently got a Raspberry Pi for testing, and to build some projects ideas I have, and right after installing Raspbian, the Linux version of Debian specifically made for the Raspberry Pi, I had a problem.
I attached the Raspberry Pi to the TV, using the HDMI cable, and I attached an USB mouse and USB keyboard to install the OS and get all “wired up”.
I then set up the VNC Server on the Pi to be able to connect to it from the Mac.
As I was removing all those cables, in order to only let the Raspberry Pi attached to the power cable, I realized that as soon as I restarted it, the IP address assigned to it would change.
This is because of DHCP, the protocol that is used by the WiFi router. It does not assign a fixed IP to every device connected: the IP changes all the time.
Sometimes it’s 192.168.1.2
.
Sometimes it’s 192.168.1.30
.
Sometimes it’s 192.168.1.43
.
I don’t really want to spend time every time to find what’s the Raspberry Pi IP address, right? It’s annoying.
So I found out that I can assign a fixed IP to a specific device, by identifying its the MAC address. The MAC address, aka Media Access Control Address, is a unique identifier. Every device has a different one.
So I connected to my WiFi router, which is running on IP 192.168.1.1
on my local network, and I went to the DHCP Server menu.
In there, I clicked “Static DHCP” and I was able to assign a specific IP to the MAC address of my Raspberry Pi:
How did I find the MAC address of the Pi?
I knew the IP address because the VNC Server panel on the Raspberry Pi showed it:
Then using my MacBook Air I scanned the network using:
ifconfig | grep broadcast | arp -a
This printed the IP and MAC addresses of all devices connected to the network, including the one I was interested in, the Raspberry PI:
? (192.168.1.42) at dc:a6:32:60:20:81 on en0 ifscope [ethernet]
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025