How to Check Your Public IP Address Remotely Using SSH
Checking the public IP address of a remote server when you only have command-line access is a common task for network administrators and developers. You can quickly What Is My IP or use simple terminal commands to query external services and network interfaces over an active SSH session. This article explains the most reliable methods to discover your remote server's public IP address, whether you are managing Linux, FreeBSD, or macOS instances.
Preparing for Remote IP Discovery
Before running commands to identify your external address, it helps to understand why standard commands like ip addr or ifconfig often fall short. These local interface utilities typically display private IP addresses assigned by cloud providers, routers, or local area networks, rather than the public NAT address facing the internet.
To find the actual public-facing IP, your remote server needs to make an outbound request to an external service that echoes back the source IP address it receives. Alternatively, you can inspect outbound network routing tables or query authoritative DNS servers.
Method 1: Using Curl and Public IP Echo Services
The most straightforward way to check remote server public ip via SSH terminal is by using curl or wget to query lightweight IP echo APIs. These services return only the raw IP address as plain text, making them script-friendly and extremely fast.
Using Curl with Reliable APIs
Connect to your remote server via SSH using your standard credentials:
ssh user@example.com
Once logged in, run one of the following commands to retrieve your IPv4 address:
curl -4 ifconfig.me
curl -4 icanhazip.com
curl -4 api.ipify.org
Sample output:
192.0.2.45
If your network infrastructure supports IPv6 and you need to verify your public IPv6 address, simply omit the -4 flag or use the -6 flag depending on the provider:
curl -6 icanhazip.com
Sample output:
2001:db8:85a3::8a2e:0370:7334
Using Wget as an Alternative
If curl is not installed on a minimal remote server image, wget is frequently available by default:
Wget -qO- https://icanhazip.com
The -qO- flags tell wget to operate in quiet mode and output the retrieved content directly to standard output rather than saving it to a file.
Method 2: Querying DNS Providers via Dig or Nslookup
If your remote server security policy restricts outbound HTTP/HTTPS traffic to external web APIs, you can resolve your public IP address using DNS queries directed at major public resolvers.
Using Dig with OpenDNS
OpenDNS provides a special TXT record lookup service that returns the querying client's public IP address. Execute this command in your SSH terminal:
Dig @resolver4.opendns.com myip.opendns.com +short
Sample output:
"192.0.2.45"
Using Dig with Cloudflare
Cloudflare offers a similar DNS query mechanism using whoami.cloudflare combined with type TXT or CH (Chaos class) requests:
Dig +short txt ch whoami.cloudflare @1.1.1.1
Sample output:
"192.0.2.45"
Method 3: Inspecting Local Routing Tables
If you want to check remote server public ip via SSH terminal without querying external third-party services, you can inspect how your server routes default traffic out to the internet. This method works best if your primary network interface has a public IP directly bound to it.
Run the following command to see your default gateway:
Ip route show match 8.8.8.8
Sample output:
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.15
In this example, src 192.0.2.15 indicates the source IP address the kernel uses to communicate with the external internet. Note that if your server sits behind a Network Address Translation (NAT) firewall or a cloud provider's one-to-one NAT mapping (common in AWS EC2 or Google Cloud), this command will show the local private IP rather than the public address.
Comparison of IP Discovery Methods
| Method | Command Example | Best Use Case | Requires External Service | Firewall Dependency |
|---|---|---|---|---|
| HTTP Echo | curl icanhazip.com |
Standard Linux/macOS servers | Yes (HTTP/HTTPS) | Outbound port 80/443 |
| DNS Query | dig @1.1.1.1 ... |
Servers with strict HTTP blocks | Yes (DNS) | Outbound port 53 |
| Routing Table | ip route show match ... |
Direct public IP assignments | No | None |
Common Mistakes and How to Fix Them
When attempting to find your remote server's public address, several pitfalls frequently trip up administrators:
- Confusing Local and Public IPs: Running
ip addrorifconfigoften displays internal ranges like10.x.x.xor192.168.x.x. Always use an external echo service or DNS query if your server is behind a cloud VPC router. - SSL Certificate Verification Failures: When writing custom scripts that query HTTPS endpoints, expired system clocks can cause
curlto fail. Fix this by ensuring your Network Time Protocol (NTP) daemon is running correctly. - Firewall Blocking Outbound Requests: Corporate or strict cloud security groups might block outbound traffic on ports 80 and 443. If
curltimes out, switch to Method 2 and query DNS over port 53 usingdig.
Quick Checklist for Remote IP Verification
- Establish an active SSH connection to your remote target.
- Test basic internet connectivity using a reliable ping or traceroute command.
- Run
curl -4 icanhazip.comfor a fast HTTP-based lookup. - Fall back to
dig @1.1.1.1 +short txt ch whoami.cloudflareif HTTP ports are closed. - Verify whether the returned address matches your cloud provider console or DNS records.