How to Get Your Public IP Address in the Linux Terminal (cURL, Wget, & DNS)
Quick Summary: To quickly find your public IP address using the Linux command line, open your terminal and run curl ifconfig.me or curl icanhazip.com. If you only have wget installed, run wget -qO- ifconfig.me. For environments without HTTP access, perform a direct DNS lookup using dig @resolver4.opendns.com myip.opendns.com +short.
Introduction
When working on a remote Linux server, configuring a firewall, or setting up a Virtual Private Cloud (VPC), knowing how to quickly determine your machine's public IP address from the command-line interface (CLI) is essential.
While checking your internal IP address is straightforward with commands like ip addr or ifconfig, these tools only reveal your local interface addresses (such as 192.168.x.x or 10.x.x.x). If your machine is behind a router, Network Address Translation (NAT) device, or cloud load balancer, your internal interface has no visibility into the public IP address exposed to the rest of the internet.
This guide provides practical, production-ready methods to check your public IPv4 and IPv6 addresses directly from the Linux terminal using curl, wget, native DNS utilities, and custom Bash automation scripts.
Method 1: Retrieve Public IP Using curl
The curl command is the standard utility for transferring data over HTTP and HTTPS protocols. Several public diagnostic services offer plain-text endpoints designed specifically for CLI consumption.
1. Standard cURL Query
Run any of the following commands in your terminal to output your public IP address cleanly without extra HTML formatting:
curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain
2. Force IPv4 or IPv6 Address Output
If your system is dual-stacked (supporting both IPv4 and IPv6 addresses simultaneously), standard HTTP requests may default to IPv6. You can explicitly force curl to return a specific IP version using command-line flags:
curl -4 ifconfig.me
curl -6 icanhazip.com
Note: If you want to view your complete network profile, ISP provider details, and geographical location without using terminal commands, you can instantly test your connection using our web-based public IP address checker.
Method 2: Retrieve Public IP Using wget
On lightweight Linux distributions or minimal container environments (such as Docker Alpine base images), curl might not be pre-installed. You can achieve the exact same output using wget.
1. Silent Wget Query
By default, wget writes output to a file and logs connection progress. Use the -q (quiet) and -O- (output to standard out) flags to print your public IP directly to the terminal screen:
wget -qO- ifconfig.me
wget -qO- icanhazip.com
2. Force IPv4 or IPv6 via Wget
Similar to curl, you can restrict wget to specific address families using explicit command flags:
wget -4 -qO- ifconfig.me
wget -6 -qO- icanhazip.com
Method 3: Retrieve Public IP Using DNS Utilities (dig & nslookup)
In hardened server environments, outbound HTTP/HTTPS connections (ports 80 and 443) may be restricted by strict firewall egress rules. However, DNS queries on UDP port 53 are often permitted.
You can query public DNS resolvers—such as OpenDNS or Google Public DNS—to report back the source IP address of your DNS request.
1. Using dig (Domain Information Groper)
Execute a targeted TXT query against OpenDNS resolvers:
dig +short myip.opendns.com @resolver1.opendns.com
dig +short txt ch whoami.cloudflare @1.1.1.1
2. Using nslookup
If dig is not available on your system, nslookup provides a reliable alternative:
nslookup myip.opendns.com resolver1.opendns.com | grep "Address:" | tail -n1 | awk '{print $2}'
Automating Public IP Retrieval in Bash Scripts
If you write deployment scripts or shell automation, you should avoid relying on a single external endpoint. If that specific endpoint experiences downtime, your script will fail or hang indefinitely.
Here is a resilient Bash script function that uses a fallback mechanism across multiple IP checking endpoints with strict timeout constraints:
#!/bin/bash
# Function to safely fetch public IP address with fallbacks
get_public_ip() {
local ip=""
# Attempt 1: ifconfig.me
ip=$(curl -s --connect-timeout 2 ifconfig.me)
# Attempt 2: icanhazip.com if first attempt failed
if [[ -z "$ip" ]]; then
ip=$(curl -s --connect-timeout 2 icanhazip.com)
fi
# Attempt 3: DNS lookup fallback
if [[ -z "$ip" ]]; then
ip=$(dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null)
fi
# Output result
if [[ -n "$ip" ]]; then
echo "Public IP: $ip"
else
echo "Error: Unable to resolve public IP address." >&2
return 1
fi
}
# Run the function
get_public_ip
Frequently Asked Questions (FAQ)
What is the difference between my local IP and public IP address?
Your local IP address (e.g., 192.168.1.50) is assigned by your local network router and is only accessible inside your private home or office network. Your public IP address is assigned by your Internet Service Provider (ISP) and represents your router or server across the entire public internet.
Why do different terminal commands return IPv4 vs IPv6 addresses?
If your Internet Service Provider and network interface support IPv6, commands without specific flags (like curl ifconfig.me) will automatically prefer IPv6 paths. Use the -4 or -6 flags to specify which IP format you want to retrieve.
Can I verify if my public IP address is behind a proxy or VPN?
Yes. If you are connected to a Virtual Private Network or proxy server, terminal IP queries will return the public egress IP of the remote VPN server rather than your physical connection's IP address. You can verify your connection's security and inspect active network details using our comprehensive IP geolocation lookup tool.