Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

When a Linux box has two NICs, both connected to the same subnet, is there any way to tell which one is being used as the default gateway?

share|improve this question
    
The one with the default gateway. Regardless, this is hardly a question that fits the profile of the site. –  Chris McKeown Jul 19 '12 at 23:28
    
Seems I cant downvote myself. but "ip route ls" will do what you actually asked. I thought your question was actually interface/ip selection behaviour when speaking on the same subnet. I guess the answer can just stay for posterity. –  Matthew Ife Jul 19 '12 at 23:46

1 Answer 1

up vote 3 down vote accepted

There are a couple of conditions.

  • Both IP addresses are on the same subnet (like you already mentioned)
  • The socket being generated has not been bound specifically to a particular source IP programatically.
  • There are no advanced routing rules that switch behaviours based off of various other conditions (fwmark, dsmark etc).
  • The destination is to the same subnet.

In this case, the routing table is queried, so in my example, I have two IPs on the same subnet

# ip addr ls | grep inet
    inet 192.168.122.224/24 brd 192.168.122.255 scope global eth0
    inet 192.168.122.160/24 brd 192.168.122.255 scope global eth1

Which one to choose? Well the answer to this is dominated by the routes. So if we check that out..

192.168.122.0/24 dev eth0  proto kernel  scope link  src 192.168.122.224 
192.168.122.0/24 dev eth1  proto kernel  scope link  src 192.168.122.160 

Of course, if I have a default route for a destination that will be where I would go and it would be quite obvious. Normally the same routing table wont accept duplicate rules for destination networks.

The src field indicates which IP address to select for that subnet. But, hang on -- theres a problem here. I have two routes, one for each device both specifying the source! What to do?

Other factors in the route might weigh one over the other, the metric or preference for example.

Well, simply put if all else fails and both rules are equal the kernel just chooses the one that is first in the list. If you have a preference, then you choose that one.

If you want to make a preference, you'll need to add rules (such as in my case) to the route like this:

ip route add 192.168.122.0/24 dev eth0 src 192.168.122.224 metric 2
ip route add 192.168.122.0/24 dev eth1 src 192.168.122.160 metric 1

Which will alter the routing rules out of the host. You might need to clear your arp cache after doing that.

share|improve this answer

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.