39

Is there a console command that takes an IP address as an input and shows its geographical information like city, country, ISP, etc.?

27

The command is the easy part, the difficult part is having access to a database.

For example, Ubuntu has a free database with a command line query tool (geoiplookup) in the geoip-bin Install geoip-bin package. But it only shows country information, and uses a static (hence out-of-date) database. This tool can also query the MaxMind GeoIP database, if you have a subscription there.

There are various GeoIP databases that you can look up. They're generally meant to be viewed through a web browser, but you can look for a scraping script. For example, here's a ruby script to retrieve data from the MaxMind database. Note that scraping may be against the database's terms of service.

  • 1
    just used the ubuntu geoiplookup and it is good enough. – BuZain Feb 15 '11 at 17:57
  • CentOS# yum install GeoIP – zmonteca Jul 1 '14 at 22:50
  • @StephenRauch Thanks but please use the official replacement for the broken bit.ly link: hostmar.co/software-small – Gilles 'SO- stop being evil' Apr 3 '17 at 0:23
  • 1
    @StephenRauch The apt link only works with a browser plug-in that's part of the default Ubuntu installation but rarely installed otherwise. – Gilles 'SO- stop being evil' Apr 3 '17 at 0:29
  • These are in Homebrew for Mac as well: geoipupdate; geoiplookup ... – Pysis Nov 25 '18 at 17:06
59

This trick is even nicer and doesn't require any external packages:

curl ipinfo.io/23.66.166.151
  • 2
    In case ipinfo.io gets offline someday, ifconfig.me provides a similar service (plus a REST-like API). But ifonfo.io seems much faster. – cedbeu Mar 5 '15 at 15:48
  • 1
    $ host yourdomain.com | cut -d ' ' -f 4 | curl ipinfo.io/$1 – zmonteca Mar 22 '16 at 15:57
  • 2
    ipinfo.io is free for 1000 requests/day (see their pricing documentation). – Matthieu Nov 14 '16 at 15:03
  • 4
    Another alternative: curl ipinfo.io/$(dig +short yourdomain.com) – John Red Dec 7 '16 at 17:00
  • 2
    If someone ever hits the ceiling on the ipinfo.io API (lucky you?), there is also freegeoip.net which has a mind-blowing 15,000 requests per hour limit. (Which under the hood seems to be running the geoiplookup from the other answers) – samthecodingman Mar 9 '17 at 13:52
8

Here's another great option. Instructions here: http://kbeezie.com/geoiplookup-command-line/

For example, on Centos:

$ sudo yum install GeoIP GeoIP-data
$ geoiplookup 8.8.4.4

Works perfectly.

And of course, you can always set this up as a cron:

$ /usr/bin/geoipupdate
  • geoipupdate is a MaxMind tool, you need a subscription and the licence key on /etc/GeoIP.conf. I couldn't use the free alternative (Received an unexpected HTTP status code of 401). – Pablo A Mar 9 '18 at 22:47
-1

Or use whois

$ whois 8.8.8.8 |grep country -i -m 1 |cut -d ':' -f 2 |xargs US

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.