How can I tell which network interface my computer is using?
The definitive reference for questions such as 'Which interface will be used to reach host w.x.y.z?' for any given time or situation is to refer to the routing table.
[mini-nevie:~] nevinwilliams% route get 10.10.10.10
route to: 10.10.10.10
destination: default
mask: default
gateway: 192.168.2.1
interface: en0
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
0 0 0 0 0 0 1500 0
I've both en0 and en1 enabled, and en0 is first in Service Order. Unplugging my ethernet, making no other changes, the same command gives:
mini-nevie:~] nevinwilliams% route get 10.10.10.10
route to: 10.10.10.10
destination: default
mask: default
gateway: 192.168.2.1
interface: en1
flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire
0 0 0 0 0 0 1500 0
On Mac, I use this:
if=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
if [ -n "$if" ]; then
echo "Default route is through interface $if"
else
echo "No default route found"
fi
On Linux, the first line would be slightly different:
if=$(ip route show 0.0.0.0/0 | awk '/ dev / {print $NF}')
# or
if=$(awk '$2 == 00000000 {print $1}' /proc/net/route)
Here's a dump of ifconfig -a
in each situation (I'll highlight the differences afterward):
Ethernet cable is plugged in:
lo0: flags=8049 mtu 16384 inet6 ::1 prefixlen 128 inet6 xxxx::1%lo0 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff000000 gif0: flags=8010 mtu 1280 stf0: flags=0 mtu 1280 en0: flags=8863 mtu 1500 inet6 xxxx%en0 prefixlen 64 scopeid 0x4 inet 192.168.0.110 netmask 0xffffff00 broadcast 192.168.0.255 ether xx:xx:xx:xx:xx:xx media: autoselect (100baseTX ) status: active supported media: none autoselect 10baseT/UTP 10baseT/UTP 10baseT/UTP 10baseT/UTP 100baseTX 100baseTX 100baseTX 100baseTX 1000baseT 1000baseT 1000baseT fw0: flags=8863 mtu 4078 lladdr xx:xx:xx:xx:xx:xx media: autoselect status: inactive supported media: autoselect en1: flags=8863 mtu 1500 inet6 xxxx%en1 prefixlen 64 scopeid 0x6 inet 192.168.0.110 netmask 0xffffff00 broadcast 192.168.0.255 ether xx:xx:xx:xx:xx:xx media: autoselect status: active supported media: autoselect
Ethernet cable is unplugged:
lo0: flags=8049 mtu 16384 inet6 ::1 prefixlen 128 inet6 xxxx::1%lo0 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff000000 gif0: flags=8010 mtu 1280 stf0: flags=0 mtu 1280 en0: flags=8863 mtu 1500 ether xx:xx:xx:xx:xx:xx media: autoselect status: inactive supported media: none autoselect 10baseT/UTP 10baseT/UTP 10baseT/UTP 10baseT/UTP 100baseTX 100baseTX 100baseTX 100baseTX 1000baseT 1000baseT 1000baseT fw0: flags=8863 mtu 4078 lladdr xx:xx:xx:xx:xx:xx media: autoselect status: inactive supported media: autoselect en1: flags=8863 mtu 1500 inet6 xxxx%en1 prefixlen 64 scopeid 0x6 inet 192.168.0.110 netmask 0xffffff00 broadcast 192.168.0.255 ether xx:xx:xx:xx:xx:xx media: autoselect status: active supported media: autoselect
diff plugged unplugged
shows that the wired connection does become active when it is available:
8,9d7
< inet6 xxxx%en0 prefixlen 64 scopeid 0x4
< inet 192.168.0.110 netmask 0xffffff00 broadcast 192.168.0.255
11c9
< media: autoselect (100baseTX <full-duplex,flow-control>) status: active
---
> media: autoselect status: inactive