140

I know you can do ifconfig | grep inet, but that shows you several IPv4 addresses. How do I get the specific one for SSHing et al?

  • What you are looking for is not your Mac IP address but the public IP address your ISP attributed to the Internet interface of your router. – daniel Azuelos Feb 12 at 11:26
142

Use ipconfig getifaddr en1 for wireless, or ipconfig getifaddr en0 for ethernet.

  • 7
    This shows my internal (to my router) IP address (e.g. 192.168.1.xxx), not the external IP address. (As do most of the other answers below.) – ShreevatsaR May 5 '15 at 22:38
  • 2
    Didn't work for me :( – Tough Guy May 13 '16 at 8:05
  • 2
    ifconfig shows all of the interfaces and their IPs – aralar Jul 7 '16 at 17:26
  • 3
    echo External IP: curl -s http://checkip.dyndns.org/ | sed 's/[a-zA-Z<>/ :]//g' – grigb Aug 16 '16 at 17:51
  • 26
    It has changed for years, 'ipconfig getifaddr en0' is default for wifi interface – Marek Szmalc Jan 31 '17 at 16:35
93

The following works for me on 10.8 and on 10.10 Yosemite.

ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' 

If you find the above gives you more than one answer, save the following to a script, and run it instead

ip_address.sh

#!/usr/bin/env bash

dumpIpForInterface()
{
  IT=$(ifconfig "$1") 
  if [[ "$IT" != *"status: active"* ]]; then
    return
  fi
  if [[ "$IT" != *" broadcast "* ]]; then
    return
  fi
  echo "$IT" | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
}

main()
{
  # snagged from here: https://superuser.com/a/627581/38941
  DEFAULT_ROUTE=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
  if [ -n "$DEFAULT_ROUTE" ]; then
    dumpIpForInterface "$DEFAULT_ROUTE"
  else
    for i in $(ifconfig -s | awk '{print $1}' | awk '{if(NR>1)print}')
    do 
      if [[ $i != *"vboxnet"* ]]; then
        dumpIpForInterface "$i"
      fi
    done
  fi
}

main
  • 2
    The should be the accepted answer in my opinion since it doesn't require any wired vs. wireless specification. – Erik Nomitch Jun 13 '17 at 15:14
  • @ErikNomitch except it gives me 2 with this answer – Zach Lysobey Jun 13 '17 at 15:31
  • Ah, I retract then. @ZachLysobey – Erik Nomitch Jun 13 '17 at 20:08
  • this is the best overall approach but one needs to sometimes do a bit more grepping before awk to get the right answer and the right answer might vary. – uchuugaka Sep 2 '17 at 11:17
  • I added a script that should hopefully address this issue.. fyi ! – Brad Parks Aug 31 '18 at 12:17
39

Just type curl ifconfig.me in the terminal.

  • 1
    Why the downvote? Although it is an outside IP, the command is valid and works for my ssh logins. – CousinCocaine Sep 30 '14 at 17:23
  • 5
    This is public IP address and what I was looking for. ipconfig getifaddr en0 is for local IP. – abriggs Oct 8 '15 at 19:13
  • On HighSierra I had to run this as SUDO. Besides this lists the external IP address. For the internal IP address see the answer from Brad Parks – Vincent Feb 4 '18 at 13:26
18

I've got this set up in an .aliases dotfile for frequent ip lookup:

alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"
17

You can do the following:

Type ifconfig or ifconfig -a. This command shows you the list of interfaces along with their IP and MAC addresses (the latter one only if applicable). You can also type ifconfig en0 or ifconfig en1 for the configuration of a particular interface only (as someone said in their answers, en0 is typically the wired Ethernet while en1 is the WiFi interface).

As an alternative, netstat -i will list all interfaces and will show you the IP addresses you have assigned to each of them.

Typically, when you have SSH daemon running on a box, it will listen on all available interfaces, ie. you can use any IP address that's configured on your machine to connect to that machine via SSH (this, obviously, subject to Firewall rules). If you're after what the OS calls a Primary interface and primary IP address, you can use the scutil command like this:

MacBook:~ scutil
> show State:/Network/Global/IPv4
<dictionary> {
  PrimaryInterface : en0
  PrimaryService : C0550F84-5C07-484F-8D62-C8B90DC977D8
  Router : 10.103.4.1
}
> show State:/Network/Interface/en0/IPv4
<dictionary> {
  Addresses : <array> {
    0 : 10.103.4.234
  }
  BroadcastAddresses : <array> {
    0 : 10.103.4.255
  }
  SubnetMasks : <array> {
    0 : 255.255.255.0
  }
}

Please note, that the above, even though is a command-line command, is also interactive (so you run scutil and then enter its own commands into it). The first show command tells you the name of the primary interface for the OS (i.e. this will be the one on top of the list in your System Preferences / Network Preferences window), as well as the IP address of your default router. The second show command takes State:/Network/Interface/<ifname>/IPv4 argument (in this case, en0) and gives you the IP addresses assigned to it. You're looking for the address in the Addresses array, the other two entries are broadcast addresses and the netmasks.

Hope that helps, but if anything is not clear, let me know.

  • 1
    Thanks for this answer. I wrote a quick script to get the IP your primary interface: echo "show State:/Network/Interface/$(echo 'show State:/Network/Global/IPv4' | scutil | grep 'PrimaryInterface ' | sed 's/ PrimaryInterface : //')/IPv4" | scutil | pcregrep -Mo1 " Addresses : <array> {\n 0 : ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" – DC_ Jan 14 '15 at 19:09
4

To get the IP address of your computer facing the Internet, here is a working receipe:

if=`netstat -nr | awk '{ if ($1 ~/default/) { print $6} }'`
ifconfig ${if} | awk '{ if ($1 ~/inet/) { print $2} }'

It should work even when you have multiple interfaces active, even when you have interfaces you don't know which one is actually the default gateway.

  • This gets the internal network IP address (eg: 192.168.0.*), not the external one exposed to the internet. – SimplGy Apr 30 '15 at 5:30
  • Thank you for this feedback. Could you tell me on which version of the OS you are? Could you provide me the output of netstat -nr | grep default ? – daniel Azuelos Apr 30 '15 at 6:39
  • This gets the IP of the interface that serves in/outbound traffic. If you're behind NAT, that will give your LAN address, not WAN address. But it can be useful for a lot of purposes, and it doesn't reach out for network. – folex Jan 11 at 11:25
1

Just for the record, you can make a bash script with the following content which gives you your external IP address

#!/bin/bash    
wget -qO - http://ipecho.net/plain; echo
  • 3
    By default wget is not a part of macOS and needs to be installed from a non-Apple source. – user3439894 Jan 7 at 9:39
  • Is the question macOS specific ? it is by default installed on many linux distributions. – user702846 Jan 7 at 19:56
  • This site is about Apple hardware and software, so macOS and iOS is presumed. – grg Jan 7 at 20:47
  • The default macOS command for this would be by using curl: curl -s http://ipecho.net/plain; echo – thibmaek Feb 11 at 10:32

You must log in to answer this question.

protected by Community Sep 30 '14 at 18:03

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .