Display Public IP Address using Command-Line in Linux

There are two ways to retrieve external/public IP addresses for your Linux server or system. The fastest method is by resolving the DNS (dig, host), and the other is by retrieving server data through the HTTP protocol (cURL, wget).

Local and Public are two variants assigned to different networks. Local IP addresses are assigned to laptops, mobile phones, and other devices connected to the same network for local network communication.

The ISP provider assigns public IP addresses to your local network for external communication over the internet, and they can be static or dynamic depending upon the connection.

Dynamic addresses are for general use and updated every time you connect to the internet, while static addresses remain the same until manually changed.

Today, you will learn how to display external or public IP addresses using OpenDNS and External Services.

Display External/Public IP Address using OpenDNS

in dig Command

The dig command is a valuable network administration tool for troubleshooting and querying the Domain Name System (DNS).

The other examples here go over the HTTP protocol to the server and respond in a different format or rely on the user agent to make the server respond in plain text, making the process slower.

However, DNS (OpenDNS, Google DNS, etc.) response protocol is the fastest, most standardized, and stays compatible for longer.

You can use dig with OpenDNS as a resolver to display the external/public IP address for your server or local Linux system.

$ dig +short myip.opendns.com @resolver1.opendns.com
##.###.###.###

in host Command

The host command is a simple utility for DNS lookup, and it typically looks up the domain name for an IP address and vice versa.

The below host command with OpenDNS looks for your external/public IP address.

$ host myip.opendns.com resolver1.opendns.com | awk '/address / {print $4}'
##.###.###.###

Display External/Public IP Address using External Services

External services are easy to use and require going over the server using the HTTP protocol and parsing the output data in plain text or reading the user-agent header.

This slows down the results and is less stable than the DNS lookup. For these reasons, the above-mentioned methods are recommended over this one.

in cURL Command

The curl command transfers data from or to a server and supports various protocols such as FTP, FTPS, HTTP, IMAP, POP, etc.

The below commands are currently the most stable to display your external/public IP address for the server or Linux system.

$ curl ipinfo.io
##.###.###.###
$ curl ifconfig.co
##.###.###.###
$ curl ifconfig.me
##.###.###.###
$ curl https://checkip.amazonaws.com
##.###.###.###

in Wget Command

The wget command is a non-interactive utility to download files from the internet. It supports HTTP, HTTPS, and FTP protocols and retrieval through HTTP proxies.

The below commands can help you display your Linux system’s current external or public IP address.

$ wget -qO- https://ipecho.net/plain; echo
##.###.###.###
$ wget -qO - icanhazip.com
##.###.###.###

TL;DR

The fastest way to display your Linux system’s or server’s external/public IP address:

$ dig +short myip.opendns.com @resolver1.opendns.com
OR
$ host myip.opendns.com resolver1.opendns.com | awk '/address / {print $4}'

The easiest way to display your Linux system’s or server’s external/public IP address:

$ curl ifconfig.me
OR
$ curl https://checkip.amazonaws.com
OR
$ wget -qO - icanhazip.com

If you know more command-line methods to display the public IP address in Linux, please write them in the comment section.

Leave a Reply