Sometimes, you might need to find out the geographical location of a Linux server, more so a cloud server. Such information may include its IP address and Physical location in terms of Country, City, and coordinates ( Longitudes and Latitudes ). There are different ways of going about this.
In this context, we shall look into how to find your server's Geographic location.
Before proceeding with this procedure, ensure that curl is installed on the system. We shall use it to make API requests also. First, check if curl is installed:
$ curl --version
If not available, then install install curl by simply running the command provided below:
$ sudo apt install curl
If you have the public IP address of the Linux server, that's fine. However, if you are currently logged in and would want to verify the server’s public IP, you can do so by running the following curl command:
$ curl https://ipinfo.io/ip
Additionally, you can also invoke the curl command followed by the ifconfig.me tool that is used for displaying the public IP address on Linux systems:
$ curl ifconfig.me
With the IP address in hand, you can now proceed to extract the precise Geographical details by sending API requests to ipinfo.io as shown. The ip-address is the public IP of the server:
$ curl https://ipinfo.io/ip-address
The command yields a wealth of information in JSON format which includes the city, country, region, geolocation in terms of latitude and longitude, and the timezone where the server is situated.
If you just want to get the Country information alone, you can use the geoip lookup tool. This is available in repositories for major Linux distributions. You can install it as follows.
On CentOS/RHEL, execute the command:
$ sudo yum install geoip
On Ubuntu / Linux Mint, run:
$ sudo apt install geoip-bin
On Arch / Manjaro, execute:
$ sudo pacman -S geoip
On SUSE Linux, run the command:
$ sudo zypper install geoip
Once installed, you can run it as provided to obtain the country where your server is located:
$ geoiplookup IP-address
This article covers how to get the geographical information of a Linux server via the command line. 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.
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
To get the server's public IP address, use the following curl command to make an API request to ipinfo.io in your terminal:
$ curl https://ipinfo.io/ip