How to obtain the external IPv4 address via Terminal

I'm trying to figure out how to obtain my external IP (IPv4) via Terminal without connecting to a remote website or service.

Here's what I've found so far - these all use remote/external checking:

  1. curl ifconfig.me
  2. curl icanhazip.com
  3. curl -s checkip.dyndns.org | grep -Eo '[0-9.]+'
  4. dig +short myip.opendns.com @resolver1.opendns.com

Is it even possible without checking some external source?


It is not possible without checking an external source.

In a typical networking environment, nothing on your computer knows the external IP address the computer is using to communicate with the public internet. Instead, the computer has a private IP address and sends all traffic to the private IP address of a router which then forwards the traffic the the public internet using a public IP address.

The only reliable way to determine the public IP address associated with your computer's traffic is to contact an external source and have it tell you what IP address it sees your traffic coming from.


The external IP address you seek is really an arbitrary network router somewhere upstream of your connection, so your proposed solutions are all valid in needing an external device to confirm the external address.

If your Mac has a true routable internet address, then it is "external" already whether or not your routers use NAT or not.

To know about external routing details, you need to have access to the routers or to another computer that will run a traceroute or otherwise log the source IP address it needs to send responses to for the packets that originate from your Mac.