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.
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.
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
Let’s take a closer look at what data we get back from this call:
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:
Save the script and go back to the terminal. Make the script executable from the terminal, by granting the execute permission on this file.
Now you’re ready to test it out. Run the geolocate.sh script and check the contents of the output file:
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.
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.