Sometimes you may need to find the geo location of a remote Linux system using its IP address. In this article, we will learn how to do that using simple shell scripting and APIs. Every server has an IP address directly assigned by the network it is a part of. You can use two useful APIs ipinfo.io and ipvigilante.com that allow you to easily get the city, state and country of an remote Linux server.
How to Check Linux Server Location
Here are the steps to get Linux server location from IP address.
1. Install cURL and jq
First we need to install cURL and jq tools. cURL is used to download location information from APIs and jq tool is used to convert the JSON data received from these APIs into readily usable format. So open terminal and run the following commands, depending on your system.
$ 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
2. Find Server’s IP address
This step is optional. If you already know the IP address of your remote Linux server, you can skip to the next step. Otherwise, you may need to run the following command from within the remote server (not the client) to find the remote server’s public IP address.
$ curl https://ipinfo.io/ip
Alternatively, you can Google ‘what is my IP’ from the remote server (not the client). Whichever system you run these commands from, it will show you the IP address of that system.
3. Get IP Location Data
Once you have the public IP address of remote server, you can use curl to make a request to ipvigilante API to get the location information about the IP address. Replace <your ip address> with your remote server’s public IP address.
$ curl https://ipvigilante.com/<your ip address>
Here is a sample output in JSON format.
4. Automate Geo Location Checking
We will create a shell script to automatically get the public IP of remote server and check its location. Create a new file using a text editor.
$ vi get-geo.sh
Add the following line to it.
curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'
Save and close the file. In the above code, we we use curl command to make request to ipvigilante.com. Instead of providing the IP address of remote server, we use another curl request to ipinfo.io to get the remote server’s IP address. We pass the JSON output of the first curl command to jq utility and fetch latitude, longitude, city_name, country_name.
$ chmod +x get-geo.sh
You can run the shell script from remote server using the following command.
If you want to get other information from ipvigilante, you can modify the variables used in jq command.
In this article, we have learnt how to find Linux server location. It is very useful in case you need to find the geo location of remote Linux server. If you know that remote server’s public IP address you can easily send request to ipvigilante.com and get required data.
How to Encrypt Drives Using LUKS in Linux
How to Limit Time & Memory of Processes in Linux
How to Use Yum History to Find Installed Package
What to Do After Installing Ubuntu
How to Fix No Route To Host SSH Error