Get IP Address from Command Line

By  on  

Over the past few years I've needed to look up my IP address a dozen times.  What's funny is that I don't recall why I needed it, I just did.  So what's the easiest way to get my IP address from the command line?

To get your internal IP, you can do the following:

ipconfig getifaddr en0

I go through ipecho.net to get it:

curl ipecho.net/plain ; echo

#  >> 71.87.32.78

Why go through an external resource?  Your local address and your external address aren't the same, so using the external resource is the best way to tell what the world is seeing your IP address as.

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    MooTools Text Flipping

    There are lots and lots of useless but fun JavaScript techniques out there. This is another one of them. One popular April Fools joke I quickly got tired of was websites transforming their text upside down. I found a jQuery Plugin by Paul...

  • By
    CSS Ellipsis Beginning of String

    I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to...

Discussion

  1. ifconfig | grep mask
    
  2. ElanMan

    I prefer the slightly simpler/easier to remember

    curl ifconfig.me
    
  3. nedt

    ipinfo.io can give you even more information and is easier to use via tunneling as it works without a host header.

  4. MaxArt

    Windows users can use ipconfig (notice the ‘p’).

  5. Your internal IP also depends on interface you’re using, in your case (en0) it was ethernet, but it also can be WiFi (en1).

  6. ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
  7. Some time ago I started using an alternative which only uses DNS queries:

    dig +short myip.opendns.com @resolver1.opendns.com
  8. Google > “My IP” = Winner!

  9. $ curl ipinfo.io
    {
      "ip": "174.47.27.98",
      "hostname": "174-47-27-98.static.twtelecom.net",
      "city": "Orange",
      "region": "California",
      "country": "US",
      "loc": "33.7878,-117.8531",
      "org": "AS4323 tw telecom holdings, inc.",
      "postal": "92666"
    }
    
    $ curl ipinfo.io/ip
    69.170.45.10
    
    $ curl ipinfo.io/org
    AS4323 tw telecom holdings, inc.
    

    See http://ipinfo.io/developers for more details

  10. I just search Google for “what is my ip” like a n00b.

  11. I prefer:

    alias publicip='curl ipecho.net/plain ; echo'
    alias localip="ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'"
  12. hahaha, i just think to google it :D, but thanks dude, sometimes need to apply to my terminal command

  13. It is ugly. But the Google lovers will enjoy.

    Prerequisite: curl, and grep.

    curl -s "https://www.google.ie/search?client=firefox-b&q=myip&oq=myip&gs_l" | grep "Client IP address" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!