57

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
BuZain
  • 673
  • 1
  • 5
  • 7

5 Answers5

75

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

curl ipinfo.io/23.66.166.151
drs
  • 5,363
  • 9
  • 40
  • 69
zmonteca
  • 851
  • 1
  • 6
  • 3
  • 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 05 '15 at 15:48
  • Meh, doesn't allow hostnames... – m3nda Mar 11 '16 at 04:47
  • 1
    $ host yourdomain.com | cut -d ' ' -f 4 | curl ipinfo.io/$1 – zmonteca Mar 22 '16 at 15:57
  • 3
    ipinfo.io is free for 1000 requests/day (see their [pricing documentation](http://ipinfo.io/pricing)). – Matthieu Nov 14 '16 at 15:03
  • 5
    Another alternative: `curl ipinfo.io/$(dig +short yourdomain.com)` – John Red Dec 07 '16 at 17:00
  • @Matthieu I haven't hit 1000 limit but I'm planning to script it to check some IP addresses periodically during the day. Have you hit 1000 and blocked from doing more requests? –  Jan 07 '17 at 10:08
  • @sdkks I'm not using it in a script, only from time to time when I need it. I took the limit from their documentation. – Matthieu Jan 07 '17 at 11:47
  • 3
    If someone ever hits the ceiling on the ipinfo.io API (lucky you?), there is also https://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 09 '17 at 13:52
  • 10.000 requests/mo now for freegeoip.net – Imaskar Sep 20 '18 at 08:53
  • If you have 'jq' here's a fun one-liner :: `curl -qs http://ipinfo.io/$(curl -qs http://ipinfo.io/ip) | jq -r '. | "\(.city), \(.country)"';` Produces "Reykjavik, IS" for me. – Martin Bramwell Mar 29 '23 at 16:50
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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
10

Or use whois

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

Daniel
  • 111
  • 1
  • 2
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
slm
  • 363,520
  • 117
  • 767
  • 871
zinger
  • 91
  • 1
  • 2
  • [`geoipupdate`](https://dev.maxmind.com/geoip/geoipupdate/) is a MaxMind tool, you need a subscription and the licence key on `/etc/GeoIP.conf`. I couldn't use the [free alternative](https://dev.maxmind.com/geoip/geoipupdate/#For_Free_GeoLite2_Databases) (_Received an unexpected HTTP status code of 401_). – Pablo A Mar 09 '18 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.

chris
  • 103
  • 1
user478203
  • 11
  • 1
  • 1
    What database does it use under the hood? – HappyFace Jun 20 '21 at 12:34
  • It's using https://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 '21 at 18:05