Support > About independent server > A way to find the geographic location of a Linux server in a terminal
A way to find the geographic location of a Linux server in a terminal
Time : 2024-11-29 15:26:26
Edit : Jtti

We can use the open API and simple bash scripts in commands to query the geolocation information of the IP address of a remote Linux system. Every server on the Internet has a public-facing IP address, which is either assigned directly to the server or reassigned to the server using routes that traffic sends to the server. The IP address provides an easy way to track the location of the server over time, and you can use two useful apis provided by ipinfo.io and ipvigilante.com to obtain the location of the connection to the server.

Install Curl and jq. To get the IP address geographic location of the server, you need to install the curl command line downloader and jq command line tool to process the API and JSON data from the geographic location.

$ sudo apt install curl jq #Ubuntu/Debian

$ sudo yum install curl jq #CentOS/RHEL

$ sudo dnf install curl jq #Fedora 22+

$ sudo zypper install curl jq #openSUSE

Query the public IP address of the server. To obtain the public IP address of the server, run the curl command to issue an API request to ipinfo.io at the terminal:

$ curl https://ipinfo.io/ip

After obtaining the IP location data from the API and obtaining the server's public IP address, you can now use the following command to make a request to the API of ipvigilante.com to obtain the geolocation data, making sure to replace it with <your ip address> as the server's public IP.

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

To automate the API process, use getipgeoloc.sh to create a script named (you can name it whatever you want) using any command-line editor you like:

$ vim getipgeoloc.sh

Copy and paste the following command:

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

Save the file and use the following command to execute the script:

$ chmod + x getipgeoloc.sh

Finally, run the script to obtain the Linux IP address geographical location:

$./getipgeoloc.sh

The above script shows the city and country names and approximate latitude and longitude coordinates.

Alternatively, you can run the above command without saving it in the script, as shown below.

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

Relevant contents

Common methods for viewing all installed software packages in CentOS What are the common commands for diagnosing Linux hard disk problems The role of virtual file systems in Linux Useful RPM package management commands in Linux Quick Linux Method of viewing installed software packages Common operations on Linux disk partitioning tool Parted What is the network resilience of Hong Kong servers and how to maintain it Useful tool for monitoring CPU and GPU temperature in Ubuntu Method of using dtrx command in Linux Start, stop, and restart service commands in Linux
Go back

24/7/365 support.We work when you work

Support