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

last updated in Categories , , , ,

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.[donotprint][/donotprint] 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 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:

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Notable Replies

    1. dig +short myip.opendns.com @resolver1.opendns.com
      Does anyone care to elaborate on whats exactly happening in this? I guessing myip is being passed as a host name? to opendns and its resolver?

    appreciate the lesson.

  1. yes, myip.opendns.com always returns your IP address when request sent to opendns resolver resolver1.opendns.com. myip.opendns.com will always have 0 ttl, that way it can not cached by any dns client and provides visitors true IP address using nothing but standard dns command line.

  2. I don’t think so it matters. dig ignores whatever configured in /etc/resolv.conf when you ask for specific dns resolver like @resolver1.opendns.com. Verify with +trace flag:

    dig +trace www.google.com @resolver1.opendns.com
    dig +trace www.google.com @1.1.1.1
    dig +trace www.nixcraft.com @8.8.8.8
    

    It is possible that firewall might block requests to certain or all DNS server except for home router. In that case, look into firewall logs. Are you running dig on your Linux/Unix desktop or router itself?

  3. Hm, I can vpn out to work connection and the dig +short works right away. Without it I get nothing with +short and without the short it will return:
    ?2 ? dig myip.opendns.com @resolver1.opendns.com

    ; <<>> DiG 9.10.6 <<>> myip.opendns.com @resolver1.opendns.com
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 29358
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 4096
    ;; QUESTION SECTION:
    ;myip.opendns.com. IN A

    ;; AUTHORITY SECTION:
    opendns.com. 1469 IN SOA auth1.opendns.com. noc.opendns.com. 1550803378 16384 2048 1048576 2560

    ;; Query time: 36 msec
    ;; SERVER: 2620:119:35::35#53(2620:119:35::35)
    ;; WHEN: Thu Feb 21 21:51:53 EST 2019
    ;; MSG SIZE rcvd: 91

Continue the discussion www.nixcraft.com

4 more replies

Participants

Historical Comment Archive

27 comment

  1. It’s also possible with Google resolvers :

    dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | 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
    $_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.

  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 icanhazip.com but we should trust the biggest data vacuum of them all, Google?

    Still, have a question? Get help on our forum!