2

I have a project in which a user can change the RPI's networking settings via a Web Interface from the following:

  • DHCP WiFi
  • DHCP Ethernet
  • Static WiFi
  • Static Ethernet

Each choice re-configures dhcpcd_conf and wpa_conf accordingly. It works perfectly nice.

The whole package is designed to be in DHCP Ethernet on the first boot. But this design has a flaw in it because not every user has a DHCP server in place.

That's when I thought of making a fallback static Ethernet IP and this is what I came up with:

Case 1 - DHCP Ethernet

# dhcpcd_conf

interface wlan0
noipv4
noipv6

interface eth0
hostname DeviceEth
clientid

profile static_eth0
static ip_address=192.168.1.150/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8

interface eth0
fallback static_eth0
# wpa_conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

As you can see I set wlan0 to have no IP, then I set eth0 to DHCP and then I set the same eth0 to a fallback profile.
Is this correct ? Can I have two different settings on the same interface ?

Case 2 - DHCP WiFi:

# dhcpcd_conf
interface wlan0
hostname DeviceWiFi
clientid

interface eth0
noipv4
noipv6

profile static_eth0
static ip_address=192.168.1.150/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8

interface eth0
fallback static_eth0
# wpa_conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=RO
network={
ssid="SSID"
psk="password"
}

Here I set wlan0 to DHCP, then I set eth0 to no ip and then I set the same eth0 to a fallback profile. Is this configuration correct ?

In both cases I want only one interface active, that's why I configure one to DHCP and the other one to no IP.
In the case of DHCP WiFi it doesn't matter if wlan0 stays up after the fallback is in effect, because the user will most probably configure a static IP once he reaches the Web Interface and static configuration is similar to the DHCP configuration in the way that it only leaves one interface active at a time.

3
  • I'm not sure I fully understand your use case. With the fallback option you can configure a static IP for an interface in case it doesn't get an IP address, which seem to be what you try to achieve in case 1. But case 2 is not clear to me, as you seem to try to configure a static IP on eth0 if the DHCP for the wlan0 fails. Why can't eth0 have a permanent static IP in case 2? If it's not connected, then it will be down and the static IP doesn't matter. Maybe you can add some more description about the desired behaviour and why you want such behaviour.
    – palto
    Commented Jul 24, 2020 at 7:23
  • I am starting to think that case 2 isn't realistic at all. I can either make a fallback static ip for wlan0 and keep eth0 disabled or go with your solution Why can't eth0 have a permanent static IP in case 2? If it's not connected, then it will be down and the static IP doesn't matter. But is it possible to make if conditions inside dhcpcd ? I couldn't find any examples online. Basically, what I want is to only have one interface enabled at a time, with a fallback in case DHCP doesn't work. These devices will be outdoor and I don't want them to be easily hack-able.
    – Jorje12
    Commented Jul 24, 2020 at 12:50
  • There is usually no harm in having two network interfaces configured. If one of them is down (no WiFi access or Ethernet cable unplugged), then it will be ignored anyway. I'm not quite sure about the use case for static fallback IP in case of WiFi, this looks quite unusual to me. If you can't connect to the WiFi, then the static fallback IP will not help.
    – palto
    Commented Jul 24, 2020 at 15:10

2 Answers 2

1

You can't use fallback to switch between different interfaces (which seems the goal in case 2). The dhcp fallback profile is used on the interface for which it was configured if the dhpc on this same interface fails, not if the dhcp on another interface fails.

Not sure if it is possible to use arping for such purpose (see dhcpd.conf). Idea would be to ping the first interface and point to a profile with "no ip". The alternative MAC address profile would then contain the static IP or DHCP to be used on the secondary interface. But I'm afraid that this will not work reliable since it depends on the timing of when the primary interface will get up with an IP address.

2
  • Can you show me how that can be achieved ?
    – Jorje12
    Commented Jul 23, 2020 at 20:53
  • So you are saying that I can use fallback on the interface that has been configured for DHCP and still keep the other one closed. That works for me.
    – Jorje12
    Commented Jul 24, 2020 at 12:41
0

You can do this using inform as said here

  1. Open /etc/dhcpcd.conf for editing in nano.

    nano /etc/dhcpcd.conf

  2. Add the following lines to the bottom of the file. If such lines already exist and are not commented out, remove them.

Replace the comments in brackets in the box below with the correct information. Interface will be either wlan0 for Wi-Fi or eth0 for Ethernet.

interface [INTERFACE]
static_routers=[ROUTER IP]
static domain_name_servers=[DNS IP]
static ip_address=[STATIC IP ADDRESS YOU WANT]/24

In our case, it looked like this.

interface wlan0
static_routers=192.168.7.1
static domain_name_servers=192.168.1.1
static ip_address=192.168.7.121/24

You may wish to substitute "inform" for "static" on the last line. Using inform means that the Raspberry Pi will attempt to get the IP address you requested, but if it's not available, it will choose another. If you use static, it will have no IP v4 address at all if the requested one is in use.

  1. Save the file by hitting CTRL + X and reboot.

From now on, upon each boot, the Pi will attempt to obtain the static ip address you requested.

You must log in to answer this question.

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