58

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

5 Answers 5

76

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

curl ipinfo.io/23.66.166.151
10
  • 3
    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, 2015 at 15:48
  • 1
    $ host yourdomain.com | cut -d ' ' -f 4 | curl ipinfo.io/$1
    – zmonteca
    Mar 22, 2016 at 15:57
  • 3
    ipinfo.io is free for 1000 requests/day (see their pricing documentation).
    – Matthieu
    Nov 14, 2016 at 15:03
  • 5
    Another alternative: curl ipinfo.io/$(dig +short yourdomain.com)
    – John Red
    Dec 7, 2016 at 17:00
  • 3
    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) Mar 9, 2017 at 13:52
30

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.

6
  • 1
    just used the ubuntu geoiplookup and it is good enough.
    – BuZain
    Feb 15, 2011 at 17:57
  • CentOS# yum install GeoIP
    – zmonteca
    Jul 1, 2014 at 22:50
  • @StephenRauch Thanks but please use the official replacement for the broken bit.ly link: hostmar.co/software-small Apr 3, 2017 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. Apr 3, 2017 at 0:29
  • These are in Homebrew for Mac as well: geoipupdate; geoiplookup ...
    – Pysis
    Nov 25, 2018 at 17:06
10

Or use whois

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

9

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
1
  • 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, 2018 at 22:47
1

Here is my little command line tool called "gip":

https://github.com/softhub-software-development/gip

# gip -h

usage: gip [-C] [-c] [-g] [-h] [-tT] [ip-address-or-domain ...]
gip invoked with no arguments starts the gip server
-C print country
-c print city
-g print geo coordinates only
-d print domain info if possible
-h print this help information
-t trace route omitting duplicates
-T trace route

In server-mode it renders a map with pin heads indicating the origin of requests.

2
  • 1
    What database does it use under the hood?
    – HappyFace
    Jun 20, 2021 at 12:34
  • It's using lite.ip2location.com/ip2location-lite (DB11·LITE to be precise) but includes only a few ip ranges for testing in the repository. You need to download the .csv yourself and generate the dataset which takes about 10 minutes on a raspberry pi.
    – chris
    Jul 18, 2021 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .