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.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux terminal queries.
In this context, we shall look into how to find your server's Geographic location.
What to do before finding Linux Server Geographic Location in Terminal ?
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
How to Find the Public IP address of the server ?
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
How to Find the Geographical details of the server ?
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
[Need help in fixing Software Installation on Linux Systems? We can help you. ]