Bash

Get Public IP Address

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Several free services return your public IP as plain text. Useful when scripts running behind NAT need to know what address the outside world sees.
Bash
Raw
# Single quick lookup (any of these work)
curl -s https://ifconfig.me
curl -s https://icanhazip.com
curl -s https://api.ipify.org
curl -s https://ipinfo.io/ip

# With fallback chain — try several until one responds
get_public_ip() {
    local ip
    for src in https://ifconfig.me https://icanhazip.com https://api.ipify.org; do
        ip="$(curl -fsS --max-time 5 "$src")" && [[ -n "$ip" ]] && { echo "$ip"; return 0; }
    done
    echo "Could not determine public IP" >&2
    return 1
}

ip="$(get_public_ip)"
echo "Public IP: $ip"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.