NetworkOpen Source SoftwareTutorials

How to manually remove a DHCP lease on OpenWRT router

OpenWRT

Yesterday a mistake happened in my own network. The DHCP server, running on the OpenWRT router, assigned the internal IP 192.168.15.50 to a client. This IP is actually used by my NAS, configured statically on the OS. This situation is called an IP address conflict.

It's not the fault of the DHCP Server (dnsmasq) on OpenWRT – how could it know that there's a machine with a static IP within the lease range? No, that's my fault.

One fix would be to simply change the static IP address of my NAS and use an IP address out of the DHCP range. But as several machines, including printer, have configurations pointing to the SMB shares on the NAS' IP address, I went for a different approach: Reserve 192.168.15.50 in DHCP for the NAS and re-assign a new DHCP IP to the client currently using this IP.

Ironically, the device which picked up the 192.168.15.50 IP, was the OSMC Vero V device. This device runs Kodi and connects to the NAS (using SMB) to play media files. Obviously the connection to the NAS would never work, due to the IP conflict.

Setting a DHCP reservation on OpenWRT

A DHCP reservation is basically setting the same IP address for the same client, every time the client uses DHCP to obtain a dynamic IP address from the DHCP server. It's similar to just configuring a static IP address on the client, with the difference that the DHCP server tells the client which IP to use.

In my situation, the NAS already has a static IP configured. But the DHCP server doesn't know that. A reservation on the DHCP server makes sense here, because it reserves the IP address from being used by other clients.

Login on your OpenWRT router, using the User Interface (called LuCI) over http. In the top navigation use Network -> DHCP and DNS, then click on the Static Leases tab.

There are two tables shown in this page. The first table shows the currently defined static leases (DHCP reservations).

In the screenshot you can see I added a new entry in the static leases table. Important here is to use the MAC address of the device. This is how the DHCP server identifies a unique client.

If you don't find the MAC address of the device in the suggestions, scroll down to the end of the list and enter the MAC address manually (– custom –).

With this entry, the IP address conflict won't happen in the future anymore, as 192.168.15.50 is now reserved for one (and just this one) client.

Note: Do not forget to save your changes (see top right corner on the UI).

Remove the currently active DHCP lease

The second table below shows the current DHCP leases of dynamic IP addresses and their client devices. This table shows that the OSMC device currently still uses 192.168.15.50. And it will do so for the next 23 hours and 59 minutes.

OpenWRT active DHCP leases

Unfortunately there's no button to delete this entry and remove the DHCP lease. You either have to wait until the lease time is over – or you do it the geeky way.

In order to manually delete the active DHCP, log in to your OpenWRT router, using SSH.

SSH connect to OpenWRT

OpenWRT is using a Linux OS in the background and also features a SSH server. You can connect to the OpenWRT system using the IP address and the same credentials you use for the UI login (in most cases the username is "root"). The SSH service might not be started by default. In this case make sure SSH is started. In the LuCI User Interface go to System -> Administration and click on the SSH Access tab.

Once logged in using SSH, you can find the current DHCP leases in a file located in /tmp/dhcp.leases. The file contains the same list you also see in the UI, including the osmc client:

root@OpenWrt:~# grep osmc /tmp/dhcp.leases
1780680203 94:cc:04:60:04:88 192.168.15.50 osmc 01:94:cc:04:60:04:88

To delete the entry you can either edit the file with a terminal editor (vim or nano) or you can use sed to delete a specific line. In this situation I want to delete the line which contains the IP address 192.168.15.50 from the file:

root@OpenWrt:~# sed -i "/192.168.15.50/d" /tmp/dhcp.leases

Now restart the DHCP server, which uses dnsmasq in the background:

root@OpenWrt:~# /etc/init.d/dnsmasq restart

If you're still logged in the OpenWRT web UI, you will notice that the relevant entry has now disappeared from the table.

Now you can reboot the device. In my case I rebooted the OSMC Vero V device. During the reboot, it connected to the DHCP server, asked for an IP (DHCPDISCOVER) and the DHCP Server offered a new lease – this time with a different IP.

Quickly after the osmc device showed up in the table again, this time using 192.168.15.51.

root@OpenWrt:~# grep osmc /tmp/dhcp.leases
1780680631 94:cc:04:60:04:88 192.168.15.51 osmc 01:94:cc:04:60:04:88
OpenWRT DHCP table shows renewed IP address

Claudio Kuenzler
Claudio already wrote way over 1000 articles on his own blog since 2008. He is fascinated by technology, especially Open Source Software. As a Senior Systems Engineer he has seen and solved a lot of problems - and writes about them.

You may also like

Leave a reply

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

More in:Network