Ilija Matoski

    ← Articles

    Automatically switch between WiFi and Ethernet

    I wanted a way to automatically switch between wifi and ethernet when I connect the system to the ethernet port, or auto start wifi when I disconnect the ethernet port.

    Simplest way to do so was to use NetworkManager to enable/disable wifi, when we detect an ethernet connection.

    I also wanted a way to track what is happening so we log to syslog our actions, and a way to disable the functionality without removing the file from they system.

    Open up your favorite editor and create the file bellow with the contents.

    /etc/NetworkManager/dispatcher.d/70-wifi-wired-exclusive.sh

    #!/usr/bin/env bash
    name_tag="wifi-wired-exclusive"
    syslog_tag="$name_tag"
    skip_filename="/etc/NetworkManager/.$name_tag"
    if [ -f "$skip_filename" ]; then
    exit 0
    fi
    interface="$1"
    iface_mode="$2"
    iface_type=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f2)
    iface_state=$(nmcli dev | grep "$interface" | tr -s ' ' | cut -d' ' -f3)
    logger -i -t "$syslog_tag" "Interface: $interface = $iface_state ($iface_type) is $iface_mode"
    enable_wifi() {
    logger -i -t "$syslog_tag" "Interface $interface ($iface_type) is down, enabling wifi ..."
    nmcli radio wifi on
    }
    disable_wifi() {
    logger -i -t "$syslog_tag" "Disabling wifi, ethernet connection detected."
    nmcli radio wifi off
    }
    if [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "down" ]; then
    enable_wifi
    elif [ "$iface_type" = "ethernet" ] && [ "$iface_mode" = "up" ] && [ "$iface_state" = "connected" ]; then
    disable_wifi
    fi

    If we want to disable this script, instead of deleting it we can just do

    touch /etc/NetworkManager/.wifi-wired-exclusive

    And this will skip the script everytime it’s invoked.

    And here are some details from the syslog, as you can see it turns off the wifi if there is an ethernet connection on the system, and turn on the wifi if the ethernet connection is disabled.

    Dec 20 22:47:13 x1 wifi-wired-exclusive[7980]: Interface: enx0050b6c0408e = connected (ethernet) is up
    Dec 20 22:47:13 x1 wifi-wired-exclusive[7981]: Disabling wifi, ethernet connection detected.
    Dec 20 22:47:13 x1 wifi-wired-exclusive[8051]: Interface: wlp4s0 = unavailable (wifi) is down
    Dec 20 22:47:21 x1 wifi-wired-exclusive[8097]: Interface: enx0050b6c0408e = unavailable (ethernet) is down
    Dec 20 22:47:21 x1 wifi-wired-exclusive[8098]: Interface enx0050b6c0408e (ethernet) is down, enabling wifi ...
    Dec 20 22:47:25 x1 wifi-wired-exclusive[8252]: Interface: wlp4s0 = connected (wifi) is up
    Dec 20 22:47:31 x1 wifi-wired-exclusive[8407]: Interface: enx0050b6c0408e = connected (ethernet) is up
    Dec 20 22:47:31 x1 wifi-wired-exclusive[8408]: Disabling wifi, ethernet connection detected.
    Dec 20 22:47:31 x1 wifi-wired-exclusive[8474]: Interface: wlp4s0 = unavailable (wifi) is down
    Dec 20 22:47:42 x1 wifi-wired-exclusive[8525]: Interface: enx0050b6c0408e = unavailable (ethernet) is down
    Dec 20 22:47:42 x1 wifi-wired-exclusive[8526]: Interface enx0050b6c0408e (ethernet) is down, enabling wifi ...
    Dec 20 22:47:46 x1 wifi-wired-exclusive[8679]: Interface: wlp4s0 = connected (wifi) is up