Find out my public IP on the terminal

Often it is important to find out, what is my machine's public IP address. There are numerous websites to get this information, but it is much faster to do on the terminal.

We can do this by querying Google's name servers with a specific query o-o.myaddr.l.google.com using dig. This will send back my IP as a response.

I want to query both IPv4 and IPv6 addresses, so I run the query twice with different parameter to dig.

As per usual, I ended up creating a new alias to my .zshrc.

alias myip='echo "IPv4:" && dig -4 @ns1.google.com TXT o-o.myaddr.l.google.com +short && echo "IPv6:" && dig -6 @ns1.google.com TXT o-o.myaddr.l.google.com +short'

Now I can just run myip on the terminal to get my public IPs.

> myip

IPv4:
"ipv4-address-here"
IPv6:
"ipv6-address-here"

This article is inspired by another article by someone, but I lost the link :(