1

When I boot up my Raspberry PI running Raspbian 9 while it's connected to ethernet, eth0 is not seen as disconnected when I unplug the device.

So if I do ifconfig after boot (with wired connection) eth0 has an IP. But when I unplug the ethernet cable, the eth0 interface STILL has an IP address...

BUT: this does not occur when it boots without a connected ethernet cable. If I then connect it wired, it gets an IP, and if I unplug it, eth0 loses its IP - which is what I want.

None of my interfaces have a static ip, I only set the metric from eth0 to 100 and wlan0 to 200.

So how can I make eth0 always lose its IP address whenever the ethernet cable gets disconnected?

6
  • Absence of IP address is not the same as being "seen as disconnected". 1) Does the interface information in ip addr continue showing the <UP> flag when the cable is disconnected? 2) Does it continue showing <LOWER_UP> flag and state UP too? Commented Dec 13, 2018 at 9:55
  • @grawity 1) yes, 2) yes. The raspbian keeps seeing eth0 as enabled. Even the files in /sys/class/net/eth0 like carrier indicate that eth0 is still up.
    – Mason
    Commented Dec 13, 2018 at 10:14
  • Then the problem isn't with IP address removal – the Ethernet port isn't actually providing the OS the information necessary for that. Commented Dec 13, 2018 at 11:09
  • @grawity as I mentioned, this does work without any problems when the raspberry pi boots up without an already connected ethernet cable. Then it gets an IP when its plugged, and removes the IP when it's unplugged. But that is not the case if it's boot up with an already connected ethernet cable...
    – Mason
    Commented Dec 13, 2018 at 12:47
  • That's entirely besides the point. The removal of IP addresses and other configuration is triggered by carrier state notifications from the driver – and if it doesn't report carrier state correctly, then it doesn't report carrier state correctly. You can't make the OS react to the unplug if the OS doesn't know that the unplug happened; your only option is to try to fix the driver. Are you using the latest Raspbian build? Which Rpi model is it running on, and which Ethernet driver does it use? Commented Dec 13, 2018 at 13:04

1 Answer 1

0

Using the 'ifupdown' package, this should do what you want.

sudo apt install ifupdown
sudo vim /etc/ifplugd/action.d/ifupdown

Then modify the 'ifupdown' script.

#!/bin/sh
set -e

case $2 in
up) 
        if [ $1 = eth0 ] ; then
                /sbin/dhclient -1 eth0
        fi
        ;;
down)
        if [ $1 = eth0 ] ; then
                /sbin/dhclient -r eth0
        fi
        ;;
esac

You must log in to answer this question.

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