≡ Menu

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

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 myip.opendns.com @resolver1.opendns.com
  4. Or dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
  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:
host myip.opendns.com resolver1.opendns.com
You can also use the Google server to get the same info using dig command:

dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{ print $2}'

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

The syntax is:

myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
echo "My WAN/Public IP address: ${myip}"

Sample outputs:

My WAN/Public IP address: 74.86.144.194

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 user@your-router-ip-here
telnet 192.168.0.254
ssh admin@192.168.1.254
[admin@dd-wrt ~]#  ifconfig eth1 | grep 'inet addr:' 
[admin@dd-wrt ~]#  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:

curl ifconfig.me
curl icanhazip.com
curl ipecho.net/plain
curl ifconfig.co

Not a fan of CLI?

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

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 17 comments… add one }
  • Hardware August 8, 2015, 6:54 pm

    It’s also possible with Google resolvers :

    dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/"//g'
    • Vivek Gite August 8, 2015, 7:59 pm

      Thanks for the command :)

    • Ragu Bhat August 13, 2015, 2:19 pm

      @Hardware, Your dig to the Google Public nameservers is faster in returning MYADDR. Tnx for your comment.

  • ayak August 8, 2015, 8:54 pm

    easy peasy lemon squeezy version
    curl ifconfig.me

  • Alex August 9, 2015, 1:57 am

    You can also set up a simple website in PHP and use
    $_SERVER[‘REMOTE_ADDR’]

    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.

  • Leaman August 9, 2015, 2:10 am

    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?

  • vicdeveloper August 9, 2015, 3:02 am

    Added to favorites.

    Thanks!

  • Ronny August 9, 2015, 5:15 am

    curl curlmyip.com

    • Bijit September 1, 2015, 9:04 am

      Nice! :)

  • Je August 12, 2015, 6:04 pm

    Nice! Added as an alias to my cygwin setup for easy access. Thanks.

  • Klaus August 12, 2015, 6:37 pm

    >>> dig myip.opendns.com +short <<< should do it too.

  • Yehuda September 7, 2015, 8:09 am
  • Lee September 16, 2015, 10:29 pm

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

  • MW A March 28, 2016, 8:05 pm

    I like this website which is very accurate:

    http://www.whatismyip.global/

    The website even keeps a history of your IPs.

  • Aditya May 15, 2016, 5:47 am

    To Find IP Address This site will help you
    http://myiptools.net

  • Andrea Zappaterra December 13, 2016, 3:24 pm

    Almost in Windows (and linux):
    nslookup myip.opendns.com resolver1.opendns.com

    Easy!
    Andrea

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: