There are plenty websites that show you your public IP address when you visit them. The simplest way would be to just ask google. These sites get the job done but when you want to constantly monitor your public ip it becomes tedious to keep visiting them. You need to have a browser running and the website itself takes 3-4 seconds to load.

There are some handy api services like ipify API. If you visit you'll get a plain text http response. This method is slightly faster than asking google due to the smaller response size. It's also handy as you can use it in creating scripts.

ipify Ipify Api Service

But there's a faster method. Much much faster! It's crazy how fast it is! It takes no more than a 100-200 milliseconds. This method involves quering a DNS server rather than an HTTP server.

I use the dig command to make DNS queries. Dig is a DNS lookup utility that comes preinstalled in most linux distros.

You can use dig to find the ip address of a website like this.

# This will spill out a bunch of metadata

# This will simply return the ip address
dig +short

But we're here to find the ip address of our router and not some website's. In order to get the ip address of our router, we need to query a special url to the DNS resolver

This DNS resolver is programmed to return the requesters ip address when a request is made to that special url. If you simply query with any other DNS resolvers then you'll get NXDOMAIN response; which basically means that the domain does not exist.

# To know you public ipv4 address
dig +short -4

# To know you public ipv6 address
dig +short AAAA

Besides OpenDNS, Google and Akamai also provide a similar feature

dig ANY +short

dig TXT +short

Akamai's service was a bit slow for me.


I have added an alias to my zshrc file so that I can query my public ip address from the terminal by simply typing publicip.

alias publicip='dig +short -4'


# Polybar module

type = custom/script
exec = dig A +short -4
interval = 100
label =  %output%