14

I have been doing some VM testing. I have created and destroyed a large number of VMs on my subnet to test fresh provisioning via puppet. However, it looks like Ubuntu doesn't free a DHCP lease when the system is shut down, and the DHCP lease time is a week. So I have reached a point where I can't get a DHCP lease with a new VM. I could have released the leases on the test VMs manually had I known, but I need to release those leases so I can get my free IPs back. DHCP server is serving some production servers as well, so I can't just nuke all the leases and call it a day.

DHCP server OS is Ubuntu 12.04.4. Using dhcpd for the DHCP server.

Alexis Wilke
  • 2,057
  • 1
  • 18
  • 33
cat pants
  • 2,139
  • 10
  • 33
  • 44
  • Did you configure the network as a /24? If you are using RFC1918 addresses you might as well configure your network with a shorter network prefix. A /16 would last a lot longer, and the overhead of storing a lot of leases on the DHCP server is usually negligible. – kasperd Nov 25 '15 at 21:38

2 Answers2

19

Apparently there is no such elegant method to accomplish the job.

So, let's do it manually.

  • Stop dhcp server.
  • Check your /var/lib/dhcp/dhcpd.leases file and check for the entry. It contains the list of all dhcp leases.
  • Remove the entry carefully and start the dhcp server again.
Diamond
  • 8,791
  • 3
  • 22
  • 37
  • I guess it is a good idea to stop `dhcpd` before editing `dhcpd.leases`. – kasperd Nov 25 '15 at 21:39
  • Will any active servers that have acquired leases from dhcpd experience any connectivity issues while dhcpd is stopped? – cat pants Nov 25 '15 at 22:08
  • @kasperd, you are right. I have updated my answer. – Diamond Nov 25 '15 at 22:23
  • @catpants, a restart should not cause any problem. – Diamond Nov 25 '15 at 22:23
  • 1
    @catpants As long as the lease doesn't expire while the DHCP server is down, the DHCP clients should be unaffected. Additionally clients usually renew their DHCP lease halfway through their lease time, so if your lease time is a week the DHCP clients should keep their address even if the DHCP server was down for a few days. – kasperd Nov 25 '15 at 22:26
  • Wouldn't it be `/var/lib/dhcp/dhcpd.leases`? – Vasconcelos1914 Feb 12 '20 at 15:53
  • @Vasconcelos1914, you are right. Now I have corrected the typo. Thanks! – Diamond Feb 18 '20 at 07:59
0

There is a utility called dhcp_release that can do this. According to the man page you just pass it the interface, IP address, and MAC address, like so:

sudo dhcp_release enp1s0f0 192.168.1.100 01:02:03:04:05:06

It is available in the dnsmasq-utils package. I don't know if it works with any DHCP server other than dnsmasq, but it seems like it should. With dnsmasq it is essential, because dnsmasq does so many other things that stopping it for a while so you can manually edit the leases file may cause headaches.

Chad
  • 101
  • 2