Disable WLan if Wired/Cable Network is available

The question says it all. All i want is that my W-Lan connection should be disabled whenever a wired connection is available. What would be the easiest way to do that in Ubuntu/Gnome?

In all guides (for instance some about guessnet) i found i had to configure my whole network configuration (WPA keys, DHCP, ...), but i find that a bit too complicated for such a simple use case. I just want to disable wlan0 when eth0 is connected.


Solution 1:

You can drop this script to /etc/NetworkManager/dispatcher.d/99-wlan:

#!/bin/bash
wired_interfaces="en.*|eth.*"
if [[ "$1" =~ $wired_interfaces ]]; then
    case "$2" in
        up)
            nmcli radio wifi off
            ;;
        down)
            nmcli radio wifi on
            ;;
    esac
fi

Don't forget afterwards:

chmod +x /etc/NetworkManager/dispatcher.d/99-wlan

This catches the legacy eth* names and the new kernel "predictable named interfaces" that start with en and then use either the bus path or the MAC address so that the name of each interface is the same on every boot. This worked with the USB-C (passthrough) and USB ethernet adapter I tried with and I'm confident it will work with built-in adapters as well.

Solution 2:

Since v0.9.10 of network-manager, the first script has to be modified

#!/bin/bash

if [ "$1" = "eth0" ]; then
    case "$2" in
        up)
            nmcli radio wifi off
            ;;
        down)
            nmcli radio wifi on
           ;;
   esac
fi

Hope it helps!