How to find the external ip address in Linux
Get Public Ip Address In Linux Terminal
First you need to know that there are two versions of IP addresses: IPv4 and IPv6. Each of these two types of IP addresses can be either private or public. In this article we will show several ways how you can check your outgoing ip address using the dig commands, host command, and some external services to find a public IP address on Linux VPS or dedicated server.
What Is An External Ip Address?
An external IP address is a UNIQUE IP address for the entire Internet, which is assigned to a server, virtual machine or computer. There are a limited number of such IP addresses in the world. External IP addresses belong to hosting providers and are allocated from a pool of addresses. Thanks to this IP address, you gain access to the outside world.
Advantages Of An External Ip Address
An external IP address is indispensable in hosting services. A public IP address allows your server or computer to be used to access websites, email servers, or other other resources. You can use such an IP address for remote access to devices and networks, which is very convenient and important when working remotely. You can also use a public IP address to set up protection using a firewall and other services. You can read more about protecting your server in our article - secure linux server.
Disadvantage Of Public Ip Addresses
Despite the fact that a public IP has a number of advantages, there is also the disadvantage that it is not sufficiently protected. Your public IP is viewable and you may be at risk of hacking your server or computer. You need to take action yourself to protect your server from hacking.
Linux: Get Public Ip Address From The Linux Command Line
Check External Ip Address Using Command Curl:
curl ifconfig.me
curl icanhazip.com

Check Public Ip Address Using Command Wget:
wget -q -O - ifconfig.me
wget -q -O - icanhazip.com

Linux: Get External Ip Address Using DynDNS.org
1. You can get it using utilite wget:
wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
2. Using utilite curl:
curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
3. Using utilite lynx:
lynx -dump checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//' | sed '/^$/d'

Additional Methods To Find Your Public Or External Ip In Linux
In some environments, basic curl or wget commands may fail or return incomplete results. When that happens, you may need alternative ways to find external IP in Linux without relying on standard HTTP endpoints. Below are several practical methods that work on different Linux distributions and hosting setups.
Check External Ip Using Dig (Dns Lookup Method)
DNS-based lookups often work even when outbound web requests are restricted.
Google DNS:
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
OpenDNS:
dig +short myip.opendns.com @resolver1.opendns.com
If you need a linux command to check public IP that does not depend on third-party services, dig is usually the most reliable option.
Check Public Ip Using Host
host myip.opendns.com resolver1.opendns.com
This method is helpful on minimal Linux installations where dig is not available.
Check Public Ip Using Nslookup
nslookup myip.opendns.com resolver1.opendns.com
Useful on older systems or stripped-down images.
Getting Public Ip From Cloud Metadata
When running a Linux VPS or dedicated server in the cloud, the most accurate way to retrieve the external IP is through provider-specific metadata endpoints.
These commands do not rely on DNS or external lookup services — they return the actual public IP assigned to the VM.
AWS EC2
curl http://169.254.169.254/latest/meta-data/public-ipv4
This is the most direct command to get external IP in Linux on an EC2 instance.
Google Cloud (GCP)
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip
http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip
Microsoft Azure
curl -H Metadata:true \
"http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2021-02-01"
DigitalOcean
curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address
Check Ipv6 Public Address In Linux
If your server is IPv6-enabled, you can retrieve the external IPv6 address with:
curl -6 ifconfig.co
This is the simplest way to check IPv6 public address in Linux.
Why The Public Or External Ip May Differ (Troubleshooting)
If different commands return different IP addresses, it’s usually caused by one of the following:
● NAT or shared gateway — your machine’s traffic is routed through another IP.
● VPN or proxy — commands show the exit IP, not the server’s original address.
● Containers (Docker, LXC) — you may see the bridge interface instead of the host.
● Cloud networks — private IPs and public NAT IPs often differ.
● Firewall or outbound filtering — curl/wget are blocked or redirected.
In such cases, it’s better to check external IP from command line using DNS lookups or metadata queries.
Conclusion
The main purpose of this article is to show you several ways to find out your public IP address. We also tried to briefly explain what a public IP address is, its main advantages and disadvantages. We hope you found this article helpful.
FAQ
You ask, and we answer! Here are the most frequently asked questions!
-
Why do different tools show different IPs?
- Each method uses a different resolver or endpoint. Metadata is the most accurate.
-
Why do I get an IPv6 address instead of IPv4?
- Your system prefers IPv6. Use curl -4 ifconfig.me to force IPv4.
-
Why does my public IP change?
- Dynamic IP pools, NAT setups, or provider policies can trigger IP rotation.