How to set a static IPv4 address on a Raspberry Pi using dhcpd

How to set a static IPv4 address on a Raspberry Pi using dhcpd

Here's a quick tip, that also applies to any linux machine using dhcpd.

Every now and then my Raspberry Pi or my router can reboot, and I'm not certain than my Pi will get the same IP address from the DHCP server.

Sure, I could fix it on the DHCP server using the Pi's MAC address, but if you're a lazy bum like me you can tell dhcpd to request a specific IPv4 address.

Add theses lines in /etc/dhcpd.conf:

interface eth0
static ip_address=192.168.1.42/24
static routers=192.168.1.1

You NIC is probably going to be eth0, but make sure to set the 2 last lines to the correct parameters (client address, netmask and gateway).
You can get your current IP configuration by running the ifconfig or ip a commands.

Once it's done, restart the dhcpd service or reboot.

Stanislas
Author
Stanislas
I like building things with code and computers

Comments

2Atom feed
Markdown supported
  1. Lord

    But why not use a static IP ? You could get rid of your DHCP service and still be sure to always find your raspi with the same IP address.

    1. StanislasAuthor

      Yep. Actually I found this when I needed a static IP but didn't have access to the router. It can be useful :)