There are times when you want to fetch the geographical location of a remote Ubuntu machine based on its IP address. This article will explain how you can do so by first obtaining a system’s public server IP and then using this IP to fetch the geographical location, all through the APIs provided by ipinfo.io and ipvigilante.com
We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.
Show Geographical Location of an IP Address on the Terminal
In order to fetch the geographical location of your server, you will need the Curl downloader and the JQ command line tool. This tool will let you get and process the required data from the geolocation APIs from the Internet. Please follow these steps in order to install these tools and then fetch the required location information by using your machine’s public IP.
Step 1: Install the Curl and jq packages
Curl and jq are easily available through the Official Ubuntu repository and can be installed through the command line using the apt-get command.
Open your Terminal application either through the system Application Launcher Search or through the Ctrl+Alt+T shortcut.
The next step is to update your system’s repository index through the following command:
$ sudo apt-get update
This helps you in installing the latest available version of a software from the Internet.
Please note that only an authorized user can add, remove and configure software on Ubuntu.
Now you are ready to install the required tools; you can do so by running the following command as sudo:
$ sudo apt-get install curl jq
The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.
Step 2: Find the Ubuntu machine/server’s Public IP
All servers on the Internet are recognized by public IP addresses. This is the IP address assigned to your server directly or through a network traffic router. We can fetch this IP of your machine by using the API provided by ipinfo.io.
Use the following curl command in order to fetch your public IP:
$ curl https://ipinfo.io/ip
The output, blurred here due to privacy concerns, is the Public IP through which your server is recognized in the Internet world.
Step 3: Get Geolocation based on Public IP
Once you know your public IP, you can make a request to the ipvigilante.com’s API to send you your servers’ geographical location. Use the curl command mentioned below to fetch your location details such as:
- Latitude and Longitude
$ curl https://ipvigilante.com/<public-IP>
The information you get is pretty in-detail and serves the purpose well.
Use Bash script to Print Geo Location
Instead of following the above described steps every time you have to check your geo location, you can make use of a bash script that we will describe here. With this bash script, you can automate your system to fetch the geographical details of your server through a single command that runs this script. Here is how you can go about it:
Open a new script file in one of your favorite text editors. We will use the nano editor in order to open an empty script file by the name of geo_location.sh
$ nano geo_location.sh
In that empty file, add the following script.
curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'
Tip: Instead of typing the whole script into your bash file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.
Now, exit the file through the Ctrl+X shortcut and save the file on the “Save modified buffer?” prompt by typing Y and then hitting enter.
In order to make this file an executable script, run the following command in your Terminal:
$ chmod +x geo_location.sh
Finally, run this script as follows:
You can see how this script prints your server’s latitude, longitude, city, and country name neatly to the Terminal.
If you have to fetch your server’s geolocation just once, you do not need to form a bash script. Otherwise, the script will automate the entire process each time you have to check your Ubuntu machine’s graphical location based on its public IP.