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'