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

Originally published at:

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 -qO -

Example output:

[root@BERTHA ~]# wget -qO -
1 Like

For that : open the terminal and type “curl

  1. dig +short
    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, always returns your IP address when request sent to opendns resolver 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 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?


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

dig +trace
dig +trace @
dig +trace @

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?


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


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

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

; EDNS: version: 0, flags:; udp: 4096
; IN A

;; AUTHORITY SECTION: 1469 IN SOA 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 any different than trusting ?