How to Find Linux Server Geographic Location in Terminal

In this article, we will show you how to find the IP address geographic location of a remote Linux system using open APIs and a simple bash script from the command line.

On the internet, each server has a public-facing IP address, which is assigned directly to the server or via a router that sends network traffic to that server.

IP addresses provide an easy way to track the location of the server in the world by using two useful APIs provided by ipinfo.io and ipvigilante.com to get the city, state, and country connected with a server.

Install Curl and jq

To get the IP address geographic location of the server, we need to install curl command line downloader and jq command-line tool to process the JSON data from the geolocation APIs.

$ 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
Install Curl and JQ in Linux
Install Curl and JQ in Linux

Find the Server’s Public IP Address

To get the server’s public IP address, use the following curl command to make an API request to ipinfo.io in your terminal as shown.

$ curl https://ipinfo.io/ip
Get Linux Server IP Address
Get Linux Server IP Address

Get IP Location Data From The API

Once you have got the server public IP address, you can now make a request to ipvigilante.com‘s API to fetch the geolocation data using the following command. Make sure to replace <your ip address> with the server’s public IP.

$ curl https://ipvigilante.com/<your ip address>
Get Linux IP Geo Location
Get Linux IP Geo Location

This is the data we get from the above command.

Server Location Details
Server Location Details

Automate API Call using Bash Script

Now to automate the API process, we will create a script called getipgeoloc.sh (you can name it anything you want) using any of your favorite command line editors.

$ vim getipgeoloc.sh

Then copy and paste the following long command in it.

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 make the script executable with the following command.

$ chmod +x getipgeoloc.sh

Finally, run the script to get your Linux IP geographical location as shown in the following screenshot.

$ ./getipgeoloc.sh
Find Linux Server Geolocation
Find Linux Server Geolocation

The above script shows the city and country name along with the approximate latitude and longitude coordinates.

Alternatively, you can also run the above command without saving it in a script as shown.

$ curl -s https://ipvigilante.com/$(curl -s https://ipinfo.io/ip) | jq '.data.latitude, .data.longitude, .data.city_name, .data.country_name'
Get Linux Server Location Details
Get Linux Server Location Details

You might also like to read these following related articles:

  1. 4 Ways to Find Server Public IP Address in Linux Terminal
  2. Find Out All Live Hosts IP Addresses Connected on Network in Linux
  3. Find Top 10 IP Addresses Accessing Your Apache Web Server

That’s it for now! In this short article, we have shown how to get your Linux IP geographic location from the terminal using the curl and jq commands. Share your thoughts with us or ask any questions via the feedback form below.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

9 thoughts on “How to Find Linux Server Geographic Location in Terminal”

  1. There is a MUCH easier way to get the Geographic information.

    $ curl https://ipinfo.io/json
    

    In my case it returns:

    {
       "ip": "187.113.8.100",
       "hostname": "187.113.8.100.static.host.gvt.net.br",
       "city": "Campo Grande",
       "region": "Mato Grosso do Sul",
       "country": "BR",
       "loc": "-20.4428,-54.6464",
       "org": "AS18881 TELEFÔNICA BRASIL S.A",
       "postal": "79000-000",
       "timezone": "America/Campo_Grande",
       "readme": "https://ipinfo.io/missingauth"
    }
    
    Reply
  2. gip -g prints the geo location for the IP address.

    The tool is available at GitHub: https://github.com/softhub-software-development/gip.

    Reply
  3. I created a command-line tool called jc that JSONifies the output of many common Linux CLI tools for piping into jq. No need to parse with sed/awk/grep/tr/cut/etc. :) For example:

    $ ifconfig | jc --ifconfig | jq ...
    

    https://github.com/kellyjonbrazil/jc

    I’ll be adding more parsers to make life easier for cli jockeys!

    Reply
  4. There are many geoip lookup services. My issue with ipinfo.io is that they don’t appear to do IIPv6. By way of contrast, ipgeolocation.io has a comparable API and *will* do IPv4 and IPv6 addresses. They do require that you get an API key, but they have a development pricing level that’s free.

    jeffs@jeffs-desktop:/home/jeffs $ curl -4 -s "https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}" | jq ".ip" | sed -e s/\"//g
    97.126.67.233


    jeffs@jeffs-desktop:/home/jeffs $ curl -6 -s "https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}" | jq ".ip" | sed -e s/\"//g
    2602:61:7e43:e900:58fd:e794:22b1:e919

    Likewise, I can get the information about somebody else’s IPv4 or IPv6 address:

    jeffs@jeffs-desktop:/home/jeffs $ curl -4 -s "https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}&ip=208.97.189.29" | jq ".latitude,.longitude" | awk -F": " '{ print substr($1,2,length($1)-2)}'
    33.91670
    -117.90000


    jeffs@jeffs-desktop:/home/jeffs $ curl -6 -s "https://api.ipgeolocation.io/ipgeo?apiKey=${API_KEY}&ip=208.97.189.29" | jq ".latitude,.longitude" | awk -F": " '{ print substr($1,2,length($1)-2)}'
    33.91670
    -117.90000

    I am filtering the outputs through sed to get rid of the ” characters.

    Bottom line is that there are many IP geolocation services. You should try several different ones, see what API you like best. Everything else in this article is a little bash-foo.

    Reply
  5. Nice tutorial.

    You can also find IP by using:

    # curl ifconfig.me
    

    and if GeoIP is installed:

    # geoiplookup
    

    Gives you the country name and info about your ISP.

    If you specify location of GeoLiteCity.dat file:

    # geoiplookup -f /usr/share/GeoIP/GeoLiteCity.dat
    

    You get the rest (city and coordinates).

    Reply

Leave a Reply to Aaron Kili Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.