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

Originally published at: https://www.cyberciti.biz/faq/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?

I use this command:

wget http://ipinfo.io/ip -qO -

Example output:

[root@BERTHA ~]# wget http://ipinfo.io/ip -qO -
xx.152.198.xxx
1 Like

For that : open the terminal and type “curl ifconfig.me

  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.

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.

1 Like

Interesting discovery, I have my home router set to use opendns, so the command was returning nothing. So by dropping the @resolver1.opendns.com it does. I suspected that it was having trouble since it was already set to use those dns servers. Care to elaborate on what i saw?

Thanks

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?

HI

On macOS laptop, the home router has the opendns dns addresses set.

Thanks

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

how is trusting opendns.com any different than trusting ifconfig.me ?