1

I'm using Raspbian Jessie.

I would like to have 2 ip addresses on my raspberry eth0 interface.

  • One dynamic, dhcp assigned,
  • One static, configured either in /etc/network/interfaces, either in /etc/dhcpcd.conf

I tried changing the file /etc/network/interfaces like this:

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto eth0:0
iface eth0:0 inet static
    address 10.166.247.2
    netmask 255.255.255.0

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

It works fine, but after a while, the dynamic ip address disappears. It seems the lease isn't renewed.

I've read a lot of advises to leave /etc/network/interfaces untouched and rather use /etc/dhcpcd.conf to setup a static ip address, but is it possible to ask dhcpcd to setup both a static and a dynamic ip address at the same time?

Other solutions (systemd-networkd) are welcome too! (as long as I don't need a gui)

Thanks!

edit: Since a lot of people is interested in this question, I'm sharing my workaround. Maybe it will help you too. I tried a few possible solutions but none was stable enough. The reason why I would have wanted this was that I wanted 2 raspberry pi based devices to be able to talk together in a network that I was not controlling, but these would also need to have internet access, so one dhcp ip for the default routing, and one fix ip in a different range for the internal connection. That was working but eventually the dhcp was not renewing the lease. I finally managed to solve my problem using the mdns "hostname.local" address to communicate without having to know the ip of the machines. It's also a cleaner solution in my opinion.

Olivier
  • 119
  • 1
  • 6
  • Please can you edit your question and add the output from `ip addr` when eth0 has two ip addresses? – Ingo Mar 15 '18 at 14:16
  • You are using jessie which is oldstable and you want to use deprecated [debian networking mixed up with dhcpcd](https://raspberrypi.stackexchange.com/a/41187/79866). As you was already told this is very sophisticated. I never understand it. If you are willing to migrate do modern systemd-networkd I could present you a very simple solution. But that would'nt be an answer to your question. You are asking for dhcpcd. – Ingo Mar 15 '18 at 19:20
  • @Ingo, I edited my question: other solutions (systemd-networkd) are welcome too! – Olivier Mar 26 '18 at 08:47
  • hey @Olivier did you ever figure out a solution? – Simon Oct 10 '19 at 02:29
  • Your question popped up again after years. Does the answer help you? If so it would be nice if you could accept it or make your own answer and accept it after two days. Only this will finish your question. – Ingo Dec 01 '19 at 12:59
  • Sorry but no, I tried a few possible solutions but none was stable enough. The reason why I would have wanted this was that I wanted 2 raspberry pi based devices to be able to talk together in a network that I was not controlling, but these would also need to have internet access, so one dhcp ip for the default routing, and one fix ip in a different range for the internal connection. That was working but eventually the dhcp was not renewing the lease. I finally managed to solve my problem using the mdns "`hostname`.local" address to communicate without having to know the ip of the machines. – Olivier Apr 03 '20 at 22:52

3 Answers3

2

There are two uncertainty factors for this answer.

  1. The question is for Raspbian Jessie but I've tested it with Raspbian Stretch. But Raspbian Jessie started with systemd-networkd so it should work.

  2. There are two wifi connections at the same time. I can't test it because I have only one wifi. The configuration suggestion for this here is theoretical.

With systemd-networkd the default configuration file for eth0 looks like this:

rpi3 ~$ cat /etc/systemd/network/04-eth0.network
[Match]
Name=eth0
[Network]
DHCP=yes

For additional ip addresses you only add additional lines:

[Match]
Name=eth0
[Network]
DHCP=yes
Address=10.166.247.2/24

How it works? Follow these instructions to migrate your networking to systemd-networkd. It's a little effort. You only need
Step 1: Preparation
Step 2: Setup ethernet interface
Step 3: Setup wlan interface - two times. Second time replace wlan0 with wlan1
Step 5: Clean up
( Bonding is not needed here ).

At least your configuration files should look like this:

rpi3 ~$ cat >/etc/systemd/networkd/04-eth0.network <<EOF
[Match]
Name=eth0
[Network]
DHCP=yes
Address=10.166.247.2/24
EOF

rpi3 ~$ cat >/etc/systemd/networkd/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
DHCP=yes
EOF

rpi3 ~$ cat >/etc/systemd/networkd/12-wlan1.network <<EOF
[Match]
Name=wlan1
[Network]
DHCP=yes
EOF

Have in mind that you also have to setup
/etc/wpa_supplicant/wpa_supplicant-wlan0.conf and
/etc/wpa_supplicant/wpa_supplicant-wlan1.conf.

Ingo
  • 40,606
  • 15
  • 76
  • 189
0

I finally managed to solve my problem using the mdns "hostname.local" address to communicate without having to know the ip of the machines. This removed the need for fixed IP address. It's also a cleaner solution in my opinion. Just make sure to have avahi-daemon installed and running, and let it do the magic!

Olivier
  • 119
  • 1
  • 6
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Apr 07 '20 at 08:31
0

RaspiOS Buster (the official Raspberry Pi operating system based on Debian 10) uses dhcpcd to configure network interfaces, both eth0 and wlan0. The use of /etc/network/interfaces is obsolete and there is not a way to configure a static IP address alias into /etc/dhcpcd.conf. It is possible however to create an user defined hook script following the dhcpcd-run-hooks man page. In short, create a file called /etc/dhcpcd.enter-hook and write in it some lines:

if [ "$interface" = "eth0" ]; then
    case $reason in
        PREINIT)
            /usr/sbin/ip addr add 192.168.3.38 dev eth0 label eth0:0 || true
            ;;
    esac
fi

For more info see https://www.rigacci.org/wiki/doku.php/doc/appunti/linux/sa/dhcpcd_ip_alias