What's new

Tutorial Making DHCP leases survive a reboot, and optionally extending DHCP lease time beyond default limits

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

drinkingbird

Part of the Furniture
If you want DHCP leases to be preserved after a restart (mostly for auditing and historical data, since clients will typically get the same IP after a reboot even if the lease is no longer present), it is relatively straightforward.

Also, optional configs to extend your LAN lease time beyond the 7 day limit in the GUI, and/or extend the Guest Network 1 lease beyond the 1 day default (which can't be changed in the GUI).

### Go to the very bottom for the short version that sets all your leases to 30 days and has them survive a reboot.

This is on my RT-AC1900 (RT-AC68U variant). Should work on others, but you'll need to check if the setup is the same and/or test it out.

First, let's preserve your current lease table and move it to JFFS:
cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases
(or put it in a subdirectory of jffs if you want)

Now create a script in /jffs/scripts named dnsmasq.postconf
If you used a subdirectory when copying above, update the below to match that
#!/bin/sh
#Replace the default lease file with a symbolic link to one on JFFS so it survives a reboot
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

Then like any script do chmod a+rx /jffs/scripts/* to make it executable

Now do service restart_dnsmasq

All set, leases will be kept after a reboot. Technically each reboot will make them a couple minutes out of synch with the client, but since every cilent will renew upon router reboot (or when the client comes online next), it will synch back up.

_____________________________________________________________________________________________________________________________

If you want to extend your lease time beyond the 7 day max for LAN, or the 1 day hardcoded default for Guest Wireless 1:

Create a script called services-start in /jffs/scripts if you don't already have one
#!/bin/sh
#Set the Main LAN lease time to 30 days
nvram set dhcp_lease=2592000
If you want something other than 30 days, you can use that, but it must be specified in SECONDS
If all you want to change is the main LAN lease time (or you aren't using Guest Wireless 1) skip to nvram commit below
If you're changing Guest Wireless 1 also, include these two below (again, use whatever value you want in seconds)
Note that these 2 values don't appear to actually get used anywhere, but just in case they do (or do in the future) can't hurt to update them
nvram set lan_lease=2592000
nvram set lan1_lease=2592000
nvram commit

If all you want to do is the main LAN (or you aren't using Guest Wireless 1) above is all you need.

Again do chmod a+rx /jffs/scripts/* to make it executable
This won't take effect until you reboot the router, (or you could run the script manually and restart dnsmasq if you don't want to reboot, the script will be used next time you reboot)
The router GUI DHCP settings will reflect the 2592000 lease time, but if you try to edit it in the GUI, you'll see it won't let you unless you drop it back to 7 days or less.

If you want to adjust the Guest Wireless 1 Pools - continue below

Add below in the same dnsmasq.postconf script that was created earlier to change the lease file location:
Below will update all 1 day lease times to 30 days (specifically targeting the guest wireless 1 pools here since they are hardcoded to 1 day)
**If your main LAN is set to 1 day lease time, this will update that to 30 days also. Make sure you're using something other than 1 day (86400) for LAN, or set it to 86399 or 86401 in the GUI if you want LAN to keep 1 day.
The LAN should not be adjusted with this script, it should be done via the GUI for up to 7 days, or via the NVRAM script above for over 7 days.
If you want something other than 30 days (less or more), convert it to seconds and put it in place of 2592000
Technically you can specify days in the format like "30d" instead of "2592000s" but I wanted to stay with what the router does by default
#Replace all 1 day lease times with 30 days
sed -i "s/86400s/2592000s/g" "/etc/dnsmasq.conf"

Again do service restart_dnsmasq unless you're going to do the reboot below right away.

To test everything is working right, you can reboot the router and you should see the leases are all still there, and are showing 30 days or whatever you chose. You can SSH in and verify that the symbolic link is still in place and the file on JFFS is getting updated (timestamp will update when you do a renew on any client).

###############################################################################################################
Below is the full config to set all DHCP pools to 30 day lease times, and have the leases survive a reboot by putting them in the root of /jffs:

cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

services-start script:
#!/bin/sh
nvram set dhcp_lease=2592000
nvram set lan_lease=2592000
nvram set lan1_lease=2592000
nvram commit

dnsmasq.postconf script:
#!/bin/sh
sed -i "s/86400s/2592000s/g" "/etc/dnsmasq.conf"
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

Now do chmod a+rx /jffs/scripts/* and then reboot the router.
 
If you want DHCP leases to be preserved after a restart (mostly for auditing and historical data, since clients will typically get the same IP after a reboot even if the lease is no longer present), it is relatively straightforward.

Also, optional configs to extend your LAN lease time beyond the 7 day limit in the GUI, and/or extend the Guest Network 1 lease beyond the 1 day default (which can't be changed in the GUI).

### Go to the very bottom for the short version that sets all your leases to 30 days and has them survive a reboot.

This is on my RT-AC1900 (RT-AC68U variant). Should work on others, but you'll need to check if the setup is the same and/or test it out.

First, let's preserve your current lease table and move it to JFFS:
cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases
(or put it in a subdirectory of jffs if you want)

Now create a script in /jffs/scripts named dnsmasq.postconf
If you used a subdirectory when copying above, update the below to match that
#!/bin/sh
#Replace the default lease file with a symbolic link to one on JFFS so it survives a reboot
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

Then like any script do chmod a+rx /jffs/scripts/* to make it executable

Now do service restart_dnsmasq

All set, leases will be kept after a reboot. Technically each reboot will make them a couple minutes out of synch with the client, but since every cilent will renew upon router reboot (or when the client comes online next), it will synch back up.

_____________________________________________________________________________________________________________________________

If you want to extend your lease time beyond the 7 day max for LAN, or the 1 day hardcoded default for Guest Wireless 1:

Create a script called services-start in /jffs/scripts if you don't already have one
#!/bin/sh
#Set the Main LAN lease time to 30 days
nvram set dhcp_lease=2592000
If you want something other than 30 days, you can use that, but it must be specified in SECONDS
If all you want to change is the main LAN lease time (or you aren't using Guest Wireless 1) skip to nvram commit below
If you're changing Guest Wireless 1 also, include these two below (again, use whatever value you want in seconds)
Note that these 2 values don't appear to actually get used anywhere, but just in case they do (or do in the future) can't hurt to update them
nvram set lan_lease=2592000
nvram set lan1_lease=2592000
nvram commit

If all you want to do is the main LAN (or you aren't using Guest Wireless 1) above is all you need.

Again do chmod a+rx /jffs/scripts/* to make it executable
This won't take effect until you reboot the router, (or you could run the script manually and restart dnsmasq if you don't want to reboot, the script will be used next time you reboot)
The router GUI DHCP settings will reflect the 2592000 lease time, but if you try to edit it in the GUI, you'll see it won't let you unless you drop it back to 7 days or less.

If you want to adjust the Guest Wireless 1 Pools - continue below

Add below in the same dnsmasq.postconf script that was created earlier to change the lease file location:
Below will update all 1 day lease times to 30 days (specifically targeting the guest wireless 1 pools here since they are hardcoded to 1 day)
**If your main LAN is set to 1 day lease time, this will update that to 30 days also. Make sure you're using something other than 1 day (86400) for LAN, or set it to 86399 or 86401 in the GUI if you want LAN to keep 1 day.
The LAN should not be adjusted with this script, it should be done via the GUI for up to 7 days, or via the NVRAM script above for over 7 days.
If you want something other than 30 days (less or more), convert it to seconds and put it in place of 2592000
Technically you can specify days in the format like "30d" instead of "2592000s" but I wanted to stay with what the router does by default
#Replace all 1 day lease times with 30 days
sed -i "s/86400s/2592000s/g" "/etc/dnsmasq.conf"

Again do service restart_dnsmasq unless you're going to do the reboot below right away.

To test everything is working right, you can reboot the router and you should see the leases are all still there, and are showing 30 days or whatever you chose. You can SSH in and verify that the symbolic link is still in place and the file on JFFS is getting updated (timestamp will update when you do a renew on any client).

###############################################################################################################
Below is the full config to set all DHCP pools to 30 day lease times, and have the leases survive a reboot by putting them in the root of /jffs:

cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

services-start script:
#!/bin/sh
nvram set dhcp_lease=2592000
nvram set lan_lease=2592000
nvram set lan1_lease=2592000
nvram commit

dnsmasq.postconf script:
#!/bin/sh
sed -i "s/86400s/2592000s/g" "/etc/dnsmasq.conf"
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

Now do chmod a+rx /jffs/scripts/* and then reboot the router.
nice work!

I usually use sed -i "s/86400s/infinite/g" "/etc/dnsmasq.conf"

For a windows device, that usually means.

1674120139019.png
 
...
cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

....
dnsmasq.postconf script:
#!/bin/sh
sed -i "s/86400s/2592000s/g" "/etc/dnsmasq.conf"
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases
I recommend modifying the following commands to take into account those routers on which the location of the "dnsmasq.leases" file is *not* the "/tmp/var/lib/misc" directory.

FROM:
Bash:
cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

TO:
Bash:
cp -fp /var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

FROM:
Bash:
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

TO:
Bash:
ln -sf /jffs/dnsmasq.leases /var/lib/misc/dnsmasq.leases

My 2 cents.
 
I recommend modifying the following commands to take into account those routers on which the location of the "dnsmasq.leases" file is *not* the "/tmp/var/lib/misc" directory.

FROM:
Bash:
cp /tmp/var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

TO:
Bash:
cp -fp /var/lib/misc/dnsmasq.leases /jffs/dnsmasq.leases

FROM:
Bash:
ln -sf /jffs/dnsmasq.leases /tmp/var/lib/misc/dnsmasq.leases

TO:
Bash:
ln -sf /jffs/dnsmasq.leases /var/lib/misc/dnsmasq.leases

My 2 cents.

Fair enough, not sure how other routers are set up, in mine just figured I'd follow the "real" path instead of the var symlink. Obviously "your router may vary".

Since it is a one time copy (and not done repeatedly with script) didn't bother with the force switch, since the file is not there already. Also it preserved the attributes so didn't bother with that either. Neither hurts though.
 
...
Add below in the same dnsmasq.postconf script that was created earlier to change the lease file location:
Below will update all 1 day lease times to 30 days (specifically targeting the guest wireless 1 pools here since they are hardcoded to 1 day)
**If your main LAN is set to 1 day lease time, this will update that to 30 days also. Make sure you're using something other than 1 day (86400) for LAN, or set it to 86399 or 86401 in the GUI if you want LAN to keep 1 day.
The LAN should not be adjusted with this script, it should be done via the GUI for up to 7 days, or via the NVRAM script above for over 7 days.
To prevent changing the DHCP Lease Time for the "lan" interface and target *only* the default WLAN Guest Network interfaces (i.e. br1 & br2) you can do the following in the "dnsmasq.postconf" script:
Bash:
NewLeaseTime=2592000s
for IFace in br1 br2
do
   lineNum="$(grep -ne "^dhcp-range=${IFace},.*,86400s$" /etc/dnsmasq.conf | awk -F ':' '{print $1}')"
   [ -n "$lineNum" ] && sed -i "$lineNum s/86400s/${NewLeaseTime}/" /etc/dnsmasq.conf
done

All these recommended changes are for the benefit of others using your tutorial on different routers.
 
To prevent changing the DHCP Lease Time for the "lan" interface and target *only* the default WLAN Guest Network interfaces (i.e. br1 & br2) you can do the following in the "dnsmasq.postconf" script:
Bash:
NewLeaseTime=2592000s
for IFace in br1 br2
do
   lineNum="$(grep -ne "^dhcp-range=${IFace},.*,86400s$" /etc/dnsmasq.conf | awk -F ':' '{print $1}')"
   [ -n "$lineNum" ] && sed -i "$lineNum s/86400s/${NewLeaseTime}/" /etc/dnsmasq.conf
done

All these recommended changes are for the benefit of others using your tutorial on different routers.

You can also just use a couple sed commands to find br1 and br2 and change them that way, I just figured it would be easier to tell people to change the LAN lease to 1 second more or less to avoid the script replacing it. Yours is more elegant as you are only replacing the time rather than doing two simple SED commands which requires you to specify the rest of the line (start and end IPs etc).

I guess I'd be surprised if people wanted 1 day on the LAN and 30 on the two guests but it is possible. Figured those people could just adjust the LAN to "1 second off from 1 day" since it would be the same thing really.

Obviously some routers now have 3 guest VLANs (501, 502, 503) so those would need further modification too. With the info posted here by you me and others people should be able to cobble together whatever they need.

Not that it is a hugely common ask but I like having it this way. I actually found a post back from 2015 where someone did something similar (not 30 day lease but making the leases survive a reboot).
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top