How to Geo-Locate Linux Server Using 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 the router, which is used to send signals or traffic to the server.

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

Find the geographic location of a Linux server on CentOS 8

You need to run various commands on your system to determine the geographic location of your server. Log in as root user on CentOS 8 system and follow these steps:

Open terminal

Open a terminal window with the keyboard shortcut Ctrl + Alt + t or click Actions and launch the terminal by typing “terminal” in the application’s search bar as follows:

Install curl and jq

To find out the IP address and geographic location of a Linux server, you need to install the Curl and Jq command line tools, which will be used to process JSON data using geolocation APIs. To do this, you run the following command on the terminal:

$ sudo yum install curl jq

Install software

You will press “y” and “Enter”, which will allow you to use the extra space for the installation.

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

Confirm software installation

How do I 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 ipinfo.io url using the terminal as follows:

$ curl https://ipinfo.io/ip

Find geo IP address information

How to get geographic location data from API?

After getting the server IP, you will send an API request to ipvigilante.com which will get the geographic location. To do this, use the following command where you replace <ваш IP-адрес> public IP address of the server:

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

Retrieve IP address data for a given IP address

Get geo location data using bash script

You can automate the API process using the bash script method. To create a script, you need to create a text file where 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 as getipgeoloc.sh. You can also save the desired name as per your requirement. Go back to the terminal window and enter the command to create the executable as follows:

$ chmod +x getipgeoloc.sh

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

$ ./getipgeoloc.sh

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

Create bash script to get IP details

You can run the above script without saving it to the script executable. So, you type the following command in 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 geo-information IP

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

Conclusion

In this article, you learned how to geo-locate a remote Linux server with a public IP. In addition, you learned how to install and use curl, jq tools on your CentOS 8, how to use them to geo-locate your remote server. If you have any questions, leave comments in the comments box.

How to Geo-Locate Linux Server Using Terminal on CentOS 8

Sidebar