edgararroyo
Posts: 17
Joined: Mon Aug 13, 2012 5:39 am

Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 7:25 pm

I've search and found only one solution but it does not work on Raspbian.

On /etc/network/interfaces I added this:

Code: Select all

# 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

But I got no network after a reboot.

Can someone help me on this? I am trying to get 3 different IP addresses from DHCP on my ethernet adapter so I can bind 3 services that use the same ports to 3 different IPs.

Andyroo

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 7:36 pm

/etc/network/interfaces has effectively been dropped from Stretch and Buster unless you hack around removing dhcpcd and go back to network manager I think.

Does it have to be from DHCP or could you set static IP addresses?

If you can then try editing dhcpcd.conf:

Code: Select all

interface eth0
static ip_address=192.168.1.11/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253

interface eth0:0
static ip_address=192.168.1.12/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253

interface eth0:1
static ip_address=192.168.1.13/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253
If its http traffic, it makes more sense to handle the site naming correctly in virtual machines (well in Apache) on the same IP address. Lots less overhead.

edgararroyo
Posts: 17
Joined: Mon Aug 13, 2012 5:39 am

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 7:47 pm

Andyroo wrote:
Sat Sep 07, 2019 7:36 pm
/etc/network/interfaces has effectively been dropped from Stretch and Buster unless you hack around removing dhcpcd and go back to network manager I think.

Does it have to be from DHCP or could you set static IP addresses?

If you can then try editing dhcpcd.conf:

Code: Select all

interface eth0
static ip_address=192.168.1.11/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253

interface eth0:0
static ip_address=192.168.1.12/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253

interface eth0:1
static ip_address=192.168.1.13/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253
If its http traffic, it makes more sense to handle the site naming correctly in virtual machines (well in Apache) on the same IP address. Lots less overhead.

Yes, unfortinately it needs to be DHCP.
And I need to get the IP address it gets assigned so I can bind the service, or can I use 0.0.0.0 for eth0, 0.0.0.1 for vir1, etc etc?

epoch1970
Posts: 6059
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 7:53 pm

If you want multiple addresses specifically via DHCP, macvlan is a good option, however you have to look at the DHCPID the client sends to the server. If it is based on the MAC address, you'll get an IP 3 times. If it is the hostname or some other ID that belongs only to the node, you'll be served 3 times the same IP.

Raspbian uses dhcpcd as its DHCP client and network setup utility. Dhcpcd clashes with /etc/network/interface that frankly nobody should use anymore.

If you want to stay with the old clunky interfaces file, first add "denyinterfaces eth0" to /etc/dhcpcd.conf. Then check which DHCP client ifupdown calls and look for available options in case you don't get your multiple IP addresses but a single one.

If you choose to leave ifupdown and use dhcpcd, start by undoing anything you did in the interfaces file. That file should be empty of any interface declaration.
By default on Raspbian dhcpcd uses a DHCPID based on the MAC address, so that part should be good. What dhcpcd does not handle is interfaces creation.
You'd have to handle that part in something like /etc/rc.local or much better in /etc/systemd/network by creating .netdev unit files.
If you choose the best option, systemd, you'll need to enable systemd-networkd as the service is disabled by default on Raspbian Buster.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

epoch1970
Posts: 6059
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 8:15 pm

Andyroo wrote:
Sat Sep 07, 2019 7:36 pm
If you can then try editing dhcpcd.conf:

Code: Select all

...

interface eth0:0
static ip_address=192.168.1.12/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253

interface eth0:1
static ip_address=192.168.1.13/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.253
Does this really work?

The "interface:label" syntax is something out of ifupdown. Since the interfaces file was structured around interface names came the invention of interface "aliases" to handle multiple IP addresses. I know "label" is still an option in "ip address add .... label ..." but AFAIK that's only supported to please the output of ifconfig (which only shows 1 IP per interface).

I thought dhcpcd didn't care at all about all that, and the only way to get multiple addresses was via something like adding adhoc ip commands in dhcpcd's exit-hook script?
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Andyroo

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Sat Sep 07, 2019 8:39 pm

Well not in Buster :oops:

I took my notes out of a Stretch Evernote build doc I had - wonder if it did work even then as I had set up virtual hosts on the web service :oops:

Let me open a new thread on multiple static IPs on one interface to keep it separate and blow my notes out of the water.

edgararroyo
Posts: 17
Joined: Mon Aug 13, 2012 5:39 am

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Thu Sep 12, 2019 2:53 pm

This was actually more simpler than I thought!

On /etc/rc.local I added:

Code: Select all

# Create VLANs
ip link add link eth0 name eth1 address xx:xx:xx:xx:xx:xx type macvlan
ip link add link eth0 name eth2 address yy:yy:yy:yy:yy:yy type macvlan

And I now have eth0, eth1 and eth2 (3 separate interfaces with different mac addresses and they all get their own IP)

On another note, I no longer need this :D But hey, it's good info.

Cheers!

dazz100
Posts: 44
Joined: Mon Apr 17, 2017 4:54 am

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Wed Oct 02, 2019 9:37 am

Hi
My reading on this is that the

Code: Select all

ip link add link 
code creates a vlan. That adds a tag to the packets so needs a vlan aware device to connect to.
I have just got this working:

Code: Select all

sudo ip address add 192.168.1.12/24 dev eth0

As an experiment, I added 3 IP addresses to eth0. I can ping all three from another raspi.

The command to remove an address is:

Code: Select all

sudo ip a del 192.168.1.12/24 dev eth0
I can confirm this works with Buster.

Dazz

edgararroyo
Posts: 17
Joined: Mon Aug 13, 2012 5:39 am

Re: Raspbian Buster - Need to get 3 IP addresses on eth0 from DHCP

Wed Oct 02, 2019 1:50 pm

Thank you for your reply. I am aware of how to add IPs. Problem is I need to host on the IP addresses the same port.

IP1:80
IP2:80
IP3:80

The app is not smart enough to handle this, but it can handle different interfaces with a bind.

That's why I am using VLANs and it has been working ok.

Return to “Raspberry Pi OS”