How does Linux pick which network interface to use?

Let's say I have a machine with two network adapters on different subnets, eth0 (10.0.100.5) and eth1 (10.0.200.5). Assuming both subnets have routes to the internet (though their own NATs) and to the rest of the local network (though their own switches):

  1. How will Linux choose which network interface to use when making a request to 10.0.150.5, or 173.194.43.102?
  2. Is there a way I can influence this at the network level? Would removing the routes from the subnets influence this? I'm in an AWS environment and working with an Appliance AMI that I can't log into, so something like this won't work.

AWS VPC environment, other machines running Ubuntu 12.04, no idea what the machine I'm trying to influence is running (since I can't log into it).

Clarifications:

  • Both subnets are set up as /24s

Subnet is defined by network and mask, e.g.

network : 10.0.100.5 mask : /8

This means your subnet is : 10.0.100.5/8

Basically any valid IP within the 10.xxx.xxx.xxx family belongs to this subnet. Only one interface can be used to access hosts in this subnet. So, in your case, either eth0 or eth1 will be used. If other interfaces have IP addresses in same subnet, traffic won't be routed properly.

Here is what would happen if interfaces eth0 and eth1 are each connected to the same network with IP addresses on the same subnet.

  • eth0 10.0.100.5/8
  • eth1 10.0.200.5/8

Now only one of those interfaces has route (let's say it's eth0) which says to access subnet 10.0.100.0/8 use eth0. So packets leaving this interface will have source address 10.0.100.5

! NOTICE

  • 10.0.100.5/8 and 10.0.200.5/8 are the same subnet (because mask masks last 3 octets, we can also write it as 10.0.0.0/8)

So now consider we have a host on this subnet which attempts to communicate with 10.0.200.5 Packet enters system through eth1 but can't return via eth1 since eth0 is the only interface with a route to network 10.0.0.0/8 and responses from eth0 have different IP; so the one initiating connection to 10.0.200.5 will not be able to understand response coming from some other source.

EDIT

To determine which interface will be used you need to look at your routing table. The question does not contain a routing table so I'll try to explain with following hypothetical routing table

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 wlan0

alternative routing table output

$ ip route show
default via 192.168.0.1 dev wlan0  proto static 
10.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.5 
192.168.0.0/24 dev wlan0  proto kernel  scope link  src 192.168.0.14 

So we have 2 interfaces in example above.

  • eth0 with IP 10.0.0.5 on subnet 10.0.0.5/255.255.255.0 or in CIDR notation 10.0.0.5/24
  • wlan0 with IP 192.168.0.14 on subnet 192.168.0.14/255.255.255.0 or in CIDR notation 192.168.0.0/24

Now what routing table tells us; line by line interpretation, bottom up:

192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.14

To access hosts in subnet 192.168.0.0/24 use interface wlan0 and src IP 192.168.0.14

10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.5

To access hosts in subnet 10.0.0.0/24 use interface eth0 and src IP 10.0.0.5

default via 192.168.0.1 dev wlan0 proto static

To access any other host not covered by rules above use default route, which is reachable using interface wlan0 and gateway to other networks is 192.168.0.1