Find your IP from the command line with curl

Get your public IP address and its location in a terminal with curl host.sh: plain text fields, JSON, IPv4 or IPv6, over HTTP or HTTPS, no key needed.

$ curl host.sh/ip
…
$ curl host.sh/city
…
$ curl host.sh/country_code
…

What these commands print for the connection this page uses. curl may use IPv4 where your browser uses IPv6, or the other way round.

The commands

curl host.sh              # your IP details as JSON
curl host.sh/json         # the same
curl host.sh/ip           # just your IP address
curl host.sh/city         # one field as plain text
curl host.sh/country_code # e.g. PT
curl host.sh/help         # usage and every field

They need no key and no sign-up, and they work over plain HTTP and HTTPS. wget -qO- host.sh/ip works too, as do HTTPie (http host.sh/ip) and PowerShell (Invoke-RestMethod https://host.sh/ip). A browser that opens host.sh is sent to this website instead.

Fields

Any of these works as curl host.sh/<field> and prints the value as plain text: ip, hostname, city, region, region_code, country, country_code, continent, continent_code, latitude, longitude, time_zone, postal, offset, isp, asn, asn_org, org, flag, is_eu, currency, calling_code, utc_offset, local_time. An unknown field prints the list. curl host.sh prints all of them as one flat JSON object.

IPv4 or IPv6

curl -4 https://host.sh/ip   # your IPv4 address
curl -6 https://host.sh/ip   # your IPv6 address

Without -4 or -6, curl uses IPv6 when your connection has it. Prefer https:// with -4 and -6: on some networks plain HTTP passes through a transparent proxy, which may connect to host.sh over the other family. The IPv6 test shows both addresses in the browser.

In scripts

curl
ip=$(curl -s host.sh/ip)
echo "My IP is $ip"
curl
curl -s host.sh/json | jq -r '.city + ", " + .country'

host.sh keeps the answer for your IP for an hour, so repeated calls from a script are answered from that copy. For other addresses, batches, languages and every field of the full answer, use the API: curl https://api.host.sh/v2/lookup/ gives your own IP as the full JSON answer without a token.

FAQ

How do I find my IP address from the command line?

Run curl host.sh/ip. It prints your public IP address and nothing else.

Does curl host.sh need an API key?

No. It looks up only your own address and needs no key or sign-up.

How do I get my IPv4 address when I have IPv6?

Run curl -4 https://host.sh/ip. The -4 flag makes curl connect over IPv4.

Can I look up another IP address with curl?

Not with curl host.sh, which answers about your own address only. The lookup API looks up any address with a token: curl -H "Authorization: Bearer YOUR_TOKEN" https://api.host.sh/v2/lookup/8.8.8.8.

This page as Markdown: /curl/index.md