Discover your IP from the command line


Many times I find myself needing to keep track of a host on a DHCP’d network where its IP address is subject to change. Here are a collection of command line methods for discovering your IP using both curl/http and dns lookups.

HTTP based lookups

curl icanhazip.com
curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s http://checkip.amazonaws.com/
curl -s http://wtfismyip.com/text
curl -s curl api.ipify.org

This one is pretty slow, but it sometimes works

curl ifconfig.me/ip

DNS based lookups. These are the best options since they’re not likely to be blocked by firewalls and, being UDP, have a low overhead.

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

As a bonus, here are two services for decorating an ip address or domain with additional information such as geolocation:

curl -s freegeoip.net/json/github.com
curl -s ipinfo.io/8.8.8.8

There are severe limitations to these services so take that into account when deciding what to include in your app.

The commands above were discovered here and here.

Leave a comment