Can a Linux machine have two the same IP addresses on different interfaces/networks?

7,613

Solution 1

Well, is not only possible, but sometimes useful or even needed.

It is a choice, for example, when you have two links to different switches in an L3 switching environment. Of course, the configuration requires something more than just setting the same IP in both interfaces.

As far as I know, you can reliably manage an IP shared among several interfaces in two ways, depending on the scenario.

If you need to deploy the same configuration on different LAN's you can use namespaces, assigning different interfaces to different namespaces and running the desired services on each one in a replicated way. See this great answer.

Other much more common (I guess) use case is that that I mentioned in which your host is directly connected to two or more L3 switches. In this case you probably may want to provide load balancing, in that case you can configure the same IP in both interfaces and use two different routing tables and multihop. This is very well explained here.

Solution 2

Is it possible to have two the same IP addresses and subnets assigned from two different interfaces/networks?

If the IPs are different:

Two NICs in the same subnet are by definition in the same network. This means it doesn't matter which NIC is used to reach anything in that network. Exact behavior depends on the system. Most will probably pick one and stick with it. You might be able to configure things to load balance, or fail over, depending on your operating system.

Of course, if you are expecting traffic on NIC 2, when it was sent on NIC 1, it won't happen, because the recipient of traffic over NIC 1 will use NIC 1's IP, not NIC 2.

This situation can happen if you have a wired and wireless connection to the same network in a corporate environment.

Policy routing is what you want to look into if you want different traffic moving over different NICs within the same subnet.

If the IPs are the same:

It'll be a mess. Systems use ARP to convert IPs into MAC addresses so their Ethernet adapters can speak to each other. Whichever NIC's ARP traffic answers first will be the one used, and that's probably unpredictable, so you simply won't know where traffic will end up.

If you want 2 NICs combined to work together to send traffic as one IP, you need to look into teaming or bonding. This requires the same team/bond setup on both sides.

Share:
7,613

Related videos on Youtube

Gabrijel Šimunović
Author by

Gabrijel Šimunović

Updated on September 18, 2022

Comments

  • Gabrijel Šimunović
    Gabrijel Šimunović over 1 year

    Is it possible to have two the same IP addresses and subnets assigned from two different interfaces/networks?

    How would such machine behave? How would route table look like?

  • dirkt
    dirkt about 5 years
    While identical IP address in different namespaces certainly work (though I'm not sure if this is what the question was about), the example for the routing table you link to actually use different IP addresses (and hence from 1.2.3.4). I would be very curious how to make that work with an identical address. In particular with applications which can't bind to specific interfaces. Or do you just want to do "load balancing" by destination address? In that case you don't even need policy routing.
  • UaT
    UaT about 5 years
    You can use iptables MARK and ip rule fwmark to route each connection's traffic by the same interface without having to care about destination address.
  • Mikko Rantalainen
    Mikko Rantalainen almost 4 years
    If you want to use bonding, 802.3ad with dynamic link aggregation is usually the config you want.