11

What will happen if you have 2 network interfaces to the same network where the default gateway/router is?

e.g

  • network interface e1000g0 - 192.168.1.50
  • network interface e1000g1 - 192.168.1.51
  • Gateway - 192.168.1.1

If I send a packet to 8.8.8.8, which network interface will be used ? I am using a Solaris 10 box. But assume this question is for all unix/linux general implementation

In my routing table, it seems that e1000g1 is chosen as the UG , but why/how is it chosen?

1
  • 2
    Note that all answers here should be assuming that the subnet size (a.k.a., "prefix length", a.k.a. "subnet mask") is identical. If they were not identical, the answer is easy: whichever rule has a smaller subnet size is presumed to be more specific, and takes complete priority for traffic going to that smaller subnet.
    – TOOGAM
    Commented Nov 8, 2015 at 19:45

3 Answers 3

13

In Linux and Unix, there can be only one gateway per routing table. On Linux you can have multiple routing tables, but each has a single gateway.

In Linux, it is identified as follows:

    > ip route show
      default via 192.168.73.1 dev eth0  proto static 
      192.168.73.0/24 dev eth0  proto kernel  scope link  src 192.168.73.75  metric 1 

The key line is the one beginning with default, it states that (my) gateway is 192.168.73.1. You change that as follows:

     > ip route del default
     > ip route add default via 192.168.73.1 dev eth0

If I have more than one interface connected, my routing table will look like:

     > ip route show
       default via 192.168.73.1 dev eth0  proto static 
       192.168.73.0/24 dev eth0  proto kernel  scope link  src 192.168.73.75  metric 1 
       192.168.73.0/24 dev wlan0  proto kernel  scope link  src 192.168.73.66  metric 9 

As you can see local traffic can pass thru either eth0or wlan0, but, thanks to the smaller metric value (1 vs. 9) will be routed thru eth0.

WAN traffic will surely go thru eth0 because the default gateway is on dev eth0.

These values are automatically maintained by your network manager which favors, by default, cable over wifi connections. You can however override its choice by means of commands similar to the previous ones,

       > ip route delete default
       > ip route add default via 192.168.73.1 dev wlan0

If you have multiple interfaces of the same type (e.g. multiple ethernet connections) then the priority goes to the interface that was connected first.

The default gateway is identified, so to say, by the network itself: it is one of the parameters which are passed to your machine when a DHCP transition is negotiated (the others are netmask, network, broadcast address). You are unaware of this because the process of DHCP is handled for you by the network-manager. Occasionally, you may wish to setup a static IP address, which does not use DHCP; in this case you will have to provide yourself the four parameters mentioned above. There are techniques for learning these parameters even in networks totally new to you, but normally you will setup a static address only in LANs you already know well.

Apart from your LAN and default gateway routes, you can have specific routes thru either interface. In that case, the order of priority of routes is determined on the basis of the principle most specific routes first. Suppose you have a route for 1.1.1.0/24 thru wlan0. Then, when we need to route packets for, say, 1.1.1.1, both this rule (thru wlan0) and the default rule (thru eth0) apply, but the first one is more specific, hence thse packets will pass thru wlan0. In this case, the dafult gateway becomes the route when all else fails, i.e. when no other route applies.

In Linux you can have multiple routing tables, this is called policy or source routing. In this case you will also need a rule for the kernel to discriminate when to apply the multiple routing tables at your disposal. You find a brief introduction to policy routing here. In this case, the above commands remain identical, except they are followed by the name of the routing table you wish to work on, for instance:

       > ip route show table main
       > ip route show table MyOtherRoutingTable

You will find often references to commands like netstat, route, ifconfig. In Linux (but not on Unix, read the top comment below) they are obsolete. The current command, ip of the iproute2 suite, substitutes for all of these, and a few more. By googling linux ip cheat sheet you can find sites comparing the capabilites of ip and previous utilities.

Edit

On Unix, the commands above would be: you query the routing table as

   >  netstat -rn -f inet
      Routing tables

      Internet:
      Destination        Gateway            Flags        Refs      Use   Netif Expire
      default            192.168.11.1       UGSc           10        0     en0
      127                127.0.0.1          UCS             0        0     lo0
      127.0.0.1          127.0.0.1          UH              2   161444     lo0
      169.254            link#4             UCS             1        0     en0
      169.254.10.9       10:c3:7b:9d:c8:78  UHLSW           0        0     en0
      192.168.11         link#4             UCS             4        0     en0
      192.168.11.1       10:6f:3f:25:c6:33  UHLWIir        11     4589     en0   1162
      192.168.11.56      88:53:2e:10:77:5f  UHLWI           0        0     en0   1187
      192.168.11.65      2:f:b5:70:5b:22    UHLWI           0        0     en0   1081
      192.168.11.67      127.0.0.1          UHS             0        0     lo0
      192.168.11.113     6:20:3e:52:16:4d   UHLWIi          2      278     en0   1188

Here the gateway is identified by the default keyword, and by the G flag.

You change default gateway as:

   > route delete default
   > route add default 192.168.0.1
   > route change default -interface enp0s3

Every comment remains the same, except those about policy routing, which is not supported by Unix kernels.

2
  • 3
    Let's be careful about calling netstat/route/ifconfig obsolete. You might be able to get away with such statements on Linux, but definitely not Unix. iproute2 is encumbered by licensing that is not tolerable for some Unix systems, so it will never be adopted by some of the non-Linux systems. In fact, it even has a Solaris tag. Since the question was clearly phrased for both Linux and Unix, the statements about the traditional commands being obsolete is not applicable to all of the platforms that the question applies to.
    – TOOGAM
    Commented Nov 8, 2015 at 19:43
  • @TOOGAM Unix (the standard) doesn't specify netstat/route/ifconfig so each Unix implementer is free to deliver whatever set of commands to configure networking. Solaris engineering rewrote most of the network stack in Solaris 11 and deprecated some of the mentioned commands, replaced by dladm, ipadm, netcfg and others.
    – jlliagre
    Commented Nov 9, 2015 at 8:37
1

Having two active different physical interfaces on the same OS instance connected to the same subnet like e1000g0 and e1000g1 in your question is unreliable, and explicitly unsupported at least with Solaris:

On a system whose multiple interfaces connect to the same subnet, you must configure the interfaces into an IPMP group first. Otherwise, the system cannot be a multihomed host.

As stated, one way to have this reliably work is to enable ipmp (IP multipathing). You might also disable packet-forwarding and configure static routes like documented here for Solaris 10.

Alternatively, you might also use the lower level link aggregation, a.k.a. interface bonding in Linux terminology.

2
  • You can have multiple interfaces on the same subnet with Solaris, although not recommended -- using IPMP, or creating a link aggregation would be a preferred. You just need to ensure that local-mac-address?=true. Commented Apr 3, 2017 at 20:11
  • @sleepyweasel I explicitely mentioned IPMP, and link aggregation is indeed a similar supported approach. The OP is obviously in neither of these cases so my statement still apply. local-mac-address?=true is a prerequisite for the previously mentioned settings but outside these cases, I still maintain that such a configuration is unreliable and unsupported.
    – jlliagre
    Commented Apr 3, 2017 at 20:56
-1

I think that the message will be sent through the interface configured in the default route entry. Probably the last one that became up.

1
  • 1
    well, in the case of IP routing, a route will always be selected because it is the most specific (longest mask) to that address with the lowest Metric. Commented Nov 8, 2015 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .