Finding your public IP address is handy when you want to run a server at home to share with people on the internet (website hosting, file sharing, remote control, etc…).

There are many ways to get it, but most of the time, they rely on reading information from a website such as What Is My IP, even a google search will return your address.

However, if you need to get it programmatically, the easiest way on Mac OS X is probably to use a UNIX command in the Terminal. The concept of public IP address becomes useless if you are not connected to the internet. Hence for this trick to work you will need:

  • To be connected to the internet (with no proxy)
  • To know a hostname or IP address on the web that you know would always be live online (you cannot use a local address or host)

The command to use in the Terminal is:
ping -R -c 1

Refer to man ping for more details, but -R triggers the routing trace option and -c 1 makes ping use only 1 packet so the command returns as quickly as possible.

This returns something like:

PING ( 56 data bytes
64 bytes from icmp_seq=0 ttl=58 time=61.766 ms
RR: ( ( ( ( ( ( (

--- ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 61.766/61.766/61.766/0.000 ms

The first IP address returned is the IP address of the hostname you used. Your public IP is the second IP address listed or the first one on the line starting with “RR: “. In the above example, that is

Alternatively, you could subscribe to a Dynamic DNS service and install a software that updates your IP address as it changes. That way, your server becomes accessible from the internet using a static hostname.