Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 brd scope global eth0
    inet brd scope global eth1

Which one to choose? Well the answer to this is dominated by the routes. So if we check that out.. dev eth0  proto kernel  scope link  src dev eth1  proto kernel  scope link  src 

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 dev eth0 src metric 2
ip route add dev eth1 src 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


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.