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 nbalkota.wordpress.com

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 e3191.dscc.akamaiedge.net (2.18.125.15): 56 data bytes
64 bytes from 2.18.125.15: icmp_seq=0 ttl=58 time=61.766 ms
RR: host-78-150-157-159.as13285.net (78.150.157.159)
host-78-150-144-1.as13285.net (78.150.144.1)
10.160.1.3
loop0-rt001.man.as13285.net (62.24.240.248)
talktalk-ic.lon2.uk.atrato.net (80.94.66.14)
eth1-1.r1.lon2.uk.atrato.net (78.152.44.163)
eth1-3.core1.lon1.uk.atrato.net (78.152.44.102)
78.152.40.32
a92-122-210-1.deploy.akamaitechnologies.com (92.122.210.1)

--- e3191.dscc.akamaiedge.net 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 78.150.157.159

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.