17

I would like to create several aliases to eth0, but have the addresses assigned by DHCP instead of being set to static IP's. Is this even possible?

All the examples I've seen assign a static IP using the command:

ifconfig eth0:0 192.168.1.11 up

5 Answers 5

19

This is only possible if the two DHCP clients use different MAC addresses. Which means they cannot run both on the same physical network interface, which has a single MAC address.

The solution is Linux's virtual MAC-VLAN network interfaces. MAC-VLAN interfaces are virtual network interfaces backed by a physical interface, but using with a different MAC address, which is randomly generated when you create a MAC-VLAN device.

Once you create a MAC-VLAN interface using a physical interface, you use it the same way like you would the physical one. In your particular case, you can run one DHCP client on the physical interface, and another on the MAC-VLAN device. Each device (physical and MAC-VLAN) can then have its own IP address.

A new MAC-VLAN device macvlan0 can be created from eth0 by running

ip link add dev macvlan0 link eth0 type macvlan

It can be deleted by running

ip link delete dev macvlan0

Using NCD, my network configuration software ( http://code.google.com/p/badvpn/wiki/NCD ), the following NCD program will create two MAC-VLAN devices from eth0 (macvlan0 and macvlan1), run DHCP on each of them, and assign them the obtained IP addresses. You easily do other stuff like add routes and DNS servers; read the NCD page if you're interested.

process lan {
    # Set device.
    var("eth0") dev;

    # Wait for device to appear, set it up, and wait for cable to be plugged in.
    net.backend.waitdevice(dev);
    net.up(dev);
    net.backend.waitlink(dev);

    # Start DHCP's.
    provide("lan-link");
}

process lan_dhcp1 {
    # Wait for link.
    depend("lan-link") linkdep;

    # Choose virtual device name.
    var("macvlan0") vdev;

    # Create virtual MAC-VLAN device.
    list("/sbin/ip", "link", "add", "dev", vdev, "link", linkdep.dev, "type", "macvlan") do;
    list("/sbin/ip", "link", "delete", "dev", vdev) undo;
    run(do, undo);

    # Set virtual device up.
    net.up(vdev);

    # DHCP configuration on virtual device.
    net.ipv4.dhcp(vdev) dhcp;
    ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    println(vdev, ": got address ", dhcp.addr);

    # Assign address to virtual device.
    net.ipv4.addr(vdev, dhcp.addr, dhcp.prefix);
}

# This differs from above only in interface name (macvlan1).
process lan_dhcp2 {
    # Wait for link.
    depend("lan-link") linkdep;

    # Choose virtual device name.
    var("macvlan1") vdev;

    # Create virtual MAC-VLAN device.
    list("/sbin/ip", "link", "add", "dev", vdev, "link", linkdep.dev, "type", "macvlan") do;
    list("/sbin/ip", "link", "delete", "dev", vdev) undo;
    run(do, undo);

    # Set virtual device up.
    net.up(vdev);

    # DHCP configuration on virtual device.
    net.ipv4.dhcp(vdev) dhcp;
    ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    println(vdev, ": got address ", dhcp.addr);

    # Assign address to virtual device.
    net.ipv4.addr(vdev, dhcp.addr, dhcp.prefix);
}

You'll end up with macvlan0 and macvlan1 each with its own DHCP-obtained IP address, and eth0 which is up but does not have an IP address. Alternatively, you can create just one MAC-VLAN interface, and run one DHCP instance on eth0 itself.

1
  • 1
    Apologies for raising this from the dead, but the statement "This is only possible if the two DHCP clients use different MAC addresses" is incorrect from the DHCP protocol perspective. Even in 2011 DHCP was still based on a Client ID, which does not have to match the MAC address of the device. (RFC 2131, published in '97) Oct 20, 2016 at 15:31
0

I have never seen a DHCP configuration which would allow the DHCP server to send multiple addresses to an interface. I don't believe the protocol supports it. Once you get into multiple IP addresses on an interface, you really should configure them as static addresses.

Some DHCP clients allow static address to be assigned when an address is supplied. I don't believe Windows will allow you to add more than one.

Make sure all your static addresses are outside the DCHP address range.

-1

Using static or DHCP assigned IP addresses is a choice you make for your computer.

There is a good tutorial here.

It contains plenty of detail, and helps you avoid problems that you can have if you use static instead of DHCP assigned address, e.g. how to connect to DNS servers and gateways.

-1

I dont know for sure But i believe unless you have a open source router then you will have to use static IP to do what you want. IMO unless you have a REALLY good reason for why you want to do what you want I would just stick witrh DHCP. from your question i see thats what you want so unless someone knows how to do it I would wait and stay with DHCP until someone gives a better answer. Although if you dont have more than 1-2 pcs and nothing else on the network (printers media extenders etc) then I would not use Static IPs as IDK how or if it is possible to set these items with a static IP. Maybe you can do what you want if you get an Open source Router and you may be able to make it do what you want like just use a static IP for your machine using the mac address as a way to identify it and set its IP and the rest of the network could bne DHCP. IDK as i have never done thins i am just throwing out ideas so Sorry for not having an answer for you. Although the link posed by Bryan looks like a nice resource on DHCP ans Static IP issues. Also For the Open Source router I believe Netgear and D-Link make them I am sure more but IMO these two brands are the ones I would choose. I personally dont like Linksys since they were bought by novel (you would think they would be better but NOT IMO. Personally I prefer D-link as they are the most customizeable routers i have found.)

Also check out this link and you may be able to do more with your router if it is supported

This site has Router Firmware

-1

No, you can't do that. DHCP assigns an IP based on your hardware address. Each physical interface only have one of those, thus DHCP will only assign one IP to a physical interface.

I would advise you to just set the interface IPs statically.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .