2

I have a headless Debian server with two network cards:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 01:02:1d:54:7c:01 brd ff:ff:ff:ff:ff:ff
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 01:02:1d:54:7e:01 brd ff:ff:ff:ff:ff:ff

If I disconnect the cable connected to eth1, then port will go down, but administratively it will stay up:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 01:02:1d:54:7c:01 brd ff:ff:ff:ff:ff:ff
2: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
    link/ether 01:02:1d:54:7e:01 brd ff:ff:ff:ff:ff:ff

This means that routes associated with eth1 are still present in routing table. Is there a way to force eth1 administratively down once it's physically disconnected?

2
  • 2
    What if you then reconnect? It should also be able to detect that and bring the card back up, right?
    – terdon
    Oct 29, 2014 at 15:27
  • @terdon Yes, it should.
    – Martin
    Oct 30, 2014 at 7:57

1 Answer 1

4

A quick and dirty solution would be to use ip monitor and a script that "ifupdowns" the interface.

The script would be:

#!/bin/sh

ip monitor link | while IFS=':' read num name status; do
    read extraline # iproute2 outputs physical address on a separate line.
    newstate=$(echo "$status"|awk -F' ' '{print $NF}')
    if [ "$newstate" = "UP" ]; then
        ifup "$name"
    elif [ "$newstate" = "DOWN" ]; then
        ifdown "$name"
    fi
done

The script doesn't check network interface status before attempting to change it, but that shouldn't be a problem.

To use it just place it in /opt/bin/linkmonitor.sh, make it executable with chmod +x /opt/bin/linkmonitor.sh and add it to /etc/rc.local with an ampersand at the end:

/opt/bin/linkmonitor.sh &

If you need a more robust solution you may be interested in a route failover solution.

3
  • Thanks! Not that I have anything against custom-made scripts based on ip monitor, I thought that this is so common problem that there is some sort of daemon for this.
    – Martin
    Oct 31, 2014 at 15:07
  • Sure, it's called networkmanager :p
    – GnP
    Oct 31, 2014 at 17:56
  • :D I thought something more suitable for servers. However, I'll go with the ip monitor script. Thanks!
    – Martin
    Nov 4, 2014 at 9:57

You must log in to answer this question.

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