Assign a static IP to DHCP client
After setting up a DHCP server on a Raspberry Pi running Linux I get working leases for my clients. However, these are not static. It can happen that my smartphone gets a new IP address the next it connects: 192.168.0.161 instead of 192.168.0.160. For some clients I want to make sure they always use the same IP. This can achieved with ISC DHCP Server by registering a static lease for a specific MAC.
Example
I’ll use my soundbar for the rest of this blog as an example. The MAC of the network card is bc:30:d9:2a:c9:50. I want to always assign the IP 192.168.0.152 to the soundbar.
Find out client data
To find out the client data like MAC and current lease, check the DHCP server log. Or take a look at the back of the device or its settings to find out the MAC. For the DHCP server log on assigned leases:
sudo systemctl status isc-dhcp-server.service
The last line shows that the DHCP server assigned an IP to a client and shows also the MAC address.
DHCPACK on 192.168.0.152 to bc:30:d9:2a:c9:50
Let’s make sure the MAC bc:30:d9:2a:c9:50 always gets the IP 192.168.0.152.
Configuration
sudo vim /etc/dhcp/dhcpd.conf
This is the DHCP server configuration file. I already configured it for a subnet 192.168.0.x where the server is assigning leases for the IP addresses in the range of 192.168.0.150 to 192.168.0.240.
Inside the subnet configuration, I have to add a configuration for the soundbar for IP 192.168.0.152.
host soundbar { hardware ethernet bc:30:d9:2a:c9:50; fixed-address 192.168.0.152; }
The complete dhcpd.conf file will look like this:
subnet 192.168.0.0 netmask 255.255.255.0 { range 192.168.0.150 192.168.0.240; option routers 192.168.0.1; option domain-name "itsfullofstars.de"; option domain-name-servers 8.8.8.8, 8.8.4.4; host soundbar { hardware ethernet bc:30:d9:2a:c9:50; fixed-address 192.168.0.152; } }
Activate configuration
To activate the new configuration, make either DHCPD load the new configuration from file, or restart the service.
sudo systemctl restart isc-dhcp-server.service
Check the status of the service.
sudo systemctl status isc-dhcp-server.service
Result
The assigned leases can be found in the dhcpd.leases file. All leases assigned are listed here, including the mac address, IP address, start and end time of the lease. If all works out as planned, the soundbar will be in there with the static IP.
sudo more /var/lib/dhcp/dhcpd.leases
8 Comments
Dillon · July 17, 2020 at 23:24
With a few exceptions all of my Wi-Fi devices use DHCP addresses, but the printers, AND and Plexserver all need static so this is useful to me. I’ve been able to configure them manually, but not every device is that friendly.
Johnny · August 21, 2020 at 19:50
Very useful. I configure them also in Bind9 on the Pi along with Mac assignment for the IP addresses and domain that I set, so that I can just say “ping laptop” and it pings the PC laptop or “ping mbp” and it pings the MacBook. Makes doing VNC a bit easier, or setting up dev and production web servers, database servers, and so on.
Anthon · October 7, 2021 at 07:59
The entry in the leases file, in your Result section, is IMO there because of the earlier dynamic assignment of that number.
It might be that isc-dhcpd is smart enough to not assign 192.168.0.152 to another machine, essentially excluding the fixed address from your dynamic range. IMO it is better to have fixed addresses outside of that dynamic range, that way you can have a backup DHCP server with the shared fixed addresses, but non-overlapping dynamic ranges.
Tobias Hofmann · October 7, 2021 at 16:56
DHCPD assigned .152 to that host because of the configuration. The IP is in the range of the dynamic IPs, it’s only that only the soundbar host is getting that IP. DHCPD won’t assign .152 to another host, as it is reserved. If the host requesting an IP does not match the MAC adress, it won’t get the IP assigned.
Dhruv Ranparia · February 5, 2022 at 16:11
Hello Tobias,
I have installed ISC-DHCP on Centos 7.
The issue I am facing with ISC-DHCP is it dose not store lease data for Reserved IP address.
Please find DHCP configuration as below.
log-facility local7;
allow leasequery;
update-static-leases true;
next-server 172.16.0.2;
stash-agent-options true;
subnet 192.168.43.1 netmask 255.255.255.224 {
}
subnet 172.16.116.0 netmask 255.255.255.0 {
# CMTS-1
range 172.16.116.10 172.16.116.254;
option routers 172.16.116.1;
option broadcast-address 172.16.116.255;
default-lease-time 60; max-lease-time 60;
all-clients;
host pc1 {
hardware ethernet 00:e4:00:f3:dd:0e;
#hardware ethernet c4:04:15:7f:fc:dd;
fixed-address 172.16.116.5;
stash-agent-options true;
}
}
Tobias Hofmann · February 7, 2022 at 08:59
Hi Dhruv,
what do you want to achieve? The IP for the host is fix, why let the DHCP server manage the lease data? The usage of the flag update-static-lease is not recommended: https://linux.die.net/man/5/dhcpd.conf
Are you sure that you need this configuration?
Dhruv · February 9, 2022 at 21:45
Hello Tobias,
Thank you for the respond and removed the update-static-lease
in the Network my CPE device is connected via Cable Modem(CM).
in order to authenticate the CPE I require to have the CM mac which we are getting in option 82.
In case of Host reservation where we require to bind a CPE to a specific IP(which is not technically configured in the CPE)
hence from next time onwards DHPC shall return only the specific IP to CPE. in this case I am not getting any lease entry for the said IP thus no option 82 info so can’t authenticate the client.
Dhruv Ranparia · February 10, 2022 at 13:09
Hello Tobias,
Thank you for the response.
I will try to explain the complete requirement in brief here.
I want to bind some clients to a fixed IP, hence it will make sure that this clients will only Get fixed IP from DHCP server every time. (This IP is not configured statically in the their devices.)
in order to authenticate such devices we require MAC address and along with that some option 82 details. and we can get option 82 details in DHCP server only if there is a lease entry available in lease file.
hence require to have lease data available for reserved IP address if it’s allocated to the client.