Different distros of Linux, Unix, some versions of Microsoft Windows, and many other Operating Systems are not limited to using just one network interface to reach the internet.
Many Operating Systems will detect more than one valid interface, capable of reaching the internet, and set up each of them for carrying traffic to the internet (specifically, gateways that ultimately connect to the internet).
If the OS tries to reach an external network and succeeds using eth0
, it will add that interface to the routing table and tie it to that network.
If the OS tries to reach the same external network
and also succeeds using eth1
,
it will also add that interface (eth1
) into the routing table
as an additional way to reach that same network.
Other posters thus far have not considered the importance of the metric values in a routing table.
My routing table, below, shows two different interfaces, eth0
and wlan0
.
Both are up, both have been set automatically by the operating system as default routes to the gateway 192.168.1.1
,
and both have had a route built automatically by the operating system
to the 192.168.1.X
(i.e., 192.168.1.0/24
) network.
This entire routing table was written automatically by the OS.
No edits to it were made by me. The difference shown here is that
the wired ethernet interface (eth0
) has a lower metric (202),
and thus more of my traffic to nodes beyond my own network
will be routed through this interface (it is preferred by the lower metric),
while the remainder of my traffic to nodes beyond my network
will be routed through the wireless interface (wlan0
)
(it has a higher metric of 303, and thus is less preferred by the OS).
pi@raspberrypi:~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.1 0.0.0.0 UG 202 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 303 0 0 wlan0
192.168.1.0 0.0.0.0 255.255.255.0 U 202 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 303 0 0 wlan0
Both interfaces are being used to reach "the internet", and their use is weighted by the "Metric" values, as can be seen in the bytes and packets statistics for eth0
and wlan0
below:
pi@raspberrypi:~ $ ip -s address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:a6:32:31:a2:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.195/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
valid_lft 80787sec preferred_lft 69987sec
inet6 fe80::2f3f:3f1d:8c35:a05e/64 scope link
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
14341060 22393 0 0 0 971 ←—— high
TX: bytes packets errors dropped carrier collsns
1190274 10745 0 0 0 0 ←—— high
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether dc:a6:32:31:a2:c8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.193/24 brd 192.168.1.255 scope global dynamic noprefixroute wlan0
valid_lft 80787sec preferred_lft 69987sec
inet6 fe80::4f31:5fcf:8f70:b5ca/64 scope link
valid_lft forever preferred_lft forever
RX: bytes packets errors dropped overrun mcast
4963408 7954 0 0 0 929 ←—— lower
TX: bytes packets errors dropped carrier collsns
49371 235 0 0 0 0 ←—— lower
As the annotations indicate, eth0
is being given more traffic by the OS.
Many Operating Systems will give a lower metric to a wired interface that has a faster connection,
and a higher metric to a wired interface with a slower connection.
For example, if eth0
and eth1
both connect to the same node,
but eth0
negotiated a 100 Mbs connection,
and eth1
negotiated only a 10 Mbs connection,
eth0
would be given a lower metric than eth1
.
Similarly, many Operating Systems will give a lower metric to a wired interface, and a higher metric to a wireless interface.
More than one interface can be set up automatically (depending on the OS),
or manually, to reach the same external node by making/editing entries to the routing table.
A minimally tested script to find the interface with the lowest metric
as reported by route
: [Added by G-Man]
route | awk '
BEGIN { min = -1 }
$1 == "default" {
if (min < 0 || $5 < min) {
min = $5
iface = $8
}
}
END {
if (iface == "") {
print "No \"default\" route found!" > "/dev/stderr"
exit 1
} else {
print iface
exit 0
}
}
'
You might want to extend this to look at the "Flags"; i.e., $4
.