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.
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements Linux, macOS, WSL or Unix-like OS
Time 1m
This page explain how to find out your own IP address using command line options on an Ubuntu, Debian, CentOS Linux, macOS/OS X and Unix bash shell.

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

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 user@your-router-ip-here
ssh admin@
[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:

## store output in $server_ip ##
## Display it ##
printf "Server public ip4 %s\n" $server_ip

Not a fan of CLI?

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

This entry is 2 of 2 in the Find IP Address Tutorial series. Keep reading the rest of the series:
  1. How to find Public IP address AWS EC2 or Lightsail VM
  2. Find My Public IP Address From Command Line On a Linux

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • Hardware Aug 8, 2015 @ 18:54

    It’s also possible with Google resolvers :

    dig TXT +short | sed 's/"//g'
    • 🐧 Vivek Gite Aug 8, 2015 @ 19:59

      Thanks for the command :)

    • Ragu Bhat Aug 13, 2015 @ 14:19

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

  • ayak Aug 8, 2015 @ 20:54

    easy peasy lemon squeezy version

  • Alex Aug 9, 2015 @ 1:57

    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.

  • Leaman Aug 9, 2015 @ 2:10

    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?

    • 🐧 Vivek Gite Aug 9, 2015 @ 4:01

      You may get some shell code or nasty stuff when you do ‘curl url/ip’ or ‘curl url/ip | cmd1 process’ or typical ‘curl -s | sh’ kind of stuff.

    • 0m3ga Dec 20, 2020 @ 14:18

      curl was always the quickest easiest way for me. But thanks for the others!

  • vicdeveloper Aug 9, 2015 @ 3:02

    Added to favorites.


  • Ronny Aug 9, 2015 @ 5:15


    • Bijit Sep 1, 2015 @ 9:04

      Nice! :)

  • Je Aug 12, 2015 @ 18:04

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

  • Klaus Aug 12, 2015 @ 18:37

    >>> dig +short <<< should do it too.

  • Yehuda Sep 7, 2015 @ 8:09
  • Lee Sep 16, 2015 @ 22:29

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

  • Andrea Zappaterra Dec 13, 2016 @ 15:24

    Almost in Windows (and linux):


  • Vikram Mar 24, 2017 @ 14:21

    externalip=$(wget --timeout=30 -qO -)
    echo $externalip

  • Steve Aug 25, 2020 @ 1:19

    I am curious, using the method in this post (not any of the ones in the comment), how often can you do this without getting blacklisted? I am using it to update my DDNS records, and currently it runs every 2 hours, but I would like to do it more often. Just don’t want to run the risk of getting blacklisted by some site.

    • 🐧 Vivek Gite Aug 25, 2020 @ 7:34

      Google or OpenDNS will never ban you as it is their main business. But, in rare cases if you are doing something illegal or your system/PC/router infected they might block you. As usual be a good netizen

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @