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 -s '' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s
curl -s
curl -s curl

This one is pretty slow, but it sometimes works


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
dig TXT +short

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

curl -s
curl -s

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.