Quickly Get an External IP Address from the Command Line

Jul 16, 2013 - Leave a Comment

External IP address example

Need to quickly get your external IP address from the command line for SSH or otherwise? No sweat, you can use either the curl command or dig to extract the information quickly from a variety of sources. We’ll focus on two different options that have proven to be reliable over time, the first is quite short and easy to remember, but the latter option may be considered the most reliable.

curl ifconfig.me

Or use the following:

dig +short myip.opendns.com @resolver1.opendns.com

The response with either string will be nothing but your external IP address, which is what the outside world sees, different from your LAN IP.

The dig command string is obviously quite a bit longer, but OpenDNS is a very reliable service that does much more than just retrieving IP information, so it may be considered a more reliable longterm solution, and thus it’s probably what is best to base any shortcuts or scripts on.

If you find yourself frequently needing your external IP, create a bash alias out of the aforementioned dig command, or you can turn to the menubar with the help of a free third party app that does nothing but keep your external IP address in the menu bar of OS X.

To create a quick IP retrieval command with using bash aliasing, paste the following line into your .bash_profile:

alias getmyip='dig +short myip.opendns.com @resolver1.opendns.com'

Save changes to .bash_profile, and now you will only need to type ‘getmyip’ to use the entire lengthy command string. Heads up to CommandLineFu for the dig string.

Note that you can also go to various websites like ifconfig.me and whatismyip.org through any web browser, be it lynx from the terminal, or Safari and Chrome in the GUI, to get an external IP that way as well. These commands will work the same in Mac OS X or Linux, whereas the browser based approach will work in quite literally anything capable of running a web browser.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates