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 oracle.com as our example.

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

$ nslookup www.oracle.com

Running the above code gives us the following result −

Server:127.0.1.1
Address:127.0.1.1#53
Non-authoritative answer:
www.oracle.comcanonical name = ds-www.oracle.com.edgekey.net.
ds-www.oracle.com.edgekey.netcanonical name = e870.dscx.akamaiedge.net.
Name:e870.dscx.akamaiedge.net
Address: 104.80.62.56

Step 3 − Find Information about the I.P address

Next we use IP info.in 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 https://ipinfo.io/104.80.62.56

Running the above code gives us the following result:

{
   "ip": "104.80.62.56",
   "hostname": "a104-80-62-56.deploy.static.akamaitechnologies.com",
   "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": "https://ipinfo.io/missingauth"
}

Finding Latitude and Longitude

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

$curl -s https://ipvigilante.com/104.80.62.56 | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'

Running the above code gives us the following result −

"42.36260"
"-71.08430"
"Cambridge"
"United States"

Updated on: 03-Jan-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements