2

On /etc/network/interfaces (Raspbian), is it possible to set a static IP with optional fallback to DHCP?

That can be handy in case the configured static IP is already taken. Or it could be in a different range than the network it is trying to connect to (eg: setting a static 192.168.0.10 in a 10.0.0.0/24 network).

2 Answers 2

1

There is no good or reliable way for a host to tell whether another machine is already using its IP address, or if the IP is in the wrong subnet for your local network.

You could usetcpdump or arpwatch or similar to listen to the network interface to try to find out if some other MAC address is using your IP (but even this is not reliable because the other host may be switched off or have no reason to send any packets on the network at the moment. Also your host is likely to be on a switch - hardly anyone uses hubs these days - so will only be able to see broadcast packets from the other host and not, e.g., communication between the other host and a server).

Even trying to detect packets from different subnets is problematic - it is quite normal and legitimate for the same physical ethernet segment to have multiple subnets transported on it. You may see packets from the alternate subnet but none from your own subnet and assume that your subnet is wrong, when all that has really happened is that there has been no traffic from other hosts on your subnet in the time you were listening.

How long do you want to listen to the network interface for? How long do you consider it reasonable to delay booting and starting network-dependent services while you listen? 5 seconds? not long enough. 5 minutes? that's probably long enough for a decent sample of the network (no guarantees, though) but way too long to wait for your rpi to boot.

It makes more sense to try for a DHCP IP and then fallback to a static address if there is no DHCP server (but if you knew what a safe static IP address to fallback to was, it'd be simpler just to use that).

Conclusion:

Stick to using either DHCP or a static address.

If you're the network admin, make sure that there's a working DHCP server on the network. Optionally configure it to give a fixed IP addresses to your rpi's MAC addresses.

4
  • I do remember that Debian used to have a tool, handy for laptop configurations, to select your network configuration based on passive listening of the network, but I cannot either remember the name or locate it with apt. Mar 15, 2016 at 9:31
  • An arp request would tell you if something was using a given IP address.
    – roaima
    Apr 14, 2018 at 19:50
  • you could first let the dhcp assign you an IP (to be on the network), then test (ping) the wanted IP, then assign it to yourself.
    – Sandburg
    May 20, 2022 at 11:19
  • @sandburg No, ping does not and can not tell you whether an IP address is already assigned. The best it can do is tell you "this IP address is not responding to an ICMP ECHO at this particular moment in time". The machine it is assigned to may be temporarily offline, it may be busy, it may be blocking/ignoring pings. The IP may be pre-allocated to another machine or service that doesn't even exist yet. If it's your own home network that you're screwing around on then knock yourself out. But if it's a work network, don't be surprised if HR turn up to escort you from the building.
    – cas
    May 20, 2022 at 15:53
0

You can't set in /etc/network/interfaces Indeed if you are using a recent (post May 2015) release you will be warned to NOT set static this way.

See How do I set up networking/WiFi/static IP address? | Raspberry Pi Stack Exchange

This doesn't specifically answer how to set a Fallback Profile but refers you to man dhcpcd.conf which does.

You must log in to answer this question.

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