Network interface preference

8,725

Well - this IS a routing-question.

The answer is simple: The first entry that will give the best routing-entry is "the winner". So look at netstat -rn to see which interface is first.

Update: The network-inferface-routing-settings normally get set up during system startup. So the startup order of network devices will be the order in that table. With PCI-devices these normally are being processed by lowest slot-number first. External devices (e.g. USB) normally come later on.

But the exact order depends on the network-startup-script of your Linux flavour (this is something where they very much differ).

BTW: If you want to make use of your two links, you should go for bonding. There you can set up the order of usage for your links.

Share:
8,725

Related videos on Youtube

Dominick Pastore
Author by

Dominick Pastore

Updated on September 18, 2022

Comments

  • Dominick Pastore
    Dominick Pastore over 1 year

    How does Linux determine which network interface to use, when both are connected to the same network?

    Note that this isn't a question on routing. I'm familiar with how that works. This is if, say, I have my laptop connected to my wireless router through both my ethernet card and my wireless card, or if I have two ethernet cards both connected to the same router.

    I can say from experience that in my case, my laptop seems to favor the ethernet card (eth0) over the wireless (eth1--I know that's not a typical name for a wireless interface, but that's what I have), but I was wondering, how does it decide that? If it just picks from the lowest numbered interface, what if the two choices are, say, eth0 and wlan0?

    Edit: @Nils has pointed out that this is still a matter of routing, and the routing table provides the answer (see his answer). This still leaves my original question, but in a different form. What determines the order of entries in the routing table in Linux? For example, here is my routing table while connected to both interfaces:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    0.0.0.0         192.168.4.1     0.0.0.0         UG        0 0          0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
    192.168.4.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
    192.168.4.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1
    

    It's clear that eth0 is higher priority in the table than eth1 for destinations in the local network, but is that decided in Linux from link latency, link throughput, even the interface name, or what? (The same question could go for why eth0 is the interface for the default route.)

  • Dominick Pastore
    Dominick Pastore over 11 years
    That makes sense. When I said routing, what I meant was it wasn't simply a matter of picking which interface is connected to the destination network (directly or indirectly), since both are. I should have been more careful with my terminology. Thanks for the helpful tips (especially about bonding). This still leaves my original question, though, in a slightly different form. How is the order of routing entries determined in Linux (I edited the original question)?
  • Dominick Pastore
    Dominick Pastore over 11 years
    Ah. I didn't realize it could vary so much between distributions. I looked all over for information on this, but all I found were articles on how to set routing entries, not how the original ones are determined. I'm using Xubuntu 12.04. This does raise one other question (small though; I feel very enlightened from your answer :-) ). If I'm connected via wireless (eth1) and later connect via Ethernet (eth0), the default route and top entries in the routing table go back to eth0. This seems to go against your answer. Am I misunderstanding, or is that a NetworkManager idiosyncrasy, or what?
  • Nils
    Nils over 11 years
    @Dominick Ubuntu is very special. It does some magic stuff in the background when plugging/unplugging network devices. I guess it restarts the whole network stack there. (I normally use SLES/RH and am not too deep into Ubuntu)