Linux – Two ip address on one NIC

iplinuxnetworkingnicrouting

I am trying to get two external ip addresses(both ip assigned by DHCP) on single NIC. I added one macvlan device vir1 with its own mac address bounded to the primary NIC eth0. Set eth0 to promiscuous mode.

Both vir1 and eth0 get its own ip address, I intend to use both of the ip address at the same time.

When I ping from an external host to eth0 ip address, I can get a ping response, but when I ping to vir1 ip address, there isnt any reply. tcpdump shows that both vir1 and eth0 get the ping request when I ping to vir1

I want both ip address can be used by client to access my web service, the computer is connected to the internet directly with ethernet cable from isp

Any help would be appreciated!

/etc/network/interfaces

# Primary interface
auto eth0
iface eth0 inet dhcp
    up ip link set dev eth0 promisc on

# Macvlan interfaces
auto vir1
iface vir1 inet dhcp
    pre-up ip link add link eth0 address 02:cd:ab:00:10:01 vir1 type macvlan
    post-down ip link delete vir1

ifconfig result:

eth0      Link encap:Ethernet  HWaddr 8c:73:6e:b7:c7:10  
          inet addr:120.201.123.143  Bcast:120.201.123.255  Mask:255.255.252.0
          inet6 addr: fe80::8e73:6eff:feb7:c710/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1

vir1      Link encap:Ethernet  HWaddr 02:cd:ab:00:10:01  
          inet addr:120.201.120.227  Bcast:120.201.123.255  Mask:255.255.252.0
          inet6 addr: fe80::cd:abff:fe00:1001/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

Best Answer

If you add a second IP to a virtual VLAN interface, the NIC will tag frames destined for that VLAN (802.1Q). If the VLAN isn't set up on the switch it'll just get dropped.

If you want to use the same segment you need to add the IP to the NIC directly:

auto eth0:1
iface eth0:1 inet static
address 120.201.120.227
gateway 120.201.120.001          <= insert correct gw address
netmask 255.255.252.0

DHCP with two IP addresses won't work, not even with Linux.