nstall geoiplookup on Debian, Ubuntu or Linux Mint:
$ sudo apt-get install geoip-bin
To install geoiplookup on Fedora:
$ sudo yum install geoip
To install geoiplookup on CentOS, first enable EPEL repository
$ sudo yum install epel-release
then use yum command:
$ sudo yum install geoip
The default installation of geoiplookup comes with GeoIP.dat database file which is located in /usr/share/GeoIP. With this database, you can look up the country information only.
$ geoiplookup 23.66.166.151 GeoIP Country Edition: US, United States
You can download additional GeoIP databases from MaxMind, which give you more detailed information about IP addresses beyond country info. You can also download more up-to-date GeoIP.dat from the site. This is recommended because GeoIP.dat may have already been outdated by the time you install it from Linux repositories. The GeoIP databases available on MaxMind website are updated every month.
To install additional GeoIP databases from MaxMind, do the following. You may want to set up a monthly cronjob to automate this process.
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz $ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz $ wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz $ gunzip GeoIP.dat.gz $ gunzip GeoIPASNum.dat.gz $ gunzip GeoLiteCity.dat.gz $ sudo cp GeoIP.dat GeoIPASNum.dat GeoLiteCity.dat /usr/share/GeoIP/
Now if you re-run geoiplookup, you will see the additional AS number information of an IP address. This basically tells you which administrative domain the IP address belongs to.
$ geoiplookup 128.112.119.209 GeoIP Country Edition: US, United States GeoIP ASNum Edition: AS88 Princeton University
When run without any parameter, geoiplookup tool automatically uses GeoIP.dat and GeoIPASNum.dat only, but not use GeoLiteCity.dat. The latter can give you city-level information.
To obtain city-level geolocation information, explicitly tell geoiplookup to use GeoLiteCity.dat database.
$ geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 23.66.166.151 GeoIP City Edition, Rev 1: US, MA, Cambridge, 02142, 42.362598, -71.084297, 506, 617
The output includes state, city, zipcode, latitude and longitude. The accuracy of the inferred location varies across different countries and networks. For example, the geolocation result tends to be more accurate for broadband IP addresses, but not as accurate for mobile networks.
You can try ipinfo.io online service. Unlike other services, ipinfo.io provides JSON-based geolocation API, so you can easily look up geolocation from the command line, using tools like curl.
$ curl ipinfo.io/23.66.166.151