edgararroyo
Posts: 16
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
Posts: 4134
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

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.
Need Pi spray - these things are breeding in my house...

edgararroyo
Posts: 16
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: 3538
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: 3538
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
Posts: 4134
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

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.
Need Pi spray - these things are breeding in my house...

edgararroyo
Posts: 16
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!

Return to “Raspbian”