How to find the public IP address from command line

Last updated on August 20, 2020 by Dan Nanni

Suppose your Linux machine is behind a NAT router. Then what is visible to your machine is only a private IP address (e.g., 192.168.x.x, 10.x.x.x, 172.16.x.x) 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 on Linux.

Prerequisite: curl

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

On Debian, Ubuntu or Linux Mint:

$ sudo apt-get install curl

On Fedora, CentOS or RHEL:

$ sudo yum install curl

Find Your Public IP Address with curl

The following shows a list of commands that you can run to determine the public IP address. The output of any of these commands will be your public IP address. You can define an alias using one of these commands, or use one directly in a shell script.

$ curl ifconfig.me
$ curl icanhazip.com
$ curl ipecho.net/plain
$ curl whatismyip.akamai.com
$ curl myip.dnsomatic.com
$ curl -s checkip.dyndns.org | sed -e '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.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean