How do you prevent a DHCP release during shutdown?

5,334

Solution 1

Most unix dhcp clients are polite in that they always clean up after themselves and release the lease upon exit. In most cases this makes things flow better in that the server can easily tell if a lease is actively being used, but this is not necessary required for the protocol to function. There are three ways to do what you want cleanly (and one ugly way that just works).

  1. write a dhcp client
  2. modify a dhcp client with a commandline option to change the behavior.
  3. modify a dhcp client just to have the new behavior
  4. kill -9

There are two race conditions that the kill -9 approach introduces

  1. should a lease expire after you killed the dhcp client but before the shutdown completes you will be using an expired lease
  2. if you happen to kill the dhcp client during a renewal the behavior is undefined.

Why this works: The normal behavior of a dhcp client with a valid lease is to spend most of its time asleep it only wakes up to renew the lease or to release it to shutdown. When it receives a signal to shutdown it releases the lease and exits. Signal -9 is not catchable and results in immediate termination without any cleanup, bypassing the lease release. Most (although not all) dhcp clients will just renew a lease if they are started with a valid lease in their cache.

Solution 2

When you get DHCP lease, that lease bound to specific MAC-ID till the expiry. It's immaterial if you switch off your machine or not; all that matters is whether it re-requests an IP address before the lease expires.

Additionally, most DHCP servers work with 'soft' lease expiry, meaning that when a lease officially expires they'll hold it in their cache regardless until and unless they actively need to hand out that particular address somewhere else, or they receive instructions to clean out their cache.

(Note: Some ISPs actively turn this feature off as a security measure — although how effective it is in that regard tends to be debatable — or to prevent customers from running their own servers on the cheap when they're not supposed to. If you're trying to achieve the latter you may want to look into dyndns.)

If you are getting a new IP address every time you boot up, you may want to look into the DHCP server's settings (if you have admin access to it) or at least ask about them (if you don't).

Share:
5,334

Related videos on Youtube

neverendingqs
Author by

neverendingqs

I am adding an "About me" purely for the user card.

Updated on September 18, 2022

Comments

  • neverendingqs
    neverendingqs over 1 year

    I am currently using Ubuntu 14.04, but expect this to be applicable for different distros.

    I would like to prevent a DHCP release during shutdown, and opt for either the lease to expire or for a DHCP renew (if needed) on startup.

    Is this possible?

    EDIT: I (used to) have a distributed application that responded differently depending on the domain name resolution. If the machine is online but the application is unavailable, the application on other machines understands it as unavailable. However, if the IP has been released (via shutdown), the application fails to understand it us unavailable, but hangs or throws an error.

    Unfortunately, I no longer have the setup to reproduce the error, and can't remember if it hanged or threw an error, and what the error was.

    • Admin
      Admin over 9 years
      Are you really sure that this is what happens? I pretty much doubt that any distro will just release a DHCP lease on shutdown. You can force it with -r, but by default, it shouldn't do that. (Is your leases file writable etc? Checked syslog?)
    • Admin
      Admin about 9 years
      Also running Ubuntu 14.04 server ( on a corporate network ) I seem to end up with a new IP every time I reboot. Watching the console output during shutdown I see DHCP release as one of the items and also started wondering if skipping whatever causes the "release" command would allow the machine to reboot and be re-assigned the same IP address. With the same machine on home network the DHCP will persist. Is there a "trick" on some networks to keep the same DHCP address until it expires instead of until a reboot?
    • Admin
      Admin about 9 years
      @sr_: In my opinion most distros will release nowadays and it's IMO the correct behavior.
    • Admin
      Admin about 9 years
      @neverendingqs Please amend your question with the motivation to do that. Are you working around bad configuration of a company network or something like that?
    • Admin
      Admin about 9 years
      Amended. It's been a while though, so I'm not sure if the edits help.
    • Admin
      Admin over 4 years
      In dnsmasq.leases the entry is removed when I shoutdown the client machine. I tried to resolve the hostname -> MAC address for wake-on-lan, but that only works when the machine is running :D, because otherwise dnsmasq does not provide a result. Not even full `dhcp-host="..." with mac,ip,hostname work. Without the lease dnsmasq does not resolve the host.
  • Pavel Šimerda
    Pavel Šimerda about 9 years
    My experience in a couple of networks is different. You may get a new IP address after reboot even when you are still in the expiration time. Also different DHCP servers may react substantially different to renew/rebind/release events.
  • neverendingqs
    neverendingqs about 9 years
    When you say modify, do you mean source code, or via configurations or command line options?
  • hildred
    hildred about 9 years
    source code, although someone may have done so, I am not aware of it.
  • hildred
    hildred about 9 years
    this is only half effective in that although the server has not been notified of the release, the client has.