This brief tutorial will walk you through how to get your geolocation from commandline in Linux. This could be useful when you wanted to know the location of your VPS or remote servers. A fellow Linux user Rafael Rinaldi has created a handy tool called "whereami" to find out the geolocation information using freegeoip.net from the commandline. Please note that some hosting providers might have hidden their server's exact place, or faked it due to security reasons. In such cases, this tool won't help. Now, let us get started to find out the geolocation of the Linux system from commandline using whereami utility.
To install npm on RPM based systems such as RHEL, CentOS, Fedora, run the following commands:
sudo yum install epel-release
sudo yum install npm
sudo dnf install epel-release
sudo dnf install npm
On Debian, Ubuntu, Linux Mint, run:
sudo apt-get install npm
On Arch Linux and its derivatives:
sudo pacman -S npm
sudo zypper install npm
Once npm installed, run the following command to install "whereami" tool.
npm install -g @rafaelrinaldi/whereami
You will get an output something like below.
/usr/bin/whereami -> /usr/lib/node_modules/@rafaelrinaldi/whereami/bin/whereami /usr/lib └─┬ @email@example.com ├─┬ firstname.lastname@example.org │ ├─┬ email@example.com │ │ └── firstname.lastname@example.org │ ├── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ ├── firstname.lastname@example.org │ ├── email@example.com │ └─┬ firstname.lastname@example.org │ └── email@example.com ├─┬ firstname.lastname@example.org │ └─┬ email@example.com │ ├── firstname.lastname@example.org │ └─┬ email@example.com │ └─┬ firstname.lastname@example.org │ ├── email@example.com │ └── firstname.lastname@example.org ├── email@example.com ├─┬ firstname.lastname@example.org │ └── email@example.com └── firstname.lastname@example.org
Get Your Geolocation From Commandline In Linux
Now, it's play time. To know your location, simply run:
Sample output would be:
As you see in the above output, It displays the latitude (11.1), and longitude (77.35) of my location.
I don't understand, can I have a human-readable format? Of course, you can! Add --f human switch with whereami command like below.
whereami --f human
Here is my output:
Tiruppur, Tamil Nadu, India
I wouldn't say this is an accurate result, but it was very close to my actual location.
I need more details, can I have that too? Yes! Display the raw result that contains the public IP address, country code, zip code, metro code time zone etc., using the following command:
To know more details about whereami command, run:
Usage: whereami [OPTIONS] Get your geolocation information using freegeoip.net from the CLI Example: $ whereami -23.4733,-46.6658 $ whereami --f human San Francisco, CA, United States Options: -v --version Display current software version -h --help Display help and usage details -f --format Output format (either human, json or sexagesimal) -r --raw Output raw data from freegeoip.net
I am pretty sure there could be other tools and ways to find out the geolocation. D you know any other methods? Please enlighten me in the comment section below.
How could I get this to work on OS X?
I don’t have Mac OS X. I guess you can install it using homebrew. To install npm, run – brew install npm. And then install whereami using command – npm install -g @rafaelrinaldi/whereami. Let me if it works. Good luck.
On OS/X, install ‘jq’. I use ‘homebrew’ for installing on OS/X.
To install a package, ‘brew install jq’
Then use this one liner (works on Linux, too, with ‘jq’ installed):
curl -s http://freegeoip.net/json/ | jq -r ‘”(.latitude) (.longitude)”‘
When I run whereami I get the following error message:
Can’t write PID file to /var/run/whereami.started.20241 at /usr/sbin/whereami line 229.
Try as root user or use “sudo”. Good luck.
I am new with Linux and having some issues with the “whereami” command.
To be more accurate, if I try to use it in a bash script like this:
echo “You are now working from $(whereami –f human)”;
then the output will only contain the location, without the “You are now working from” string before it.