Get the geographical location of the IP address from the terminal

about get the geographical location of an IP from the terminal

In the next article we are going to take a look at how we can get the geographic location of our public IP address. We will be able to do this through open APIs and a simple bash script, all from the Ubuntu terminal. Today all devices connected to the Internet have an IP, which has been their identifier to the world. This address is the one we will use to obtain your location.

When we surf the Internet, each server has a public IP address, this is assigned directly to the server or through a router that sends network traffic to that server. IP addresses provide an easy way to track the geographic location of the server in the world. This can be achieved by making use of two APIs provided by e with which we will be able to obtain the country of the server, the name of the city and its geographical coordinates.

Related article:
Get your public or private IP address in Gnu / Linux

In the following lines we will see how get the geographical location of an IP from the terminal helping us with curl and jq. The main purpose and use for cURL is to automate file transfers or unsupervised sequences of operations, while jq is a very high-level functional language with support for backtracking and management of JSON data streams.

Geographical location of an IP from the terminal

Install Curl and jq

To obtain the geographical location of the IP address, we will have to install curl and jq command line tool to process the JSON data obtained from the geolocation APIs. To install both, in a terminal (Ctrl + Alt + T) you will have to write the command:

install curl and jq

sudo apt install curl jq

Get the public IP address of the server

Before getting the location, you need to know the public IP address of the server to which we connect. For this, we will use the following cURL command with which we will make a request to from terminal (Ctrl + Alt + T) as shown below:

public ip obtained with curl and


Obtain the data of the geographical location of the IP of your system thanks to the API

After executing the previous command we will obtain the public IP address. Once we have it, we will be able to make a request to to obtain geolocation data. For this we will use the following command in the terminal (Ctrl + Alt + T). It is important to replace the public IP with the one obtained with the previous command.

geographical location of an IP obtained with ipvigilante


Automate the API call using Bash Script

To automate the communication process with the API we are going to create a script inside a file called (the name is optional). For its creation we will be able to use any editor for the command line with which each one feels most comfortable.


When the editor opens, inside you just have to paste the line shown below:

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

Once pasted, all you have to do is save the file and exit the editor. After this it will be necessary make the script executable. We are going to do this by writing in the same terminal:

chmod +x

At this point, we can run the script as follows to get the geo location of the IP act:

geographic location of an IP with bash


The above script displays the city and country name along with the approximate latitude and longitude coordinates. The truth is that I do not know the margin of error that it can have.

If you don't want to save the above long command inside a file, you can also run that same command directly in terminal (Ctrl + Alt + T):

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

In this short article, we have seen how get geolocation of an IP from Ubuntu using terminal and curl and jq commands. You can get more information about jq or about cURL in their corresponding Wikipedia articles.

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.