If you want to find the actual geographic location of an IP address on the Earth, you can use many online GeoIp lookup services (such as geoiptool.com ). Most of these online services are supported by a free GeoIP database such as MaxMind. When using these web-based services, there are also some different methods to query the GeoIP database, especially the Linux Command Line.
In this tutorial, I want to demonstrateHow to obtain the actual geographic location of an IP address through the Linux Command Line.
Method 1The first method is to use the geoiplookup tool, which is a command line client used to query the GeoIP database of MaxMind. Geoiplookup allows you to query the geographical information or network information of an IP address (or domain name. You can install it using the following command (it comes with a free GeoIP database ).
Install geoiplookup On Debian, Ubuntu, or Linux Mint:
1 sudo apt-get install geoip-binInstall On Fedora:
1 sudo yum install geoipTo install it on CentOS, open the EPEL source and run the yum command:
1 sudo yum install geoipBy default, the installed geoiplookup and GeoIP. dat database files are located in/usr/share/GeoIP. This database can only query country information.
1 geoiplookup 23.66.166.1511 GeoIP Country Edition: US, United StatesYou can download an additional GeoIP database from MaxMind, which will tell you more detailed information, not just the country. You can also download many latest GeoIP. dat databases from this website. I recommend that you do this because the GeoIP. dat installed from a Linux source may be outdated. The GeoIP database on MaxMind is updated every month.
To install the GeoIP database from MaxMind, follow these steps. You may want to add a cronjob (crontab task, which is a command used to execute tasks cyclically in Unix and Unix-like systems) to automate this process.
1234567 wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gzwget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzwget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gzgunzip 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-Execute geoiplookup, you will see the additional autonomous system Number (AS Number. This will give you a general idea of which administrative domain the IP address belongs ).
1 geoiplookup 128.112.119.20912 GeoIP Country Edition: US, United StatesGeoIP ASNum Edition: AS88 Princeton UniversityIf you do not include any parameters when running the command, the geoiplookup tool automatically uses GeoIP. dat and GeoIPASNum. dat instead of GeoLiteCity. dat. We will show you how to view information at the city level.
To obtain the geographic location information at the city level, you must explicitly tell geoiplookup to use the GeoLiteCity. dat database.
1 geoiplookup-f/usr/share/GeoIP/GeoLiteCity. dat 23.66.166.1511 GeoIP City Edition, Rev 1: US, MA, Cambridge, 02142, 42.362598,-71.084297, 506,617The output includes the state, city, zip code, latitude, and longitude. The accuracy of the indicated country and network address varies by location. For example, the geographic location information of a broadband IP address is more accurate than that of a mobile network.
Method 2If you want to install and upgrade the GeoIP database, try the ipinfo. io online service. Unlike other services, ipinfo. io provides JSON-based geographic information APIs, so you can easily obtain geographic information using tools such as curl on the command line.
1 curl ipinfo. io/23.66.166.151Note that their API limits a maximum of 1,000 requests per day.