How to Find Linux Server Geographic Location in Terminal?

For purpose of security, cyber-crime investigation, government compliance or just for curiosity we might ned to track the geographical location of a Linux server in the internet or at least the location of the server which diverts the internet traffic to the server we are interested in. It involves getting the I.P address of the server and using some third party services offered in the web, to map that I.P address to get the location. In this article we will see the steps to achieve that.

Step 1 − Install curl jq

The curl package will make the http requests to the server and jq will be required to process the JSON data we receive from APIs for mapping the IP addresses to geographical locations. The below steps shows how to install these tools.

sudo apt-get install curl jq

Running the above code gives us the following result −

$ sudo apt-get install curl jq
Reading package lists... Done
Building dependency tree
Reading state information... Done
curl is already the newest version (7.47.0-1ubuntu2.14).
The following NEW packages will be installed:
jq libonig2
Setting up libonig2:amd64 (5.9.6-1ubuntu0.1) ...
Setting up jq (1.5+dfsg-1ubuntu0.1) ...
Processing triggers for libc-bin (2.23-0ubuntu11) .

Step 2 − Find the Server’s I.P address

You can use nslookup to find the server’s I.P address if you know the domain name being served from the server. In the below example we see how to get the IP address of a domain name by using nslookup command. Let's consider as our example.

Use the following curl command to get the server I.P address. is a web service that gives the I.P details.

$ nslookup

Running the above code gives us the following result −

Non-authoritative answer: name = name =

Step 3 − Find Information about the I.P address

Next we use IP to get the details related to this IP address. That details comes back in form of JSON listing the the hostname,the city, the state, the country etc.

$ curl

Running the above code gives us the following result:

   "ip": "",
   "hostname": "",
   "city": "New York City",
   "region": "New York",
   "country": "US",
   "loc": "40.7143,-74.0060",
   "org": "AS20940 Akamai International B.V.",
   "postal": "10004",
   "timezone": "America/New_York",
   "readme": ""

Finding Latitude and Longitude

Now when we need additional details like latitude and longitude we can use another geolocation service provider called Then we use the jq tool to get the formatted response from the JSON structure returned by the the API call.

$curl -s | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'

Running the above code gives us the following result −

"United States"

Updated on: 03-Jan-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started