14

I'm working on a linux gadget.

I want it to get DHCP if I plug in the network cable after it has booted up already or if the network cable unplugged and replugged.

One solution is to run a script such as this (which works, btw):

#!/bin/bash

NET_STATUS='different'

while true
do
  NEW_NET_STATUS=`ifconfig | grep RUNNING | grep -v LOOPBACK`
  if [ "${NEW_NET_STATUS}" = "${NET_STATUS}" ]
  then
    echo "no change"
    sleep 1
    continue
  fi
  NET_STATUS=${NEW_NET_STATUS}
  if [ "${NET_STATUS}" ]
  then
    echo "cable plugged in"
  else
    echo "cable unplugged"
  fi
  sleep 1
done

However, I've got a feeling deep down in my little toe that tells me that there's a better way to deal with hotplug events for the ethernet cable.

2
  • 1
    Unfortunately as of 2016 both ifplugd and netplugd appear unmaintained. However this similar question looks like it has a good (simple) answer using udev rules - no additional package needed. serverfault.com/a/312296/275255 Feb 22, 2016 at 17:22
  • Change auto to allow-hotplug in interfaces.
    – Ken Sharp
    Jun 13, 2018 at 12:52

4 Answers 4

9

netplug

netplug is the solution that I went with. ifplugd may work just as well.

Installation

sudo apt-get install netplug

Interface Configuration

cat /etc/netplug/netplugd.conf
eth*

Event Configuration

cat /etc/netplug/netplug
#!/bin/sh
PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

dev="$1"
action="$2"

case "$action" in
in)
    echo "$dev : $action : plugged in" >> /tmp/netplug.log
    ;;
out)
    echo "$dev : $action : unplugged" >> /tmp/netplug.log
    ;;
probe)
    echo "$dev : $action : probed" >> /tmp/netplug.log
    ;;
*)
    echo "$dev : $action : I feel violated" >> /tmp/netplug.log
    exit 1
    ;;
esac

Testing

/etc/init.d/netplug stop
/etc/init.d/netplug start

cat /tmp/netplug.log
eth0 : probe : probed
eth1 : probe : probed
...
eth15 : probe : probed
eth0 : in : plugged in
5

ifplugd handles this situation very well:

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with onboard network adapters, since it will only configure the interface when a cable is really connected.

ifplugd interfaces with your distribution's native network configuration utilities.

Some features:

  • Uses your distribution's native ifup/ifdown programs.
  • [...]
  • Supports the Linux SIOCETHTOOL (newer, aka ethtool API), SIOCGMIIREG (older, aka mii-diag/mii-tool API) and SIOCDEVPRIVATE (oldest, aka mii-tool API) ioctl()s for getting link status. Release 0.24 introduces support for link detection with the IFF_RUNNING interface flag.
  • [...]
  • Can be configured to ignore short "unplugged" periods (-d option) or short "plugged" periods(-u option)
  • [...]
  • Compatibility mode for network devices which do not support cable detection (-F option)
2
  • My local LUG directed me to netplug, so I went with that instead. Thanks!
    – coolaj86
    Sep 7, 2011 at 19:29
  • 1
    This did the trick for me with one caveat. With netplugd enabled, I found that I had to remove auto <interface> from /etc/network/interfaces. If not, I had to plug in the Ethernet cable, remove it, and plug it in again before I got an IP address from DHCP. With this line removed, I get an IP address from DHCP on the first plug-in event or on boot if the cable was already plugged in (IP on boot worked with or without the line removed when ifplugd was enabled).
    – MrMas
    May 17, 2019 at 17:33
5

I would suggest using ifplugd over netplugd as it works better and is actively maintained.

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled. This is useful on laptops with onboard network adapters, since it will only configure the interface when a cable is really connected.

2

Using this software of mine for network configuration, it's very easy.

http://code.google.com/p/badvpn/wiki/NCD

The following NCD program will do. It also shows how you can extend the configuration, in this case by running something when the network goes up/down.

process eth0 {
    # Set device.
    var("eth0") dev;

    # Wait for device to appear, set it up, and wait for cable to be plugged in.
    net.backend.waitdevice(dev);
    net.up(dev);
    net.backend.waitlink(dev);

    # DHCP configuration.
    # net.ipv4.dhcp() will block here until it obtaines an IP address.
    # It doesn't check the obtained address in any way,
    # so as a basic security measure, do not proceed if it is local.
    net.ipv4.dhcp(dev) dhcp;
    ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
    ifnot(test_local);

    # Assign IP address to interface, as obtained by DHCP.
    net.ipv4.addr(dev, dhcp.addr, dhcp.prefix);

    # Add default route.
    net.ipv4.route("0.0.0.0", "0", dhcp.gateway, "20", dev);

    # Add DNS servers.
    net.dns(dhcp.dns_servers, "20");

    # Run an external program when network comes up or goes down.
    list("/some/program/to/run/when/up", "argument") do;
    list("/some/program/to/run/when/down", "argument") undo;
    run(do, undo);
}

Note that this will not only handle the network cable being plugged in or out, but also the network interface itself appearing and disappearing (useful if it's USB).

The advantage of using NCD compared to other software like ifplugd is that its design alows for great flexibility. It basically allows you to program your own network config, as opposed to being limited to a limited set of hardcoded capabilities.

UPDATE: I've made Ubuntu packages for BadVPN and NCD. I've updated the wiki with installation and usage instructions for Ubuntu too: http://code.google.com/p/badvpn/wiki/NCD#Running_it

3
  • This really does look useful, do you think you'll get it into the Ubuntu / Fedora repos?
    – coolaj86
    Sep 7, 2011 at 19:30
  • Currently I have packages for Arch and Gentoo, and nothing else because I don't use other distros. I may find some time to make packages for other distros. However it's easy to just compile and install it into /usr/local or something, and have make a simple init script to start it. Sep 7, 2011 at 19:37
  • Ubuntu packages done, per your request :) Sep 9, 2011 at 12:36

You must log in to answer this question.

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