7

If I want to have 2 static IP addresses, I can do something like this:

auto eth0
    iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    broadcast 192.168.1.255

auto eth0:0
    iface eth0:0 inet static
    address 192.168.1.11
    netmask 255.255.255.0
    broadcast 192.168.1.255

However, I'd like to use DHCP to get addresses. I tried the following:

auto eth0
    iface eth0 inet dhcp

auto eth0:0
    iface eth0:0 inet dhcp

...but I only end up with eth0, and no second IP. Is there a simple way to go about this?

5

After a little bit of research, apparently you can't do that.

The sub-interface cannot request another IP address because it is using the same MAC address as the physical interface, and since DHCP requests are by MAC address, you can't get two IPs for the same interface.

An answer from SuperUser suggests the following two methods:

Method 1

Create a script that will do something like this (with a subinterface defined on the primary):

  1. Primary interface issues DHCP and gets IP address,
  2. macchanger changes MAC address of interface,
  3. Sub interface issues DHCP and gets IP address,
  4. Revert mac address with macchanger.
  5. Kill the DHCP client so that it doesn't automatically run later.

Work out the lease time of the IP address you are given, and schedule this script to run again before the lease expires.

Method 2

For this you will need iproute2 installed. The following command adds a virtual interface bound to an existing eth0 interface:

ip link add link eth0 address 00:11:22:33:44:55 virtual0 type macvlan

Replace the mac and "virtual0" name of the interface to whatever you like. Turn it on:

ip link set virtual0 up

Then configure using dhcpd or dhclient or ifconfig as needed. I have tested this on Debian squeeze - your distro may not have everything needed enabled in the kernel (macvlan particularly).

Source: SuperUser: Getting 2 IP addresses on one network card, using DHCP

Other sources:

6

You also need to make sure that the primary interface is configured for promiscuous mode:

ip link set dev eth0 promisc on

If using a Virtual Machine, also make sure that your VM host permits the guest to use promiscuous mode.

You can bring this all together in /etc/network/interfaces like this:

# 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

auto vir2   
    iface vir2 inet dhcp
    pre-up ip link add link eth0 address 02:cd:ab:00:10:02 vir2 type macvlan
    post-down ip link delete vir2

...etc

0

Expanding on David's response, it worked for me, without needing promsic mode. I found it helpful to add a hostname, to get my router to resolve DNS for each interface.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.