How to find the geographical location of a Linux server using the terminal on CentOS 8

A public IP address is assigned to each server when it is connected to the Internet. This address can be assigned directly to a router that is used to send signals or traffic to the server.

This article shows how the IP address and geographical location of the remote Linux system can be determined using open APIs and how a bash script can be executed through the terminal. We have executed all commands and scripts on the CentOS 8 system, which are listed below:

Find the geographical location of a Linux server on CentOS 8

You need to run the various commands on your system to find your server’s geographic location. Login as root user on your CentOS 8 system and perform the steps as follows:

Open terminal

Open the terminal window through the shortcut method ‘Ctrl + Alt + t’ or Click on ‘Activities’ and launch the terminal by typing ‘terminal’ in the application search bar as follows:

Open Linux Terminal

Install curl and jq

To find the IP address and geographic location of a Linux server, you need to install Curl and Jq command-line tools that will be used to process the JSON data using geolocation APIs. For that purpose, you will execute the following command on the terminal:

$ sudo yum install curl jq

Install software

You will press ‘y’ and ‘Enter’ that will allow you to use additional space for installation.

After a while, you will see that curl and jq have been successfully installed on your system CentOS 8.

Confirm software installation

How to find the public IP address of a remote server?

Using curl command, you can get the public IP address of the server. The curl command is used to send an API request to the following URL ipinfo.io using the terminal as follows:

$ curl https://ipinfo.io/ip

Find GEO information of an IP address

How to get geographic location data from API?

Once the IP address of the server is retrieved, now you will send a request to ipvigilante.com API that will fetch the geographic location. For this purpose, use the following command in which you will replace the <your IP address> with the public IP address of the server:

$ curl https://ipvigilante.com/<Enter your IP address>

Get IP address data fo a given IP address

Get geographic location data using a bash script

You can automate the API process using the bash script method. To create a script, you need to create a text file in which you will paste 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'

Now, save the above script file with the name ‘getipgeoloc.sh’. You can also save with your desired name as per your requirements. Again, move to the terminal window and type the command to create an executable file as follows:

$ chmod +x getipgeoloc.sh

Now it’s time to run the above script file to display the Linux server’s IP geographic location using in the following command:

$ ./getipgeoloc.sh

After executing the above script, the city, country name will be displayed along with longitude and latitude coordinates.

Create bash script to get IP details

You can run the above script without saving it in an executable script file. So, you will type the following command on the terminal:

$ curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'

All in one command to get IP geo information

You will see that approximate coordinates with your geographical location will be displayed on the terminal as follows:

Conclusion

In this article, you have explored how to find the geographical location of a public IP remote Linux server. Furthermore, you learned how to install and use curl, jq tools on your CentOS 8, how to use these to find the geographical location of your remote server. In case of any queries, please do comments in the comment box.