DEV Community

ishant singh
ishant singh

Posted on Originally published at ipcomet.com AI-assisted

How to Get Your Public IP Address From the Command Line

The quickest way to get your public IP address from a terminal is curl https://ipcomet.com. It prints your address and nothing else, so the output can go straight into a script. This guide shows the same one-liner on macOS, Linux and Windows, how to do it from Python and Node.js, how to get your location and ISP as JSON, and how to choose between IPv4 and IPv6.

The one-line answer

curl https://ipcomet.com
Enter fullscreen mode Exit fullscreen mode

The output is a single line such as 203.0.113.7. The home page recognises command-line clients (curl, wget, PowerShell and similar) by their User-Agent and returns just the address instead of the web page. In scripts, use the explicit endpoint https://ipcomet.com/api/ip, which always returns plain text.

macOS and Linux

curl https://ipcomet.com/api/ip
wget -qO- https://ipcomet.com/api/ip
Enter fullscreen mode Exit fullscreen mode

macOS includes curl but not wget. To keep the result in a shell variable, use MYIP=$(curl -s https://ipcomet.com/api/ip). The -s flag hides curl's progress meter.

Windows

Windows 10 and 11 ship with curl, so curl.exe https://ipcomet.com/api/ip works in both Command Prompt and PowerShell. Type curl.exe rather than curl in PowerShell, where curl can be an alias for Invoke-WebRequest. The native PowerShell way is:

(Invoke-RestMethod https://ipcomet.com/api/ip).Trim()
Enter fullscreen mode Exit fullscreen mode

Python

With the requests library:

import requests
print(requests.get("https://ipcomet.com/api/ip", timeout=10).text.strip())
Enter fullscreen mode Exit fullscreen mode

With only the standard library, set a User-Agent. Python's default one (Python-urllib) is rejected by the bot protection in front of many sites, ipcomet included, which answer with a 403 error (Cloudflare error 1010):

import urllib.request

req = urllib.request.Request("https://ipcomet.com/api/ip", headers={"User-Agent": "my-script/1.0"})
print(urllib.request.urlopen(req, timeout=10).read().decode().strip())
Enter fullscreen mode Exit fullscreen mode

JavaScript and Node.js

const ip = (await (await fetch("https://ipcomet.com/api/ip")).text()).trim();
console.log(ip);
Enter fullscreen mode Exit fullscreen mode

This works in Node.js 18 and later, which has fetch built in, and in a browser page, because the endpoint allows cross-origin requests.

Get your location and ISP as JSON

curl https://ipcomet.com/api/me
Enter fullscreen mode Exit fullscreen mode

The response describes the connection that made the request. An example, with made-up values:

{
  "ip": "203.0.113.7",
  "version": 4,
  "city": "Mumbai",
  "region": "Maharashtra",
  "country": "India",
  "countryCode": "IN",
  "timezone": "Asia/Kolkata",
  "asn": 64500,
  "org": "Example Broadband Ltd"
}
Enter fullscreen mode Exit fullscreen mode

The full response also has the postal code, latitude and longitude, and a few details about the connection (the edge location, the HTTP and TLS versions and the round-trip time). To pick out one field, pipe it through jq: curl -s https://ipcomet.com/api/me | jq -r .country.

To look up any other address, use curl "https://ipcomet.com/api/lookup?q=8.8.8.8". It returns the IP version, hostname, city, region, country, coordinates, timezone, ASN and network owner. Lookups are limited to 60 a minute per caller, and location data is approximate because it comes from network records, not GPS.

Force IPv4 or IPv6

If your connection has both, add -4 or -6 to curl: curl -4 https://ipcomet.com forces IPv4, and curl -6 https://ipcomet.com forces IPv6, which only works if your connection has IPv6.

Other services you will see recommended

  • curl ifconfig.me, curl icanhazip.com, curl api.ipify.org and curl ipinfo.io/ip are other free services that return a bare address in the same way.
  • Without HTTP, you can ask a DNS resolver instead: dig +short myip.opendns.com @resolver1.opendns.com.

They all report the address the internet sees, so they should agree.

Why do two services show different IP addresses?

If they differ, your router may be sharing traffic across two internet connections, or a VPN may be carrying only some of your traffic.

These commands return your public address, the one websites see. For the private address on your own network, such as 192.168.1.20, check your network settings or run ipconfig on Windows and ip addr on Linux.

Limits and privacy

No key or sign-up is needed. Please keep requests reasonable: checking every few minutes is fine, but calling it in a tight loop may be rate limited. ipcomet does not store or log the addresses it sees. The free IP API page lists every endpoint with its exact rate limit.


This guide first appeared on ipcomet, a free IP checker I maintain.

Top comments (0)