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:
On Fedora, CentOS or RHEL:
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 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.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!