Posts: 3
Joined: Sun May 24, 2020 10:21 am

Assign DHCP IP Based on Mac Address

Sun May 24, 2020 10:25 am

I have a RPi-4 and running as an accesspoint with numerous DHCP clients. I want to assign these clients a fixed IP address based on their Mac Address so every time they request a lease - the same IP is assigned.

I simply added to /etc/dhcpcd.conf

host hostname {
hardware ethernet 50:02:91:e9:fe:6e;

But that doesn't seem to work and the device with the above mac address still gets a random IP assigned.

What's the correct way of configuring this?

Posts: 726
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Assign DHCP IP Based on Mac Address

Sun May 24, 2020 2:51 pm

dhcpCd is the dhcp client, not the server. There are several dhcp servers available, such as isc-dhcp-server, kea, etc. I've been using isc-dhcp-server for a long time, and had the same issue as you: wanted to assign fixed IP addresses to systems and devices.

Turns out that's super-easy to do. The stanza you have included goes in the dhcp server config file (/etc/dhcp/dhcpd.conf for isc-dhcp-server). I edited my own config files for far too long, and finally built a little tool to make it easy to add/view/manage/delete the settings for each IP address.

If you're interested, it's at Some day I'll extend it to support other DNS and DHCP servers, but for now it supports bind9 and isc-dhcp-server, which are both rock-solid. It also supports dynamic DNS naming.

Code: Select all

pisrv1~/tmp# ndm help

ndm builds the dhcp and named/bind configuration files from a database
maintained in a JSON-formatted configuration file.

Use the 'ndm build' command to generate the service configuration files.
Use the 'ndm install' command to install the service configuration files
into the system and restart the services.

usage: ndm [-h]
           {add,build,config,delete,diff,install,list,modify,show,help} ...

Build and install bind/dhcpd config files from database

positional arguments:
                        sub-command help
    add                 Add a new host to the database
    build               Build new host databases in tmp staging area
    config              Manage configuration database
    delete              Delete a host from the database
    diff                Diff current and new config files
    install             Install new config files into the system
    list                List the complete database
    modify              Modify host attributes in the database
    show                Show a single host
    help                Print help

optional arguments:
  -h, --help            show this help message and exit

Use 'ndm command --help' for help on a specific command

pisrv1~/tmp# ndm list                       gw               +nodhcp                                         www              zoneonly                                        xunil            +nodhcp                                         ns1              zoneonly                      dc:a6:32:02:xx:22 pisrv1                                         dc:a6:32:43:xx:7e pisrvx                                         08:00:27:f2:xx:d3 dnx              +nodhcp                       50:1a:c5:a3:xx:3b xbox                       scout            +nodhcp                           

pisrv1~/tmp# ndm add --help
usage: ndm add [-h] [--cname] [--db DB] [--dhcphostopt DHCPHOSTOPT]
               [--dhcponly] [--hostname HOSTNAME] [--hostsonly] [--mac MAC]
               [--nodhcp] [--nodomain] [--note NOTE] [--zoneonly]

positional arguments:
  ip                    New host IP address

optional arguments:
  -h, --help            show this help message and exit
  --cname               Put this entry in DNS zone files as a CNAME
  --db DB               Specify alternate config file
  --dhcphostopt DHCPHOSTOPT
                        Name of dhcphostopt to add to this host's dhcp entry
  --dhcponly            Only put this host in dhcpd.conf (no hosts or DNS
                        config entries)
  --hostname HOSTNAME   New host name
  --hostsonly           Only put this host in hosts file (no dhcpd.conf or DNS
                        zone entries)
  --mac MAC             MAC address for new hostname
  --nodhcp              Don't add host to dhcpd.conf
  --nodomain            Don't add the domain name to this entry; It's already
                        fully qualified
  --note NOTE           Note text for new hostname
  --zoneonly            Only put thishost in DNS config files (no dhcpd.conf
                        or hosts file)
Pi tools:
Free your network from your router's DHCP/DNS and run it on a Pi:
Quickly and easily build customized-just-for-you SD Cards:
Easy strongSwan VPN installer/manager:
Lightweight Virtual VNC Config:

Return to “Networking and servers”