On CentOS, you can use the command line to find the geographic location information of an IP address.

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

1 sudo apt-get install geoip-bin

Install On Fedora:

1 sudo yum install geoip

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

1 sudo yum install geoip

By 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.151

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

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

12 GeoIP Country Edition: US, United StatesGeoIP 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.

1 geoiplookup-f/usr/share/GeoIP/GeoLiteCity. dat 23.66.166.151

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

1 curl ipinfo. io/23.66.166.151

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

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.