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

Every server on the Internet has a public IP address. This is either assigned directly to the server or assigned to a router that sends traffic to that server. The IP addresses give us an idea of ​​where this server is 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 one server or another remote system. It doesn’t give you an accurate GPS location; it just lets you see the general area of ​​the IP address.

Connect to a remote system

You will run the following commands on the Linux server or other remote systems you wish to geolocate, so you must log in to the server and access a shell first. For example, you can connect via SSH. You can 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 we retrieve. 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 installer instead.

sudo apt-get install curl jq

Find the public IP address of the server

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 API

Now that we have the server’s public IP address, we can call the ipvigilante.com API to get the geolocation data. Replace with the address that returned in the previous command.

curl https://ipvigilante.com/

curl command output

Let’s take a closer look at the data we retrieve from this call:

metadata showing location information

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

Creating a script to automate the API call

We can create a script that grabs 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 called geolocate.sh . Insert the contents of the script 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 return to the terminal. Make the script executable from the terminal, granting execute permission on this file.

chmod u+x geolocate.sh

You are now ready to test it. Run the geolocate.sh script and check the contents of the output file:

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

execution of the geolocation script

Update geolocation data once a day with a Cron job

Let’s create a cron job for our server to 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 server’s /tmp/ folder. Creating a 24 hour cron job is as simple as placing our script in the /etc/cron.daily directory. We need to 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, like plotting our servers on a map or combining geolocation with traffic logs to see where our server hotspots are around the world.