Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.)

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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|improve this answer
    
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 Dec 2 '12 at 23:25
    
@Dominick I updated my answer accordingly. What distribution and what major-version are you using? –  Nils Dec 3 '12 at 21:42
    
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? –  Dominick Dec 4 '12 at 0:52
    
@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) –  Nils Dec 4 '12 at 21:29
add comment

Not to offend you, but if you are familiar with the routing portion of your TCP/IP communications stack on any linux machine, you should also know that, the results of connecting two network interfaces to the same network, is, unpredictable at best. If I were you I would either disable the wireless on my laptop or run an ifconfig eth0 down command (assuming eth0 is your wired ethernet connection) to shut it off. Linux might have some intelligence to make the same selection for every packet, but in case of a network hiccup at some point, may result in some unexpected results. My 2 cents.

share|improve this answer
    
It's not unpredictable. First hit in routing table is used as explained by @Nils. –  gertvdijk Dec 2 '12 at 21:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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