Forcing re-authentication on public WiFi

I’ve been playing around with (captive portal) public WiFi networks, and needed to force re-authentication in order to do some testing. Here’s how I did it.

Public WiFi networks are surprisingly good at remembering you once you’ve authenticated once. Great for most people, annoying for me. This is what i worked out:

#!/bin/bash
INTERFACE=$1
SSID=$2
nmcli connection delete "$SSID" || exit 1
nmcli radio wifi off
sudo ip link set "$INTERFACE" down || exit 1
sudo macchanger -a "$INTERFACE" || exit 1
sudo hostnamectl set-hostname random-hostname-"$(date +%s)"
sudo systemd-resolve --flush-caches
nmcli radio wifi on
sudo systemctl restart NetworkManager
sudo ip link set "$INTERFACE" up
while [[ $(nmcli dev wifi list | wc -l) -eq 1 ]]; do
 sleep 0.5
done
nmcli dev wifi connect "$SSID"
while ping -c 1 1.1.1.1 2>&1 | grep -q 'Network is unreachable'; do
 sleep 0.5
done
sudo dhclient -r "$INTERFACE" || exit 1

This script relies on macchanger, and dhclient, as well as you using NetworkManager. It forgets your previous connection, then generates a random MAC address for your interface, before reconnecting to the WiFi network. It then refreshes the DHCP lease to get a new IP address. This seems to be enough to re-trigger the captive portal login page.

I’ve only tested this on Sydney City library public WiFi, and I’m not sure whether simply refreshing MAC or DHCP would be enough for another network, or whether more steps would be needed.


Back to the other posts?

Hector Brown

Hi. I was a Physics student in London, now unemployed and travelling around Australia. I’m interested in Unix/Linux, terminal-centric workflows, vim and FOSS in general. Mainly, I write in Python, but I’m interested in Rust, functional languages like Haskell, and shellscript. Take a look at my Github, or email me if you want to get in contact:


2025-11-01