Assign a static IP to DHCP client

2 min read

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.