- How to Find Linux Server Geographic Location in Terminal
- Install Curl and jq
- Find the Server’s Public IP Address
- Get IP Location Data From The API
- Automate API Call using Bash Script
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Related Posts
- 8 thoughts on “How to Find Linux Server Geographic Location in Terminal”
- Got something to say? Join the discussion. Cancel reply
- Navigating Your File System in Linux
- This chapter is from the book
- This chapter is from the book
- This chapter is from the book
- List Files and Folders
- How can I get the current working directory? [duplicate]
- 5 Answers 5
- How To Get Your Geolocation From Commandline In Linux
- Install whereami
- Get Your Geolocation From Commandline In Linux
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.
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.
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 with the server’s public IP.
Get Linux IP Geo Location
This is the data we get from the above command.
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.
Then copy and paste the following long command in it.
Save the file and make the script executable with the following command.
Finally, run the script to get your Linux IP geographical location as shown in the following screenshot.
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.
Get Linux Server Location Details
You might also like to read these following related articles:
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.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Related Posts
8 thoughts on “How to Find Linux Server Geographic Location in Terminal”
gip -g prints the geo location for the IP address.
The tool is available at GitHub: https://github.com/softhub-software-development/gip .
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:
I’ll be adding more parsers to make life easier for cli jockeys!
Thanks for sharing, we are grateful.
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.
[email protected]:/home/jeffs $ curl -4 -s «https://api.ipgeolocation.io/ipgeo?apiKey=$
97.126.67.233
[email protected]:/home/jeffs $ curl -6 -s «https://api.ipgeolocation.io/ipgeo?apiKey=$
2602:61:7e43:e900:58fd:e794:22b1:e919
Likewise, I can get the information about somebody else’s IPv4 or IPv6 address:
[email protected]:/home/jeffs $ curl -4 -s «https://api.ipgeolocation.io/ipgeo?apiKey=$
33.91670
-117.90000
[email protected]:/home/jeffs $ curl -6 -s «https://api.ipgeolocation.io/ipgeo?apiKey=$
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.
Many thanks for sharing this useful information as well as your thoughts about this topic, with us.
You can also pass the -r option to jq to get rid of the quotation marks. No need for sed! 🙂
You can also find IP by using:
and if GeoIP is installed:
Gives you the country name and info about your ISP.
If you specify location of GeoLiteCity.dat file:
You get the rest (city and coordinates).
Thanks for sharing, we are grateful.
Got something to say? Join the discussion. Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Источник
Navigating Your File System in Linux
This chapter is from the book
This chapter is from the book
This chapter is from the book
This chapter introduces the basic commands you’ll find yourself using several times every day. Think of these as the hammer, screwdriver, and pliers that a carpenter keeps in the top of his toolbox. After you learn these commands, you can start controlling your shell and finding out all sorts of interesting things about your files, folders, data, and environment. In particular, you’ll be learning about some of the metadata—the data describing your data—that Linux has to keep track of, and it may just surprise you how much there is.
When I updated this book for its second edition, I removed the section about mkdir -v (which shows you what mkdir is doing as it does it) and rm -v (which does the same thing, but for rm). You can find the original text on my website, www.granneman.com/linux-redactions.
Also, I took the sections on touch, mkdir, cp, mv, rm, and rmdir and used them to create a new Chapter 3 titled “Creation and Destruction” (which of course renumbered everything after it!). Finally, the section on su was moved to Chapter 8, “Ownership and Permissions,” which makes a lot more sense.
List Files and Folders
The ls command is probably the one that people find themselves using the most. After all, before you can manipulate and use files in a directory (remember, file and directory are interchangeable), you first have to know what files are available. That’s where ls comes in, as it lists the files and subdirectories found in a directory.
The ls command might sound simple—just show me the files!—but there are a surprising number of permutations to this amazingly pliable command, as you’ll see.
Typing ls lists the contents of the directory in which you’re currently working. When you first log in to your shell, you’ll find yourself in your home directory. Enter ls, and you might see something like the following:
Источник
How can I get the current working directory? [duplicate]
I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd . It just prints pwd how do I get the current working directory into a variable?
5 Answers 5
There’s no need to do that, it’s already in a variable:
The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:
Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than
bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.
For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):
Of the two, the $() is preferred since it is easier to build complex commands like:
Источник
How To Get Your Geolocation From Commandline In Linux
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:
On Debian, Ubuntu, Linux Mint, run:
On Arch Linux and its derivatives:
Once npm installed, run the following command to install «whereami» tool.
You will get an output something like below.
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.
Here is my output:
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:
Sample output:
To know more details about whereami command, run:
Sample output:
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.
Источник