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.
Install whereami
Installing 'whereami' tool is fairly simple and straight-forward. You need to install npm, a javascript package manager first.
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
Or,
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
On SUSE/openSUSE:
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 └─┬ @rafaelrinaldi/whereami@1.3.1 ├─┬ got@6.7.1 │ ├─┬ create-error-class@3.0.2 │ │ └── capture-stack-trace@1.0.0 │ ├── duplexer3@0.1.4 │ ├── get-stream@3.0.0 │ ├── is-redirect@1.0.0 │ ├── is-retry-allowed@1.1.0 │ ├── is-stream@1.1.0 │ ├── lowercase-keys@1.0.0 │ ├── safe-buffer@5.0.1 │ ├── timed-out@4.0.1 │ ├── unzip-response@2.0.1 │ └─┬ url-parse-lax@1.0.0 │ └── prepend-http@1.0.4 ├─┬ loading-indicator@2.0.0 │ └─┬ log-update@1.0.2 │ ├── ansi-escapes@1.4.0 │ └─┬ cli-cursor@1.0.2 │ └─┬ restore-cursor@1.0.1 │ ├── exit-hook@1.1.1 │ └── onetime@1.1.0 ├── minimist@1.2.0 ├─┬ pinkie-promise@2.0.1 │ └── pinkie@2.0.4 └── sexagesimal@0.5.0
Get Your Geolocation From Commandline In Linux
Now, it's play time. To know your location, simply run:
whereami
Sample output would be:
11.1,77.35
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:
whereami -r
Sample output:
{"ip":"122.178.36.244","country_code":"IN","country_name":"India","region_code":"TN","region_name":"Tamil Nadu","city":"Tiruppur","zip_code":"641603","time_zone":"Asia/Kolkata","latitude":11.1,"longitude":77.35,"metro_code":0}
To know more details about whereami command, run:
whereami -help
Sample output:
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.
Resource:
7 comments
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.
http://brew.sh
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)”‘
Thank you.
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.
Any suggestions?
Try as root user or use “sudo”. Good luck.
Hi,
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.
Any thoughts?
Thanks