Freelance web and mobile developer - Edinburgh, UK

Get internal and external IP address on Mac with Bash

February 13, 2015

Frequently while coding I need to know the internal and external IP address of my development machine. For example, when your machine is serving a website from a locally running web server, the internal IP address can be used to test the site from a smartphone browser connected to the same network. Likewise, the external IP address is useful when you want to make a local web server publicly accessible or when you need to whitelist your IP address in the firewall settings of an external server.

To help with the above, I wrote a simple Bash function that will list the internal IP address for active wifi and ethernet connections as well as the external IP address on a Mac (tested on OS X Yosemite). When you run the function from the terminal, you’ll see something like this:

[email protected]:~$ myip
Internal IP address (wired): none
Internal IP address (wifi):  192.168.0.2
External IP address:         41.52.154.137

To make this function available from the command-line, add the following to your .bash_profile:

myip() {
  external_ip=`dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null || echo none`

  echo "Internal IP address (wired): "`ipconfig getifaddr en1 || echo none`
  echo "Internal IP address (wifi):  "`ipconfig getifaddr en0 || echo none`
  echo "External IP address:         $external_ip"
}
Looking for an experienced full stack developer for your web or mobile project?
I'm available for contract work in Edinburgh and can also work remotely.