Retrieve Your Public IP with Shell Commands and a Robust Bash Script
This guide explains why home broadband users need to discover their dynamic public IP, compares browser, cURL, and dig methods for fetching it, and provides a Bash script that intelligently falls back between multiple services to ensure reliable IP retrieval for DDNS and other network tasks.
Why Detecting Your Public IP Matters
Home broadband connections often receive dynamic IP addresses from ISPs, which can change on each dial‑up or at scheduled intervals. This variability disrupts services that rely on a stable external address, such as monitoring tools or network storage, making automatic public‑IP detection essential for DDNS updates.
Simple Methods to Get the Public IP
Browser‑Based Lookup
Visiting a service like https://checkip.amazonaws.com returns the client’s public IP in plain text. Many similar endpoints exist, for example:
https://api.ipify.org
https://ifconfig.me/ip
https://icanhazip.com
https://ipinfo.io/ip
https://ipecho.net/plain
https://checkipv4.dedyn.iocURL Command
From the terminal, curl https://checkip.amazonaws.com fetches the same information without opening a browser. The generic form is:
curl <url>dig Query
The dig utility can query OpenDNS to resolve myip.opendns.com via the resolver resolver1.opendns.com:
dig +short myip.opendns.com @resolver1.opendns.comThis method sends a minimal DNS request, avoiding HTTP headers and TLS overhead, but it may be blocked or cached by some ISPs.
Robust Bash Script with Fallback Logic
The script below first tries the dig approach; if it fails, it falls back to a list of HTTP services accessed with curl -4s. Hosts are selected randomly, and the script repeats until a valid IPv4 address is obtained or the list is exhausted.
#!/bin/bash
# This script tries to obtain the current public IPv4 address using OpenDNS first,
# then falls back to several online services.
hosts=("checkip.amazonaws.com" "api.ipify.org" "ifconfig.me/ip" "icanhazip.com" "ipinfo.io/ip" "ipecho.net/plain" "checkipv4.dedyn.io")
CURL=$(which curl)
DIG=$(which dig)
# Try OpenDNS via dig
check=$($DIG +short myip.opendns.com @resolver1.opendns.com A)
if [ ! $? -eq 0 ] || [ -z "$check" ] || [[ ! $check =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Unable to get your public IP address by OpenDNS service, trying other methods."
count=${#hosts[@]}
while [ -z "$check" ] && [ $count -ne 0 ]; do
selectedhost=${hosts[$RANDOM % ${#hosts[@]}]}
check=$($CURL -4s https://$selectedhost | grep -v '^$')
if [[ $check =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
break
else
echo "The host $selectedhost returned an invalid IP address."
check=""
((count--))
fi
done
fi
if [ -z "$check" ]; then
echo "Unable to get your public IP address. Please check your internet connection."
exit 1
fi
echo "Your public IP address is $check"
exit 0The script uses curl -4s to force IPv4 and silence progress output. Random selection spreads the load across services, reducing the chance of being rate‑limited or banned.
Key Takeaways
Relying on a single external service is fragile; combining multiple providers with intelligent fallback logic ensures higher reliability for DDNS updates and other network‑dependent applications. Randomized selection also provides a simple form of load balancing, preventing excessive requests to any one endpoint.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
