Several IP address within the same subnet on the same host
Is it possible (well the real wording should be "Is it correct") to have several IP adresses that belongs to the same subnet, on the same host ?
Here is an example:
#Host 1
eth0 -> 10.0.0.1/24
eth1 -> 10.0.0.2/24
eth1:1 -> 10.0.0.3/24
I have the intuition that this can't work due to routing issues but I'm unable to explain why I think so.
So is this pattern correct ? If yes, is it common ? What can be the problems regarding such a configuration ?
Yeah, sure, there's literally no problem doing this at all - might need to be a little careful how you setup you default gateway but it really isn't a problem at all. If fact time was when that's how you had to setup multi-site webservers (we're talking a long time ago).
There is a somewhat common problem with peoples expectations in this type of setup. With multiple addresses assigned on the same subnet like that typically all outgoing communication will appear to come from a single address.
So responses to incoming connections should be fine. Replies should come from the address that the incoming connection was made to. But if you expect some process, that will be making outgoing connections, to use a specific address other then the first then you will need to make sure that you can specifically configure the IP to bind too in the application.
I have a complicated set up somewhere that uses this type of network. I have two internal interfaces and an external interface to the Internet. (this is about to change to two external interfaces on different subnets and on internal interface split to two internal interfaces, but it's going along the same route) Anyways -- on to the answer!
let's say you have two internal interfaces at:
eth0 192.168.1.2
eth1 192.168.1.3
You use an internal uplink to the Internet (router) at 192.168.1.1
So your default routing table will look like (command: netstat -rn
)
Dest Gw Genmask Flags ... ... Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG eth0
192.168.1.0 0.0.0.0 255.255.255.0 U eth0
192.168.1.0 0.0.0.0 255.255.255.0 U eth1
Here's your problem, all output will go via eth0
because it's the first hit on your routing table. So if you use another computer (or even this same box) to ping 192.168.1.3
(eth1
) you will not get a response? Why? Because it's coming from 192.168.1.2
.
You'll have to use iproute2
to set up individual routing tables for each device. This way when a device gets something on the INPUT
chain it replies via the same device.
edit /etc/iproute2/rt_tables
add:
1 my_eth0
2 my_eth1
then execute the following:
ip route add 192.168.1.0/24 dev eth0 table my_eth0
ip route add default via 192.168.1.1 dev eth0 table my_eth0
ip route add 192.168.1.0/24 dev eth1 table my_eth1
ip route add default via 192.168.1.1 dev eth1 table my_eth1
Now add the rules for the tables to be used on by executing:
ip rule add from 192.168.1.2 table my_eth0
ip rule add from 192.168.1.3 table my_eth1
This will tell your system that when it gets a request on eth0
use the my_eth0
routing table to reply. When it gets a request on eth1
, reply using the my_eth1
routing table. When you get the commands working put them in your /etc/rc.local
file and make rc.local
executable by performing sudo chmod u+x /etc/rc.local
that way your routes are not wiped out when you reboot. Have fun!