Sometimes, you just need to know where you are. IRL you can check a map or a GPS if you’re lucky. But in the world of computers and networks, you often need to know the IP address of your network as it appears to the outside world. One of my favorite linux commands to run is:
dig +short myip.opendns.com @resolver1.opendns.com
Let’s break that down:
- dig is a DNS lookup utility, and it stands for “domain information grouper”.*
- +short asks for a non-verbose answer.
- myip.opendns.com is the domain we ask about.
- @resolver1.opendns.com is the DNS server that we ask.
So what this command does is: it sends an request for the IP of the domain
myip.opendns.com to the DNS server
resolver1.opendns.com. This server is programmed so that if this special domain is requested, the IP the request comes from is sent back.
I often end up wrapping that line in a shell script and make a file called get_external_ip.sh that looks like:
#!/bin/sh dig +short myip.opendns.com @resolver1.opendns.com
Now whenever I need to know where this box is relative to the rest of the Internet, I can quickly run
./get_external_ip.sh and see the WAN IP immediately. Neato! 😎
* If you can’t
dig, try to
sudo apt-get install dnsutils or your equivalent.