Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

up vote 9 down vote accepted

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

share|improve this answer

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.

share|improve this answer

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

alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"
share|improve this answer

Just type curl ifconfig.me in the terminal.

share|improve this answer
    
In a terminal window, after the $ sign, do: curl ipecho.net/plain; echo –  user70348 Feb 14 at 3:15
    
Why the downvote? Although it is an outside IP, the command is valid and works for my ssh logins. –  CousinCocaine Sep 30 at 17:23

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.

share|improve this answer

The following works for me on 10.8

ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' 
share|improve this answer

protected by Community Sep 30 at 18:03

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.