DHCP and fallback IP – Can I have two different settings on the same network interface?

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.

Go to Source
Author: Jorje12