How To Get Country Details For IP Addresses Using Command Line In Linux Mint


If you are looking for a command line tool in Linux Mint or any Debian based distro that can display the country to which an IP address belongs to, then geoiplookup will be useful.

Installing geoiplookup :

To install geoiplookup in Linux Mint, open Terminal and type :

sudo apt install geoip-bin

installing geoiplookup tool in Linux Mint

Using geoiplookup :

Once installed, simply run that command either with the hostname or IP for which country information is needed.

geoiplookup ip address

using geoiplookup  with ip addresses

geoiplookup hostname

using geoiplookup with hostnames

When the hostname is used, geoiplookup will fetch the country details of the IP address that the hostname resolves to.

Another useful way to run geoiplookup is on a list of hostnames and IP addresses.

To do this , first create a file with a list of IP addresses, hostnames or both, each on a separate line.

list of IPs in a file for which geoiplookup can be used

Next, open Terminal and run the following command :

cat iplist.txt | xargs -L1 geoiplookup

processing files containing IPs using geoiplookup

What the above command does is pipe the contents of the list to have them executed through geoiplookup by using xargs. This is essentially the same as running it on each of the line entries but in bulk.

The same command can be used for files that contain just the hostnames or a mix of both – IP addresses and hostnames.

using geoiplookup to process a list of hostnames

You can also sort and filter the output as needed. Here is a good resource on how to do that in detail.

Using geoiplookup to find country details of public IP of the PC :

If you want to find the country details of the public IP of your system, simply use it as follows :

curl -s icanhazip.com | xargs -L1 geoiplookup

using geoiplookup to get country details of public IP address of the system

All it will do is first get the public IP address of the PC silently using curl and then pass it on to geoiplookup which will then determine the country details.

Shell script that uses geoiplookup:

Finally, geoiplookup can also be wrapped in a simple Bash shell script which can then be used to process a file containing IPs or hostnames as needed. This script can be useful when the input file keeps getting updated with IPs or hostnames.

Open any text editor like nano and make a script as follows:

nano geoip.sh

shell script that uses a file for geoiplookup

For this shell script, the file path is as a variable so that it can be changed as needed. Use the same command as before to run geoiplookup on the specified filepath.

Also, it can be useful to first display the contents of input file and then the corresponding geoiplookup details.

Save the script and make it executable :

chmod +x geoip.sh

Finally, run it :

./geoip.sh

All done.


Leave a Comment