Determining Public IP with DNS

Almost everyone knows of a site (or two) that allows you to easily retrieve your public IP address. These are often hosted on various providers and tend to disappear given enough time.

One of the simplest methods I’ve found around this is to ask Google instead. With a simple my ip in any search bar, you’ll get your public address quickly and reliably.

You can, however, also use DNS to query Google in the same way:

dig @ns1.google.com TXT o-o.myaddr.l.google.com +short

This returns your actual IP address. Notice how it’s a TXT record instead of an A/AAAA? This prevents you from pretending you’re Google, but I’m curious if there are other reasons to return this as a TXT specifically.

Also of note is that the @ns1.google.com argument is required. This directs dig to speak directly to Google’s nameservers. If you don’t add this, you will get the address of your upstream provider’s DNS server that proxies the authoritative server’s request, which can also be helpful but not accurate if you want your address.