Disable WLan if Wired/Cable Network is available

39

12

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.

Ingo Fischer

Posted 2011-01-14T22:27:00.950

Reputation: 643

The answer for this question varies from computer to computer. I don't know if Ubuntu allows this, but typically I've found it's an option in the BIOS. – Iszi – 2011-01-14T23:27:00.353

I don't mean completely disable the wlan, i mean disable Ubuntu to try to connect to any wlans if a wired connection is available. So it is an OS thing. – Ingo Fischer – 2011-01-14T23:37:27.570

Answers

57

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.

derhoch

Posted 2011-01-14T22:27:00.950

Reputation: 686

1Solid - works perfectly and nice minimal script. – Air – 2015-01-02T22:19:28.640

3With nmcli version 1.0.2 I had to change nmcli nm wifi with nmcli r wifi where r means radio – Alessandro Pezzato – 2015-06-08T06:41:58.843

If you are still using an older Ubuntu (why?) I'd suggest checking out this other answer with a smart detection script. https://askubuntu.com/a/112969/318507

– dragon788 – 2017-09-26T16:11:45.763

1Ubuntu 16.04, bash version 4.3.48, the quotes need to be removed for this script to work. – Carles Sala – 2018-10-05T16:06:46.450

@AlessandroPezzato I use nmcli 1.10.6 and radio works just fine, even though just r would work as well. So the above sctipt works magic! – sebisnow – 2019-11-25T13:39:33.950

My wireless was trying to connect again and again and it was starting to be really irritating. Thanks for this script, it finally stopped asking me for a network password. Will the script be automatically started when I reboot my computer ? – Joel Lord – 2012-09-20T12:43:00.007

2@joellord: This script is automatically run every time a network interface starts or stops. When you disconnect eth0, your wireless gets enabled. When you connect eth0, your wireless gets disabled. – phord – 2012-11-28T16:05:25.723

What does 99 in the file name mean? – day – 2014-04-25T15:41:06.427

The scripts in that directory are executed in lexical order, so it is common to prefix the filenames with a number to control the order of execution. – derhoch – 2014-04-25T19:48:41.763

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!

mruellan

Posted 2011-01-14T22:27:00.950

Reputation: 181

This can probably be a standalone answer. It would help hugely to say what version of network manager this change happens with. – Journeyman Geek – 2014-09-28T08:29:55.763

seems to be Since v0.9.10. https://wiki.gnome.org/Projects/NetworkManager/nmcli

– mruellan – 2014-09-29T12:37:29.103

1This solution is not right, as it only works if your lan interface is called "eth0". – Carles Sala – 2018-10-05T16:04:52.233

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.

rjbradlow

Posted 2011-01-14T22:27:00.950

Reputation: 49

2

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

ggustafsson

Posted 2011-01-14T22:27:00.950

Reputation: 1 698

Thanks, i already read about that. What i don't like about ifplugd is that i have to put all my network settings into /etc/network/interfaces. I'm not very good at networking stuff, so i wonder where i could get all the needed values out of my existing connections? Still hope that there is a simpler solution. – Ingo Fischer – 2011-01-15T11:22:16.670

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=""

Luis Cellino

Posted 2011-01-14T22:27:00.950

Reputation: 11

Does "docked" mean plugged into power or plugged into a literal docking station/port replicator or simply plugged into something that provides a LAN connection? – dragon788 – 2017-07-19T19:01:46.597

1

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.

joentjuh

Posted 2011-01-14T22:27:00.950

Reputation: 11

This is probably functionally equivalent to the accepted nmcli radio wifi off answers but I would be curious to see what the system logs report when closing down the connections both ways to see if one is more "graceful". – dragon788 – 2017-07-19T19:00:42.837

1

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

Askmeanything

Posted 2011-01-14T22:27:00.950

Reputation: 33

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

Steven

Posted 2011-01-14T22:27:00.950

Reputation: 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.

KIT-Inwi

Posted 2011-01-14T22:27:00.950

Reputation: 1

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).

Andy Forceno

Posted 2011-01-14T22:27:00.950

Reputation: 121

What is the systemd way to do this? – Xofo – 2019-05-10T03:03:00.907