31

What's the best way to automatically disable my Wifi/wireless connection whenever I plug in a wired Ethernet cable? I'd like it to be re-enabled when I later unplug the wire.

Also, I sometimes like to share the wired connection with other wireless users (or vice-versa). It'd be nice if I could somehow exempt these setups.

I'm using 11.10 Oneiric, which uses NetworkManager.

3 Answers3

35

The following script, put in /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired, mostly works—it disables wireless even when I want to share wired with wireless or vice-versa.

To do this, run the following command in terminal:

sudo nano /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired

And paste the following code into the text editor.

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

release=$(lsb_release -s -c)
case ${release} in
    trusty|utopic) nmobj=nm;;
    *) nmobj=radio;;
esac

case ${IFACE} in
    eth*|usb*|en*)
        case ${ACTION} in
            up)
                log "disabling wifi radio"
                nmcli "${nmobj}" wifi off
                ;;
            down)
                log "enabling wifi radio"
                nmcli "${nmobj}" wifi on
                ;;
        esac
        ;;
esac

Then save and exit.

Note the following conditions on the script, as documented in the NetworkManager manual page:

Each script should be:

  • a regular file,
  • owned by root,
  • not writable by group or other,
  • not set-uid,
  • and executable by the owner.

Instead of nmcli radio wifi off (or nmcli nm wifi off for older versions of NetworkManager), there is also rfkill block wifi. However, if rfkill is used instead of nmcli, newer versions of NetworkManager will turn wifi back on during boot.

  • How can the script tell which configuration you want? Why noy just do rfkill unblock wifi after you've connected eth0 and you want to share, then rfkill block wifi when you're done? – waltinator Mar 15 '12 at 01:40
  • @waltinator: When a connection comes up, NetworkManager sets an environment variable named CONNECTION_UUID to the identifier for that connection. I was thinking that the script could do something like only disable WiFi for the connection that I have declared the "standard" wired connection but leave it enabled if eth0 is activated by a different connection. I haven't tried it so I don't know if it works. But your point about just controlling it manually is a good one. – Richard Hansen Mar 15 '12 at 03:57
  • 1
    Use eth0|usb0) to also disable/enable WiFi when USB tethering is activated/deactivated. – krlmlr Nov 25 '13 at 12:32
  • 1
    Most useful! Instead of using rfkill, I used the commands nmcli nm wifi off and nmcli nm wifi on. But I don't have any particular reason for believing that the nmcli commands are better. I just wanted to do everything the NetworkManager way. – Jesse Hallett Jan 23 '14 at 01:35
  • syntax correction for those using the nmcli alternative given in the script above: INCORRECT: nmcli nm wifi on CORRECT: nmcli r wifi on The same correction goes for the "off" switch – doublehelix Dec 26 '14 at 20:15
  • @doublehelix: nmcli nm wifi off is correct—when I run nmcli r wifi off I get Error: Object 'r' is unknown, try 'nmcli help'. – Richard Hansen Dec 27 '14 at 00:42
  • @RichardHansen you are right - for Ubuntu. I'm coming from Debian and Fedora. Ubuntu's man page lists as valid object parameters: "nm | con | dev" while the latest nmcli (0.9.10.0) on Debian and Fedora both list as valid objects: "general | networking | radio | connection | device".

    Summary: If you are on Ubuntu "nm" works; if you are not use "r" (short for radio).

    – doublehelix Dec 27 '14 at 08:03
  • 4
    The switch statement should read "eth|usb|en)", because starting with udev197 the "Predictable Network Interface Names" [1] have been introduced. Network cards are thus named eno1/ens1 and similar and do not match the old eth and usb* naming. [1] https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/ – doublehelix Mar 26 '16 at 07:55
  • This script still works perfectly on Ubuntu 16.04.2 LT xenial (march 2017) – Cie6ohpa Mar 01 '17 at 10:51
  • @doublehelix the switch is eno|ens|enp|eth as policy 3 is applied quite commonly today (enp..) as well as policy 5 for fall back (eth..). Policy 5 is not disabled in Zesty so should still be supported. – shadowbq Sep 07 '17 at 13:55
  • @shadowbq: I've updated the answer to reflect that – doublehelix Mar 12 '18 at 12:30
  • On Kubuntu 20.04, using nmcli radio wifi off disables WiFi completely, so I cant create a hotspot. It fails with error: "Connection 'me-hotspot' is not available on device wlo1 because device is not available". – geekley Mar 01 '21 at 16:04
2

Based on the other answer, I came up with this (tested on Kubuntu 20.04).

It auto-disconnects wifi on wired connection, except for connection names ending with -hotspot (like the default names Kubuntu uses for created access point connections). It allows sharing wireless hotspot from cable. So, instead of disabling radio for all WiFi, it just disconnects the network you're using. When the cable is unplugged, it reconnects the wireless device wlo1, which makes it reconnect the last used WiFi network.

Note that if you turn off the computer without cable and plug it before turning it on, it may start with both wifi and wired connections. Similarly, I presume, if you remove the cable while computer is off, it won't auto-connect at startup, I think.

sudo nano /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired
sudo chmod 0744 /etc/NetworkManager/dispatcher.d/99-disable-wireless-when-wired
#!/bin/sh
myname=${0##*/}
log() { logger -p user.info -t "${myname}[$$]" "$*"; }
IFACE=$1
ACTION=$2
case ${IFACE} in
    eth*|usb*|en*)
        case ${ACTION} in
            up) # when plugging ethernet cable
                log "disconnecting wifi when wired"
                # list active connections
                nmcli -f uuid,type,name connection show --active |
                # filter wifi except names ending with -hotspot, return UUID
                awk '/\S\s+wifi\y/ && !/-hotspot\s*$/ {print $1;}' |
                # disconnect these UUIDs
                xargs -r nmcli connection down
                ;;
            down) # when unplugging ethernet cable
                log "reconnecting wifi when not wired"
                # auto-choose wifi to reconnect
                nmcli device connect wlo1
                ;;
        esac
        ;;
esac
geekley
  • 549
  • Thank you for your script. I Just had to do some minor changes to the awk to make it work on my kubuntu machine awk '/ +wifi +/ && !/-hotspot\s*$/ {print $1;}' | (the \S and \s did not work on my awk. also \y is completely unknown to me? – Daywalker Jun 17 '21 at 05:44
  • Very odd use of awk. Why don't you simply match $2 for wifi, i.e. awk '$2 ~ /^wifi$/ {print $1}', and be done with it? Also, why output the name field if not using it? And last but not least hardcoding the interface name in the down action when you first go through these hoops to make it work without hardcoding, is not a good style. – 0xC0000022L Jun 07 '22 at 19:34
  • (Side-note: the remark about wlo1 doesn't work on my Ubuntu 20.04). – 0xC0000022L Jun 07 '22 at 19:46
  • 1
    @0xC0000022L Yes, I have zero experience with awk, there's probably better ways to do it. But it worked for me, and I wanted to save the script somewhere I won't lose it haha – geekley Jun 07 '22 at 23:25
1

you can install tlp package and enable the following options in /etc/tlp.conf

DEVICES_TO_DISABLE_ON_LAN_CONNECT="wifi wwan"
DEVICES_TO_ENABLE_ON_LAN_DISCONNECT="wifi wwan"

and then

sudo service tlp restart
cipper
  • 111