55

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.

10 Answers10

77

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.

Benjamin Loison
  • 183
  • 1
  • 6
derhoch
  • 886
8

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!

Benjamin Loison
  • 183
  • 1
  • 6
mruellan
  • 181
4

Quite simply for the gnome GUI approach...

  1. Right click on the network system indicator in the gnome panel up by your clock. (The indicator will be one of two icons; either the up/down arrows (LAN) or the traditional WiFi Funnel. Note that the up/down icon will appear when both LAN & WiFi or only LAN are connected and the WiFi funnel appears when connected via WiFi ONLY. (LAN Disconnected)) -- [LAN trumps WiFi automatically.*]

  2. Select 'Edit Connections...'

  3. Select the 'Wireless' tab.
  4. Double click the first connection in your list and Uncheck the 'Connect automatically' box.
  5. Click the 'Apply...' button.
  6. Repeat for each connection in the list.

This will leave the Wireless network operational for on-the-fly manual connections and disconnections available by left clicking the network icon, without the NM trying to Automatically connect you all the time.

Naturally you could also disable/enable Wireless by right clicking the network icon and then left clicking on the "Enable Wireless' selection, effectively bringing down or up the Wireless interface as indicated by the presence or absence of the check mark.

  • LAN trumps WiFi automatically, there is no need to disable WiFi. Simply unplugging your Ethernet cable will seamlessly transfer the connection to WiFi and you can pick up and move about without any fuss. Likewise, with reconnecting the LAN.
  • While LAN does trump WiFi the NM (Network Manager) will find what you seek should you be on different networks simultaneously and are working both online (WiFi) and with a local host (LAN) or V/V for example.
3

Just a guess but i assume ifplugd could help. You could make it shut down wifi when cable is used.

ggustafsson
  • 2,024
2

Create two simple 'scripts', the name of the script is not important (I simply use wlan) and I assume there is only one cabled network interface, and is thus called 'eth0'... Check this with 'ifconfig' if you're not sure. Note that this disabled wireless entirely, not just wlan0. (Only an issue if you have multiple wlan interfaces and only want to disable specific ones)

These scripts could easily be adapted - by boolean logic - to a situation in which you have two or more cabled network interfaces.

Make sure these scripts are executable with 'chmod +x'

/etc/network/ip-up.d/wlan

#!/bin/sh
# If eth0 goes up, disable wireless
if [ "$IFACE" = "eth0" ]; then
  dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.DBus.Properties.Set string:org.freedesktop.NetworkManager string:WirelessEnabled variant:boolean:false
fi

/etc/network/if-down.d/wlan

#!/bin/sh
# If eth0 goes down, enable wireless
if [ "$IFACE" = "eth0" ]; then
  dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.DBus.Properties.Set string:org.freedesktop.NetworkManager string:WirelessEnabled variant:boolean:true
fi

This enables/disables wireless in the NetworkManager that can usually be found as an system indicator in the Gnome panel.

You could also use 'ifconfig wlan0 down' or 'ifconfig wlan0 up' instead of the dbus-send line, but this should be more user-friendly and interfere less with Ubuntu's system utilities.

Tested with Ubuntu Desktop 10.10, and should work with earlier versions or other distributions using NetworkManager and dbus.

Benjamin Loison
  • 183
  • 1
  • 6
2

This works for me in Debian unstable, kernel >3.17

#!/bin/sh
myname=$(basename "$0") || exit 1
log() { logger -p user.info -t "${myname}[$$]" "$*"; }
IFACE=$1
ACTION=$2

case ${IFACE} in eth|usb) case ${ACTION} in up) nmcli r wifi off ;; down) nmcli r wifi on ;; esac ;; esac

Benjamin Loison
  • 183
  • 1
  • 6
1

If you are already using tlp for power management, it has a feature to do this.

You have to modify your conf file (/etc/default/tlp)

# Radio devices to enable/disable when docked.
#DEVICES_TO_ENABLE_ON_DOCK=""
DEVICES_TO_DISABLE_ON_DOCK="wifi wwan"

