How To Lookup The Geographic Location Of An IP Via Terminal
If you want to find out where in the world a specific IP address is located, there are a few online GeoIP lookup services. They are free and have databases from a lot of services. These lookup services can be accessed on the Web, of course but what if we don’t want to go through that? Well, we can always query their databases via terminal. Of course you need an Internet connection to do that but it’s easier to do it with a command than going through the browser. In this article, I’m going to use the geoiplookup tool.
First we need to install it along with the databases used by it.
For Debian, Ubuntu, Linux Mint:
$ sudo apt-get install geoip-bin
$ sudo yum install geoip
For CentOS you need to have EPEL repository enabled and then type:
$ sudo yum install geoip
After the installation you have the GeoIP database which can look up only the country.
$ geoiplookup 188.8.131.52
GeoIP Country Edition: US, United States
After that you should install additional databases from MaxMind which is the website powering the service. That will give you additional information about the IP you are looking up. You can also update the Geoip.dat file which contains the database so you are always up-to-date.
To install additional GeoIP databases from MaxMind type:
$ 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/
If you re-run geoiplookup, you will see:
$ geoiplookup 184.108.40.206
GeoIP Country Edition: US, United States GeoIP ASNum Edition: AS88 Princeton University
It shows you the AS Number which is basically which administrative domain the IP belongs to. Without any parameters geoiplookup, the way we run it before, only returns the country and the administrative domain. But you can use GeoLiteCity.dat to get city level information.
$ geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat 220.127.116.11
GeoIP City Edition, Rev 1: US, MA, Cambridge, 02142, 42.362598, -71.084297, 506, 617
It includes state, city, zip code, latitude and longitude. The accuracy of the location has a bit of an error margin to it but is generally very good for broadband networks as opposed to mobile ones.