Using DHCP to get two different IP addresses for two NICs on one Machine

I am using Debian 7.0.

I have one machine with two NICs connected and I want to assign two static IP addresses to this machine.

Here is my configuration in /etc/network/interfaces :

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet dhcp

Here is the result from /sbin/ifconfig:

enter image description here

As you can see both NICs received an static IP address.

The strange problem is that you can ping/access only the IP from eth1. So again even when I have two NICs I have only one IP for that machine.

Where is my mistake, why I can not reach/access/ping the IP from eth0 from another machine ?

You can try the ping also and you will see.

I hope you can help me!


Solution 1:

Probably because the system with two NICs is trying to send the reply ICMP ping message from eth0 out of eth1.

When your system sends traffic out to the network, it consults its local Forwarding Information Base (FIB), or routing table.

The routing table will have a list of subnets and interfaces where that subnet can be reached. Each outgoing packet is compared against every entry in this table, and the one with the highest subnet CIDR number will "win" and be used to send the traffic. If there are no winners the default gateway is used.

You have 2 entries with the same subnet CIDR going to the same subnet. Essentially, as far as your system is concerned, these go to the same place so it does not matter which is used. Your network stack is going to use the first one it encounters, send the traffic out of it, and then be done with it. Probably the NIC with the lowest IP number is appearing first in your routing table.

You need to look into Linux's policy routing features (Google "policy routing" and good luck because I'm studying it myself), or look into redirecting traffic with iptables if you really want to do this.

Solution 2:

The simple way

At your level of knowledge, it might be best to ignore the second NIC (ie disconnect it) and have 2 IP addresses bound to the first NIC. The disadvantages are lack of redundancy and you are limited to the speed of 1 NIC.

To do this (for eth0), modify your /etc/network/interfaces file to read:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address A.A.A.A
netmask 255.255.252.0
gateway X.X.X.X

auto eth0:1
iface eth0:1 inet static
address B.B.B.B
netmask 255.255.252.0

Note that you will need to put in the correct gateway address for X.X.X.X. You will also need to make sure that the IP addresses (A.A.A.A and B.B.B.B) are not in the DHCP range.

The hard way

The idea here is to make the 2 interfaces act as 1 double speed interface (with the ability to continue work if an interface is unplugged), and then bring up the IP addresses similar to above.

The first difficulty to overcome is to work out what mode to use. There is no one-size fits all solution here, its the matter of choosing the most appropriate one for you and your hardware. Have a look here for a description of different modes. If in doubt, use mode 0 - this does not require fancy hardware, but you can only get the full effect of channel bonding for outbound traffic, but you still get redundancy. If you have traffic for lots of different addresses on the LAN, mode 2 is not a bad choice either.

You need to create a file like /etc/modprobe.d/bonding.conf to tell the system about how you want to do the bonding

alias bond0 bonding
options bond0 mode=0 miimon=100

(The above assumes you are using mode 0).

The easiest way to make this change come into effect is to reboot your computer.

You will now have a new interface available on your system "bond0". Using /etc/networks/interfaces you now enslave the 2 ethernet interfaces and then bring up the virtual interfaces as per example 1 above.

So the file will look like

# The loopback network interface
auto lo
iface lo inet loopback

# Enslave the ethernet drivers
auto eth0
iface eth0 inet manual
bond-master bond0
bond-primary eth0

auto eth1
iface eth1 inet manual
bond-master bond0
bond-primary eth1

# The primary network interface
auto bond0
iface bond0 inet static
address A.A.A.A
netmask 255.255.252.0
gateway X.X.X.X

auto bond0:1
iface bond0:1 inet static
address B.B.B.B
netmask 255.255.252.0