Radio devices to enable/disable when undocked.

DEVICES_TO_ENABLE_ON_UNDOCK="wifi" #DEVICES_TO_DISABLE_ON_UNDOCK=""

Benjamin Loison
  • 183
  • 1
  • 6
1

This is an improvement on Cyril Fessl's previous answer. (I don't have the reputation to comment.) This one works for Fedora as well, where network interfaces can now have names like wlan0, wlp6s0, em1 and enp0s20u2u1). This variation does not try to match on the interface name, but rather looks in /sys/class/net for information on the device. Works on my Fedora 21 laptop (kernel 3.18), and I believe it will work on Debian >= 7 as well.

#!/bin/sh

[ $# -ge 2 ] || exit 1

DEBUG=false STATEDIR=/var/run/nm-wired mkdir -p $STATEDIR

IFACE=$1 ACTION=$2

myname=$(basename "$0") || exit 1 log() { logger -p user.info -t "${myname}[$$]" "$IFACE/$ACTION: $*"; }

if $DEBUG; then if [ -e "/sys/class/net/$IFACE/device" ]; then log "/sys/class/net/$IFACE/device exists" else log "/sys/class/net/$IFACE/device does not exist" fi

if [ -e "/sys/class/net/$IFACE/wireless" ]; then
    log "/sys/class/net/$IFACE/wireless exists"
else
    log "/sys/class/net/$IFACE/wireless does not exist"
fi

fi

case ${ACTION} in up) rm -rf $STATEDIR/$IFACE

    # Don't do anything if this is not a physical device.
    if [ ! -e "/sys/class/net/$IFACE/device" ]; then
        log "$IFACE not a physical device -- ignoring"
        exit 0
    fi

    # Don't do anything if this is a wireless device.
    if [ -d "/sys/class/net/$IFACE/wireless" ]; then
        log "$IFACE not a wired device -- ignoring"
        exit 0
    fi

    # Keep track of wired devices. When they go down, the
    # device node may go as well (e.g. USB Ethernet dongle),
    # so we'd have no way of telling what type the device was.
    touch $STATEDIR/$IFACE

    # Now shut down WiFi.
    log "shutting down WiFi"
    nmcli r wifi off
    ;;
down)
    # Check whether we previously recognised $IFACE as a
    # physical, wired device.
    if [ ! -e $STATEDIR/$IFACE ]; then
        log "$IFACE not a wired device -- ignoring"
        exit 0
    fi

    rm -rf $STATEDIR/$IFACE

    # Instead of checking a single file, we could also check
    # whether there are still files in $STATEDIR. If so, we
    # still have a wired device enabled...
    log "enabling WiFi"
    nmcli r wifi on
    ;;

esac

Benjamin Loison
  • 183
  • 1
  • 6
Steven
  • 51
0

If you want to use the solution and make sure it works even if you suspend while ethernet is connected and then unplug and wake up your device, you can use the pre-down action instead of/additionally to just down. The pre-down action is triggered on suspend whereas down is not.

0

For whatever reason, the current top answer by derhoch does not work for me, even though it should. Some of the other suggestions do work, but I wanted something very simple. So, I'm using the following script (which I put in /etc/NetworkManager/dispatcher.d/ to turn wifi on and off depending on eth0's status.

#!/bin/bash
# Enable/disable wlan0 depending on eth0 and wlan0 current state

eth0_status=$(cat /sys/class/net/eth0/operstate) wlan0_status=$(cat /sys/class/net/wlan0/operstate)

if [[ "$eth0_status" = "up" ]]; then nmcli nm wifi off elif [[ "$wlan0_status" = "down" ]] && [[ "$eth0_status" = "down" ]]; then nmcli nm wifi on else nmcli nm eth0 on nmcli nm wlan0 off

fi

The else statement is probably unnecessary, and might even be an issue under some conditions, but I left it there just in case (without that statement, if eth0 is down, it never comes up).

Benjamin Loison
  • 183
  • 1
  • 6