How To Find My Public IP Address From Command Line On a Linux

Posted on in Categories , , , , last updated August 10, 2015

How do I find out my public IP address on the Linux and OS X Unix command line to use with my own bash shell script without using third party web site? Is there command-line option which will show my dynamic IP address on a Ubuntu or Fedora Linux?

There are many ways to find out your public IP address or wan (Wide Area Network) IP on a Linux or Unix-like operating systems such as FreeBSD, OpenBSD, NetBSD, Apple OS X, and others.

Explain IP addresses

An IP is short for Internet Protocol. It is used to identify computers or mobile devices on the Internet. Each device connected to the Internet has an IP address. IP address can be used to personalize information.

Use dig command for determining my public IP address:

  1. Open the Terminal application.
  2. Type the following dig (domain information groper) command on a Linux, OS X, or Unix-like operating systems to see your own public IP address assigned by the ISP:
  3. dig +short
  4. Or dig TXT +short
  5. You should see your IP address on screen. This is the fastest way to find out your IP address without using 3rd party site.

Sample outputs:

Fig.01: Use dig command to find your IP address
Fig.01: Use dig command to find your IP address

You can try host command to see the same information:
You can also use the Google server to get the same info using dig command:

dig TXT +short | awk -F'"' '{ print $2}'

How do I store my IP address in a shell variable?

The syntax is:

myip="$(dig +short"
echo "My WAN/Public IP address: ${myip}"

Sample outputs:

My WAN/Public IP address:

Finding Public/WAN IP address on a router

A few ADSL/Cable router allows you to login to your router using telnet or ssh:

telnet your-router-ip-here
ssh [email protected]
ssh [email protected]
[[email protected] ~]#  ifconfig eth1 | grep 'inet addr:' 
[[email protected] ~]#  ip addr show nas01

Use 3rd party web-sites to get your IP

Please note that I do not recommend following curl/wget method due to security reasons. You have been warned:


Not a fan of CLI?

Use your own web-browser. Visit the following urls to see your WAN IP:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 18 comments/add one below):

18 comment

  1. It’s also possible with Google resolvers :

    dig TXT +short | sed 's/"//g'
    1. @Hardware, Your dig to the Google Public nameservers is faster in returning MYADDR. Tnx for your comment.

  2. You can also set up a simple website in PHP and use

    This would give you at least some assurance of security, because no other / no one else’s code is used on that page.

    Plus, it allows for the output that you might use in scripts, instead of using brutal regexes. See the $_SERVER php examples for other interesting options.

  3. Vivek, thanks for this. Very interesting, and I see lot of useful possibilities for scripts.

    What are the security concerns of doing a “curl” on the sites you mention? How is it any more insecure than visiting the site with a web browser?

  4. Wait… For security reasons, we should not trust but we should trust the biggest data vacuum of them all, Google?

Comments are closed.