How to skip DHCP if no cable connected to ethernet in Debian

16,009

If you specify

allow-hotplug eth0

instead of

auto eth0

in /etc/network/interfaces, then the connection will only be initiated by udev when something triggers it, instead of at every boot.

That might be sufficient to handle your case, but not necessarily; the interfaces manpage mentions that

(Interfaces marked "allow-hotplug" are brought up when udev detects them. This can either be during boot if the interface is already present, or at a later time, for example when plugging in a USB network card. Please note that this does not have anything to do with detecting a network cable being plugged in.)

You might need to use /etc/network/if-up.d/00check-network-cable from the ifupdown-extra package to skip the interface if no cable is connected.

Share:
16,009

Related videos on Youtube

Maxim V. Pavlov
Author by

Maxim V. Pavlov

Updated on September 18, 2022

Comments

  • Maxim V. Pavlov
    Maxim V. Pavlov over 1 year

    My Bananian Linux is wasting time at logon trying to get a DHCP lease for eth0 interface which is not connected. Well, the extender cable is connected to it, but nothing is on the other end.

    I have

    auto eth0
    iface eth0 inet dhcp 
    

    set in my /etc/network/interfaces since I do want it to pick up ethernet in case it is connected, but I surely don't want to slow down the startup of the system if the cable is not connected to ethernet. I assumed system would know this automatically and would not attempt to get a DHCP lease for the interface.

    Here is what I see at load time (see the last three lines):

    enter image description here

    After if understands that the lease isn't coming, it proceeds with the boot.

    Is there a way I could tell it not to DHCP if there isn't a connected cable?

  • Maxim V. Pavlov
    Maxim V. Pavlov over 8 years
    As mentioned in the question, I do want to use DHCP if the ethernet cable is connected. So removing the DHCP client will not help me.
  • Jeff Hewitt
    Jeff Hewitt over 8 years
    What if the cable is already connected before boot? Will a udev event trigger the connection at boot time in that case? In other words, does udev check what devices are already connected to the machine's ports?
  • Stephen Kitt
    Stephen Kitt over 8 years
    I'd qualify that as "... used to maintain this package" since the last release was ten years ago ;-). Nowadays Lennart would probably recommend systemd-networkd... With ifplugd, after installation, you also need to configure your device (specify allow-ifplugd in /etc/network/interfaces).
  • user2948306
    user2948306 almost 5 years
    @StephenKitt have you used allow-ifplugd or seen the code? While searching I only found bugs.debian.org/cgi-bin/bugreport.cgi?bug=468896 , which says the allow-ifplugd feature was not accepted.