Automatically renew WAN lease on pfSense without rebooting

Kovasky Buezo | Dec 10, 2024 min read

Intro

Every now and again I would wake up to my internet being down while my local network being fine. This usually happened when my ISP renewed my assigned IP overnight. The only solution was to reboot my pfSense VM to force the IP lease renewal. Searching the web I discovered it was not uncommon for people to experience this with certain types of modems and virtualized pfSense instances. Here’s how I resolved it in my homelab.

WAN renew script

In your pfSense instance, create a file named wan_renew.sh in /usr/local/sbin with the contents below and make it executable. This script pings two external IPs, and if 50% of them fail, it renews the lease on the WAN interface:

#!/bin/sh

IP1="8.8.8.8"
IP2="1.1.1.1"

LOGFILE="/var/log/wan_renew.log"

ping_test() {
    local ip=$1
    local count=$(ping -c 4 $ip | awk -F', ' '/received/ {print $2}' | awk '{print $1}')
    if [ $count -lt 3 ]; then
        return 1
    else
        return 0
    fi
}

if ! ping_test $IP1 && ! ping_test $IP2; then
    # Replace vtnet0 with your WAN interface
    /usr/local/sbin/dhclient -4 -d -r -lf /var/db/dhclient.leases.vtnet0 -cf /var/etc/dhclient_wan.conf -sf /usr/local/sbin/pfSense-dhclient-script
    php -r "require 'interfaces.inc'; interface_bring_down('wan'); interface_configure('wan');"

    echo "$(date): Pings to $IP1 and $IP2 failed. Lease on wan vtnet0 has been renewed." >> $LOGFILE
fi

Adding the script to the cron service

Ensure the cron package installed. Navigate to Services->Cron and click on Add. Set the settings as shown in the cover picture at the top of this page.

All done!

After applying all settings, your WAN lease should be automatically renewed without having to restart your firewall.