Method One
The first approach is to use the Geoiplookup tool, which is a command-line client for querying the Maxmind GeoIP database. Geoiplookup allows you to query for geographic information or network information for an IP address (or domain name). You can install it with the following command (it has a free GeoIP database).
Install Geoiplookup on Debian,ubuntu or Linux mint:
sudo apt-get install Geoip-bin
Install on Fedora:
sudo yum install GeoIP
To install on CentOS, you need to first open the Epel source and then use the Yum command:
sudo yum install GeoIP
The Geoiplookup and GeoIP.dat database files installed by default are located in/usr/share/geoip. This database can only be queried for country information.
Geoiplookup 23.66.166.151
GeoIP Country Edition:us, United States
You can download the additional GeoIP database from Maxmind, which will tell you more about the information than the country. You can also download many of the latest GeoIP.dat databases from this website. I recommend that you do this because the GeoIP.dat installed from the Linux source may be out of date. The GeoIP database on Maxmind is updated once a month.
To install the GeoIP database from the Maxmind, follow these methods. You may want to add a cronjob (crontab task, crontab, which is used in Unix and Unix-like systems to automate the process of periodic tasks).
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 performing geoiplookup, you will see additional autonomous system number (AS) information. This will give you a general idea of which admin domain the IP address belongs to (administrative domains).
Geoiplookup 128.112.119.209
GeoIP Country Edition:us, United States
GeoIP Asnum edition:as88 Princeton University
If you run the command without any arguments, the Geoiplookup tool will automatically use GeoIP.dat and GeoIPASNum.dat instead of GeoLiteCity.dat. You will be told to look at the city level information later.
To get geographic information at the city level, you need to explicitly tell Geoiplookup to use the 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
Output includes state, city, zip code, latitude and longitude. The accuracy of the indicated different country and network addresses varies by location. For example, a broadband IP address has more accurate geographic information than a mobile network.
Method Two
If you want to eliminate the hassle of installing and upgrading the GEOIP database, you can try Ipinfo.io online service. Unlike other services, Ipinfo.io provides a JSON based geographic information API, so you can easily access geographic information at the command line using tools like curl.
Curl ipinfo.io/23.66.166.151
GeoIP
It is to be noted that their APIs limit the maximum of 1,000 requests per day.