Use commands on CentOS to obtain the geographic location of IP addresses

Source: Internet
Author: User
Tags geoip geoip database geoip lookup maxmind linux mint
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. While using these web-based services, there are also some different methods to query GeoIP

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 1

The 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:

Sudo apt-get install geoip-bin

Install on Fedora:

To install it on CentOS, open the EPEL source and run the yum command:

By default, the installed geoiplookup and GeoIP. dat database files are located in/usr/share/GeoIP. This database can only query country information.

Geoiplookup 23.66.166.151

 

GeoIP Country Edition: US, United States

You 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.

Wgethttp: // geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Wgethttp: // geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Wgethttp: // 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-execute geoiplookup, you will see the additional autonomous system number (ASNumber) information. This will give you a general idea of which administrative domain the IP address belongs ).

Geoiplookup 128.112.119.209

 

GeoIP Country Edition: US, United States

GeoIP ASNum Edition: AS88 Princeton University

If 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.

Geoiplookup-f/usr/share/GeoIP/GeoLiteCity. dat23.66.166.151

 

GeoIP City Edition, Rev 1: US, MA, Cambridge, 02142, 42.362598,-71.084297, 506,617

The 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 2

If 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.

Curl ipinfo. io/23.66.166.151

Note that their API limits a maximum of 1,000 requests per day.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.