Each server has a public IP address that is assigned to the server directly through a router. This public IP address can be used to determine the geolocation of the server, which includes information such as the continent, country, and even the estimated latitude and longitude of the server.
There are many situations where you need to determine the geographic location of a remote Linux machine based on its IP address. You may know how to find out the geolocation of the server through the web browser, but today we will see how to find it through the terminal application in a Debian operating system. In this article, we’ll tell you how to find out the public IP address of a system and then retrieve its geolocation using ipinfo.io’s two open APIs.
Geolocation information can be used in several ways. Some use cases are listed below:
- Digital advertising
- Content personalization
- Law enforcement
We ran the commands and procedures mentioned in this article on a Debian 11.
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 on 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 curl and jq
Launch the terminal in your Debian OS. For that, go to the Activities tab in the top left corner of the desktop. Then in the search bar, type terminal. When the Terminal icon appears, click on it to launch it.
Switch to the superuser account in your Debian OS by using the following command in the terminal and then enter the required password.
Then run the following command in the Terminal to update your system’s repository index:
$ apt-get update
<img alt="Update Debian Package lists" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-325.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="118" src="data:image/svg xml,” width=”816″>
Curl will be used to make HTTP requests and jq will be used to process the JSON data from the geolocation APIs. Curl and jq are available in the official Debian repositories, so we can install it using the apt-get command in Terminal.
Run the following command in Terminal to install curl and jq:
$ apt get insntall curl jq
<img alt="Install curl program" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-326.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="337" loading="lazy" src="data:image/svg xml,” width=”752″>
The system will provide you with a Y/n option to continue or cancel the installation process. Enter Y and then hit enter. Then, wait for a while until the installation of the above tools is completed.Advertisement
Step 2: Find the Debian machine/server’s Public IP
To find the server’s geographic location data, we will need its public IP address. It is the address that is assigned to your server or router by an ISP. All servers on the Internet are recognized by these IP addresses. To fetch the Public IP address, we will use the API provided by ipinfo.io.
We will have to use the curl command to make an API call to ipinfo.io. To do so, open the Terminal and execute the following command:
$ curl https://ipinfo.io/ip
<img alt="Find server IP address" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-327.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="74" loading="lazy" src="data:image/svg xml,” width=”448″>
The output above, (blurred here due to privacy concerns), is the Public IP of the server through which it is connected to and recognized in the Internet world.
Step 3: Get Geolocation based on Public IP
Now we have got the server’s public IP Address, we will now make a request to ipinfo.io‘s API to fetch the server’s geolocation data. We will make use of curl command for this purpose.it will fetch the following details:
- Latitude and Longitude
Execute the below command in Terminal to fetch the geolocation data from ipinfo.io‘s API using the curl:
$ curl https://ipinfo.io/
Replace the with your public IP address.
Alternative IP GeoLocation providers:
Replace the with your public IP address.
Use Bash script to Print Geo Location
Every time, you have to check your server’s geographical location, you have to follow all the above-described steps. Instead of this, we can automate this process by creating a bash script. This script will allow fetching the geographical location of your server by running only a single command. Follow the below steps to create a script.
We can create the script using any text editor. For the current scenario, we are using nano editor, so we will type nano followed by the script name (name it anything you want).
$ nano geolocation.sh
Then copy and paste the following line using the right-click menu.
$ curl -s https://ipinfo.io/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'
Press Ctrl o to save the script and Ctrl x to exit
Now we will the above script executable by granting the executable permissions to it. To do so, run the following command in Terminal:
$ chmod x geo_location.sh
Now we are ready to test the script. Run the following command in Terminal to run the script.
<img alt="Run geolocation script" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-330.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="213" loading="lazy" src="data:image/svg xml,” width=”435″>
From the above output, you can see that the script has printed the server’s latitude, longitude, city, and country name neatly to the Terminal.
That’s it for now! We have learned how to find a server’s geographical location using the Debian Terminal. The IP geographical location provides useful information consisting the name of country, city, latitude, and longitude. You can use this information can be used in several ways as discussed above in the article.