Skip to content

[Linux] How To Check Current IP Address And IP Region

Last Updated on 2024-08-04 by Clay

This article documents how to check your IP and its location on a Linux system via the terminal. While websites like myip provide this information directly, not all devices have a graphical interface. Moreover, the curl command works on both Windows and Mac OS.

Below, we'll explain how to obtain the IP address and then document how to determine its location.


Methods to Obtain IP Address

ip Command

The most straightforward method is to use the ip command:

ip -4 addr show | grep -oP '(?<=inet\s)\d+(.\d+){3}' | grep -v '127.0.0.1'


Output:

192.168.212.152
172.18.0.1
172.19.0.1
172.17.0.1



Query External Websites

curl "ifconfig.me"


Or

curl "icanhazip.com"

Methods to Determine Location

ipinfo.io

curl "ipinfo.io"


Output:

{
"ip": "203.0.113.1",
"hostname": "example.com",
"city": "Los Angeles",
"region": "California",
"country": "US",
"loc": "34.0522,-118.2437",
"org": "AS12345 Example ISP",
"postal": "90001",
"timezone": "America/Los_Angeles"
}


ip-api.com

curl "http://ip-api.com/json/"


Output:

{
"status": "success",
"country": "United States",
"countryCode": "US",
"region": "CA",
"regionName": "California",
"city": "Mountain View",
"zip": "94043",
"lat": 37.4192,
"lon": -122.0574,
"timezone": "America/Los_Angeles",
"isp": "Google LLC",
"org": "Google LLC",
"as": "AS15169 Google LLC",
"query": "8.8.8.8"
}


geoiplookup Tool

This tool can look up IP locations. Although some WiFi IPs cannot be resolved, it differs from the above two methods in that it can resolve other people's IPs. First, install the tool:

sudo apt install geoip-bin


Then query the IPs found on the local device (you might find many):

ip -4 addr show | grep -oP '(?<=inet\s)\d+(.\d+){3}' | grep -v '127.0.0.1'


Output:

192.168.212.152
172.18.0.1
172.19.0.1
172.17.0.1


Choose the IP that seems most like a public IP to query (but my example below is not):

geoiplookup 192.168.212.152

References


Read More

Tags:

Leave a Reply