Every server has a public-facing IP address that is assigned directly to the server through a router. This public IP address can be used to track the geolocation of the server that tells the information including continent, country and even the estimated latitude, longitude of the server.

There are many times when you need to fetch the geographical location of a remote Linux machine based on its IP address. You might know how to find the geolocation of the server from the web browser but today we will see how to find it through the Terminal application in a Debian OS. In this article, we will explain how to find a system’s public IP and then using that IP to fetch its geographical location all through the two open APIs provided by ipinfo.io and ipvigilante.com.

Geolocation information can be used in different ways. Some of its use cases are as below:

  • Cybersecurity
  • Digital advertising
  • Content personalization
  • Geomarketing
  • Law enforcement

We have run the commands and procedures mentioned in this article on a Debian 10 OS.

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 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 terminal and then enter the required password.

$ su

Then run the following command in the Terminal to update your system’s repository index:

$ apt-get update

Update Debian Package lists

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

Install curl program

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 above tools is completed.

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 is the address that is assigned to your server or router by an ISP. All servers on the Internet are recognized by this 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

Find server IP address

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 ipvigilante.com‘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:

  • Continent
  • Country
  • State/province
  • City
  • Latitude and Longitude

Execute the below command in Terminal to fetch the geolocation data from ipvigilante.com‘s API using the curl:

$ curl https://ipvigilante.com/<Public_IP>

Replace the <Public_IP> with your public IP address.

Get Geolocation based on Public IP

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 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://ipvigilante.com/$(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

Script to get the GEO location of a server

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.

$ ./geo_location.sh

Run geolocation script

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 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.

How to find Geographic Server Location using the Terminal on Debian 10