How to find the public IP address from command line

Suppose your Linux machine is behind a NAT router. Then what's visible to your machine is only a LAN IP address assigned to its LAN interface. Then how can you detect the public (or WAN) IP address assigned to the NAT router, preferably from command line?

Here is how to find the public IP address from command line.

You first need to install curl as you need to rely on an external web server to get the public IP address.

On Debian, Ubuntu or Linux Mint:

$ sudo apt-get install curl

On Fedora, CentOS or RHEL:

$ sudo yum install curl

The following is a list of commands that you can run to determine the public IP address. You can define an alias using one of these commands, or use them in a shell script.

$ curl ifconfig.me
$ curl icanhazip.com
$ curl ident.me
$ curl ipecho.net/plain
$ curl whatismyip.akamai.com
$ curl tnx.nl/ip
$ curl myip.dnsomatic.com
$ curl ip.appspot.com
$ curl ip.telize.com
$ curl curlmyip.com
$ curl -s checkip.dyndns.org | sed 's/.*IP Address: \([0-9\.]*\).*/\1/g'

If there is any command in the above that no longer works, or any new site that is command-line friendly, let me know.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.
Your name can also be listed here. Write for us as a freelancer.

6 thoughts on “How to find the public IP address from command line

  1. @ biOos,

    Due to security reason all of them disabled telnet. Still if you use it, I advise you to disable it to avoid any further damage on your server.

  2. @Magesh it is using the telnet port (23) to return your IPv4 address. It is not using the telnet protocol. You can observe the same behavior with nc(1) "nc v4address.com 23". I don't see an issue.

  3. A VPN I've used - BolehVPN - says many "what's my IP" services collect system fingerprint type information for agencies of various sorts. It recommends using myip.dk to get your current external IP. The implication (but not outright stated) is that myip.dk does not collect any user information. For Dan - do you know anything about myip.dk? and - since I only know how to screw things up with awk and sed - how could one extract only the IP info from this web page for use in a terminal?

  4. Dan - update to my comment. From the faq at icanhazip.com -
    "What about my privacy?
    I do keep the logs from the web server around to ensure that the service isn’t being abused. However, no data is stored in a database or provided to third parties. I may pull some general statistics from the logs from time to time about the countries where the site is the most popular, but there will never be anything released on a granular level."
    https://major.io/icanhazip-com-faq/

Leave a comment

Your email address will not be published. Required fields are marked *