# Drop dhcpcd lease before connecting

## firespace

I have a peculiar problem with my USB-modem. When modem is shut off, it forgets about all DHCP leases and it resets every time one tries to rebind a DHCP lease a modem knows nothing about, so if my modem gets disconnected due to a bad USB cable, I can no longer connect until I remove /var/lib/dhcp/*.lease. The problem is I don't know how to delete it automatically. I have already tried digging into configuration files, but to no avail.

Is there any way to make dhcpcd forget about a lease right after a link goes down or when a USB-modem goes up?

----------

## khayyam

 *firespace wrote:*   

> Is there any way to make dhcpcd forget about a lease right after a link goes down or when a USB-modem goes up?

 

firespace ... are you using netifrc to manage the interface? If so you can do something like the following:

/etc/conf.d/net

```
preup() {

   [ -f "/var/lib/dhcpcd/dhcpcd-${IFACE}.lease" ] && {

      rm -f /var/lib/dhcpcd/dhcpcd-${IFACE}.lease

   }

   return 0 

}
```

HTH & best ... khay

----------

## lost+found

Welcome to the forums!

Another way is to use /etc/dhcpcd.enter-hook (See `dhcpcd-run-hooks' manual), and execute commands on dhcp events. Even a firewall script can be (re)run here.

```
case "${reason}" in

   #BOUND|INFORM|REBIND|REBOOT|TIMEOUT|STATIC)

   #   /root/scripts/firewall.sh &>/dev/null

   #   ;;

   PREINIT|NOCARRIER)

      rm -f /var/lib/dhcpcd/dhcpcd-eth0.lease

      ;;

esac
```

----------

## UberLord

Or add 

```
reboot 0
```

 to dhcpcd.conf

----------

