Skip to content

Monitor your public IP for changes

To monitor your public IP for changes, save your current IP from curl -s ipconfig.io to a file, then on a schedule compare the live value against the saved one and run an action only when they differ. That difference check is the whole technique: one cheap HTTP request, one string comparison, and a hook for whatever should happen next.

Maintained by the ipconfig.io team · Reviewed 17 June 2026

This guide gives you a copy-pasteable POSIX shell script, a cron schedule to run it unattended, and the places to hook in real actions like updating a DNS record, refreshing a firewall allow-list, or sending a notification.

Why does my public IP change?

Your public IP changes because most residential ISPs assign addresses dynamically rather than statically. Your router leases an address from a shared pool via DHCP, and that lease can be reassigned.

A new address typically appears after a modem or router reboot, a connection drop and reconnect, a DHCP lease expiry, or routine maintenance on the ISP side. None of this is under your control, which is exactly why automated detection is useful. A static IP, where the address never changes, is usually a paid upgrade or a business-plan feature.

If you are unsure whether your address is even directly reachable, your ISP may also place you behind carrier-grade NAT, which is a separate situation worth understanding. See What is CGNAT?.

How do I detect when my public IP changes?

Detect a change by comparing the IP you fetch now against the IP you saved last time, and treat any difference as the change event. The bare / endpoint returns just the IP plus a newline, which makes it ideal for scripting with curl -s.

The script below does exactly that. Save it as check-ip.sh:

sh
#!/bin/sh
# Detect public IP changes and act only when the address differs.
set -eu

STATE="${HOME}/.cache/myip"
mkdir -p "$(dirname "$STATE")"

# Fetch the current public IP. ipconfig.io returns the bare IP + newline.
current="$(curl -fsS --max-time 10 https://ipconfig.io)" || {
  echo "check-ip: lookup failed" >&2
  exit 4
}

# Read the previously stored IP, if any.
previous=""
[ -f "$STATE" ] && previous="$(cat "$STATE")"

if [ "$current" != "$previous" ]; then
  echo "Public IP changed: ${previous:-none} -> ${current}"

  # --- Hook your action here -------------------------------------------
  # update_dns_record "$current"
  # refresh_firewall_allowlist "$current"
  # send_notification "$current"
  # ---------------------------------------------------------------------

  # Persist the new value so the next run compares against it.
  printf '%s\n' "$current" > "$STATE"
fi

Make it executable and run it once to seed the stored value:

  1. chmod +x check-ip.sh
  2. ./check-ip.sh — the first run records your current IP and prints nothing further.
  3. Run it again later. It stays silent while the IP is unchanged and prints a line the moment it differs.

A few details that keep this correct:

  • curl -fsS fails loudly on HTTP errors and stays quiet on success, so a transient outage does not overwrite your saved IP with garbage.
  • The stored value is only updated inside the change branch, so a failed lookup never clears your history.
  • --max-time 10 prevents a hung request from stalling a scheduled run.

To force a specific address family, add -4 or -6 to the curl call. curl -fsS -4 https://ipconfig.io tracks your IPv4 address; -6 tracks IPv6. Pick one per script if you care about a particular record.

How do I run the IP check automatically on a schedule?

Run the check automatically by adding it to cron so it executes every few minutes without you. Cron handles the repetition; your script handles the logic.

  1. Open your crontab: crontab -e

  2. Add a line to run the script every five minutes:

    txt
    */5 * * * * /home/you/check-ip.sh >> /home/you/.cache/myip.log 2>&1
  3. Save and exit. Cron picks up the change immediately.

Use the absolute path to the script, because cron runs with a minimal environment and a bare filename will not be found. Redirecting to a log file gives you a simple history of every detected change and any errors.

If you prefer systemd, the same script works behind a systemd timer; the cron line above is the shortest path for most machines.

What can I do when my public IP changes?

When your public IP changes, the most common actions are updating a DNS record, refreshing a firewall or SSH allow-list, and sending an alert. Each one slots directly into the hook section of the script.

  • Update dynamic DNS (DDNS). Replace the hook with a call to your DNS provider's API, passing $current as the new record value. This keeps a hostname like home.example.com pointed at your changing address, so you can reach your network by name even after the IP moves.
  • Refresh a firewall or SSH allow-list. If a remote server only accepts connections from your home IP, have the hook push $current into that allow-list (for example, via your cloud provider's security-group API) and remove the stale entry. Your access survives the change.
  • Send a notification. Pipe the change line to email, a chat webhook, or a push service so you simply know when your address moved.

Keep each action idempotent and quick. Because the hook only fires on a genuine change, you will not spam your DNS provider or notification channel on every five-minute tick.

To verify what changed by hand at any time, fetch the current value directly with curl ipconfig.io, or pull richer detail from the /json, /country, and /asn-org endpoints described in the reference.

Frequently asked questions

How do I detect when my public IP changes? Fetch your current IP with curl -s ipconfig.io, compare it to the value saved in a file such as ~/.cache/myip, and act only when they differ. Schedule that comparison with cron so it runs on its own.

Why does my home public IP keep changing? Most residential ISPs assign addresses dynamically via DHCP, so your IP can change after a reconnect, a modem reboot, a lease expiry, or maintenance. A static IP is typically a paid or business-plan option.

How often should I check my public IP for changes? Every five minutes suits most dynamic DNS and firewall use cases, keeping records fresh without overdoing it. Match the interval to how fast downstream systems need the update.

Next steps

Geolocation by MaxMind GeoLite2. No tracking, no keys.