I'm sure this has been asked and answered before, but I wasn't able to find it, so hopefully this will at least link someone to the right place.

I want to find out my local interface and ip address used to reach a certain host. For instance, if I had 3 adapters connected to my box and they all three went to different networks, I'd like to know which of the three (specifically, its ip address) is used to reach my.local.intranet (in this case, it would be a vpn tunnel interface). I suspect this is a job for ifconfig or traceroute, but I haven't been able to find the correct switches.

I'm running OSX 10.7 (Darwin)

EDIT: For easy access, This is what I ended up writing to get the trick done:

ifconfig `route get <host> | grep "interface: " | sed "s/[^:]*: \(.*\)/\1/"` | grep "inet " | sed "s/.*inet \([0-9.]*\) .*/\1/"

Any improvements / optimizations are welcome.


You can try "route" to print you're routing table which will show you the destination networks and gateways for your various interfaces.

If the IP addresses don't help you determine the interface easily, run "traceroute," after running route, look at the first hop and compare it with the associated gateways from "route," and voila - that's your interface.

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface      switch47     U     0      0      eth0     localrouter     U     0      0      eth1

#traceroute <destination>
1    switch47.ba.foo.com   (    0.5 ms  .....
2    xxxxx.xxx.xxxx        (xxx.xxx.xxx.xxx)   0.Xms. ...
| improve this answer | |
  • 2
    I did end up using route. Thank you. Here was my final command: ifconfig `route get <host> | grep "interface: " | sed "s/[^:]*: \(.*\)/\1/"` | grep "inet " | sed "s/.*inet \([0-9.]*\) .*/\1/". This works, but any suggestions / optimizations are welcome. – umop Jun 22 '12 at 21:14
  • Ahh ok, I was just gonna say, I would really recommend mgorven's approach listed here, but if you don't have ip on Mac OS, I'm glad this helps. – Univ426 Jun 22 '12 at 21:22

route -n get www.yahoo.com

I am running os x 10.6.8

| improve this answer | |
  • That's what I was looking for when I got here. Good job. – Amos Shapira Aug 19 '16 at 21:14

You can use ip route get to find and output the route which would be used for a specified destination. The output should include the interface and source address.

% ip route get via dev eth1  src
| improve this answer | |
  • 1
    I'm sorry. I forgot to specify the OS. I'm running OSX. No ip on here. =( – umop Jun 22 '12 at 21:05

I'm not sure if this is correct, but netstat might do it. It shows local address of established connections.

# netstat -nt
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0        ESTABLISHED
tcp        0      0       ESTABLISHED
| improve this answer | |
  • This is the correct definitive way to determine which local IP was used. – bahamat Jun 22 '12 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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