World map with internet connections concept
Toria/Shutterstock.com

You can find the geographic location of a remote Linux system using open APIs and a simple bash script. Geolocating a server could help you track it in the physical world, ensuring servers are located in regional hotspots.

Each server on the internet has a public-facing IP address. This is either directly assigned to the server, or assigned to a router that sends traffic to that server. IP addresses give us a clue about where that server is located in the world. We can get this geolocation data through two open APIs, provided by ipinfo.co and IP Vigilante and use it to see the city, state, and country associated with a server or other remote system. This doesn’t give you a precise GPS location; it just lets you see the general area of the IP address.

Connect to a Remote System

You’ll be running the following commands on the Linux server or other remote systems you want to geolocate, so you must connect to the server and access a shell on it first. For example, you might connect via SSH. You could run the commands on your local system to find its location, but you probably already know where you are!

Install curl and jq

We need two tools to access the geolocation API: curl to make HTTP requests and  jq to process the JSON data that we get back. Open a terminal and use  apt-get to install these tools on Ubuntu or Debian-based systems. On other Linux distributions, use your Linux distribution’s package installation tool instead.

sudo apt-get install curl jq

Find the Server’s Public IP Address

We also need the server’s public IP address before we can get the geolocation data. Use curl to make an API call to ipinfo.io in your terminal window.

curl https://ipinfo.io/ip

Get Location Data From The API

Now that we have the public IP of the server, we can make a call to ipvigilante.com’s API to get the geolocation data. Replace <your ip address> with the address that came back in the previous command.

curl https://ipvigilante.com/<your ip address>

output from curl command

Let’s take a closer look at what data we get back from this call:

metadata showing location information

The API returns the city, country, and continent, in which our server resides. It also returns the approximate latitude and longitude coordinates, in case we want to draw this server on an interactive map. We’ll be using “latitude,” “longitude,” “city_name,” and “country_name” in our script. The  jq command understands how to process the API data and extract these four fields out.

Creating a Script to Automate The API Call

We can create a script that grabs the geolocation data and writes it to a file in CSV format. The data will be written to a file called server_location.txt in the /tmp/ directory. Open your favorite editor and create a script named geolocate.sh . Insert the script contents shown below, and be sure to replace the IP address with your own:

#!/bin/sh

OUTPUT_FILE=/tmp/server_location.txt

# Grab this server's public IP address
PUBLIC_IP=`curl -s https://ipinfo.io/ip`

# Call the geolocation API and capture the output
curl -s https://ipvigilante.com/${PUBLIC_IP} | \
        jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name' | \
        while read -r LATITUDE; do
                read -r LONGITUDE
                read -r CITY
                read -r COUNTRY
                echo "${LATITUDE},${LONGITUDE},${CITY},${COUNTRY}" | \
                        tr --delete \" > \
                        ${OUTPUT_FILE}
        done

Save the script and go back to the terminal. Make the script executable from the terminal, by granting the execute permission on this file.

chmod u+x geolocate.sh

Now you’re ready to test it out. Run the geolocate.sh script and check the contents of the output file:

./geolocate.sh
cat /tmp/server_location.txt

running the geolocate script

Updating the Geolocation Data Once a Day With a Cron Job

Let’s create a cron job to make our server update its geolocation and save it to a file once a day. The daily cron job updates a file called server_location.txt in the /tmp/ folder of the server. Creating a 24-hour cron job is as easy as putting our script into the /etc/cron.daily directory. We must use the sudo command to copy the file as the root user, to avoid permission issues. Run the following command to copy geolocate.sh to the /etc/cron.daily directory.

sudo cp geolocate.sh /etc/cron.daily

These changes are immediate, and our script will run every 24 hours to update the contents of the /tmp/server_location.txt file. We can use this data to do interesting things, such as plotting our servers on a map as well as combining geolocation with traffic logs to see where in the world our server hotspots are.

Taylor Gibb Taylor Gibb
Taylor Gibb is a professional software developer with nearly a decade of experience. He served as Microsoft Regional Director in South Africa for two years and has received multiple Microsoft MVP (Most Valued Professional) awards. He currently works in R&D at Derivco International.
Read Full Bio »