Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to automatically geo-locate an ip-address on Ubuntu linux? I'm looking to do this for errors in my auth.log.

share|improve this question
What does "geo-locate an ip-address" mean? Look up where it comes from? – womble Nov 20 '09 at 17:35
Find an approximate physical location associated with the ip address. – C. Ross Nov 20 '09 at 17:39
up vote 3 down vote accepted

Ubuntu PreReqs:
sudo apt-get install libgeoip1 libgeo-ip-perl libregexp-common-perl

Script By Me just For you:

#Parses out ip and prints ip followed by country
use strict;
use warnings;

use Regexp::Common qw /net/;
use Geo::IP;

my $gi = Geo::IP->new(GEOIP_STANDARD);

while (<>) {
    #Following matches IPv4 addresses and stores the result in $1
    #The way this is now, it will only do the first IP on each line
    if (/($RE{net}{IPv4})/g) {
        print $1 . ':' . $gi->country_code_by_addr($1);

Input Output:

The script justs loops over its input, so if the script is called and is executable, you can just do something like cat access.log | If you want more accurate detail, see Geo::IP perl module docs (and you might need to install a different database).

share|improve this answer
You will want the grep function mostly like for multiple matches on a line: – Kyle Brandt Nov 20 '09 at 19:23
Works great. Thanks a lot! – C. Ross Nov 24 '09 at 17:39

It should be fairly straightforward in Perl. Just take auth.log and get a list of IPs out of it with grep or awk, then pipe your list of IPs into a Perl script, and use Geo::IP to get a country/city match from it.

share|improve this answer

From commandlinefu:

GeoipLookUp(){ curl -A "Mozilla/5.0" -s "$1" | grep "^IP.*$1" | html2text; }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.