2

I have a Debian system with more than one ethernet port.

I'm currently running a script on boot which assigns a static IP to one of the ports (by updating /etc/network/interfaces and restarting networking).

(A router with DHCP might be available sometimes, but this cannot be assumed.)

Currently the script has to either be hard-coded with the device name to assign the IP to, or at best have a simple rule like "pick the first one from /sys/class/net/e*". (Especially since you can't assume eth0 exists any more.) It can't save any information across reboots; the filesystem is read-only.

Is there a way to better auto-detect which interface actually has a cable connected to it? The goal is to allow the cable to be connected to any port (prior to boot) and the script will figure it out.

I've tried walking the /sys path above and trying to read carrier state, but at boot time there always seems to be no carrier.

Added complication: eventually I might have more than one cable connected. What I'd really like is a way to auto-detect which interface has a link to a PC in a compatible subnet with the static address. (There will only be one of these even if multiple cables are connected.)

It doesn't need to worry about the cable being moved after boot, although it might need to pause boot until the "other end" is turned on before it can detect it.

(I did try just assigning the same static IP to all interfaces, under the assumption that only one would actually connect. But that appears to really aggravate ifup.)

1
  • So it looks like you want to detect when an ethernet interface (no matter which one) goes up, scan the subnet behind it for at least one IP address (with ping or broadcast pings), and based on that assign a static IP. As you are using /etc/network/interfaces, I assume you are also using legacy ifup/ifdown (still used even by systemd), so the first thing I'd check if you can use the hooks to execute a custom script (you need to configure all interfaces as auto, and interface names will be persistent unless you change the hardware).
    – dirkt
    Sep 5 '19 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.