Bond WiFi and Ethernet for easier networking mobility

Sometimes one network interface isn’t enough. Network bonding allows multiple network connections to act together with a single logical interface. You might do this because you want more bandwidth than a single connection can handle. Or maybe you want to switch back and forth between your wired and wireless networks without losing your network connection.

The latter applies to me. One of the benefits to working from home is that when the weather is nice, it’s enjoyable to work from a sunny deck instead of inside. But every time I did that, I lost my network connections. IRC, SSH, VPN — everything goes away, at least for a moment while some clients reconnect. This article describes how I set up network bonding on my Fedora 30 laptop to seamlessly move from the wired connection my laptop dock to a WiFi connection.

In Linux, interface bonding is handled by the bonding kernel module. Fedora does not ship with this enabled by default, but it is included in the kernel-core package. This means that enabling interface bonding is only a command away:

sudo modprobe bonding

Note that this will only have effect until you reboot. To permanently enable interface bonding, create a file called bonding.conf in the /etc/modules-load.d directory that contains only the word “bonding”.

Now that you have bonding enabled, it’s time to create the bonded interface. First, you must get the names of the interfaces you want to bond. To list the available interfaces, run:

sudo nmcli device status

You will see output that looks like this:

DEVICE          TYPE      STATE         CONNECTION         
enp12s0u1       ethernet  connected     Wired connection 1
tun0            tun       connected     tun0               
virbr0          bridge    connected     virbr0             
wlp2s0          wifi      disconnected  --      
p2p-dev-wlp2s0  wifi-p2p disconnected  --      
enp0s31f6       ethernet  unavailable   --      
lo              loopback  unmanaged     --                 
virbr0-nic      tun       unmanaged     --       

In this case, there are two (wired) Ethernet interfaces available. enp12s0u1 is on a laptop docking station, and you can tell that it’s connected from the STATE column. The other, enp0s31f6, is the built-in port in the laptop. There is also a WiFi connection called wlp2s0. enp12s0u1 and wlp2s0 are the two interfaces we’re interested in here. (Note that it’s not necessary for this exercise to understand how network devices are named, but if you’re interested you can see the systemd.net-naming-scheme man page.)

The first step is to create the bonded interface:

sudo nmcli connection add type bond ifname bond0 con-name bond0

In this example, the bonded interface is named bond0. The “con-name bond0” sets the connection name to bond0; leaving this off would result in a connection named bond-bond0. You can also set the connection name to something more human-friendly, like “Docking station bond” or “Ben”

The next step is to add the interfaces to the bonded interface:

sudo nmcli connection add type ethernet ifname enp12s0u1 master bond0 con-name bond-ethernet
sudo nmcli connection add type wifi ifname wlp2s0 master bond0 ssid Cotton con-name bond-wifi

As above, the connection name is specified to be more descriptive. Be sure to replace enp12s0u1 and wlp2s0 with the appropriate interface names on your system. For the WiFi interface, use your own network name (SSID) where I use “Cotton”. If your WiFi connection has a password (and of course it does!), you’ll need to add that to the configuration, too. The following assumes you’re using WPA2-PSK authentication

sudo nmcli connection modify bond-wifi wifi-sec.key-mgmt wpa-psk
sudo nmcli connection edit bond-wif

The second command will bring you into the interactive editor where you can enter your password without it being logged in your shell history. Enter the following, replacing password with your actual password

set wifi-sec.psk password
save
quit

Now you’re ready to start your bonded interface and the secondary interfaces you created

sudo nmcli connection up bond0
sudo nmcli connection up bond-ethernet
sudo nmcli connection up bond-wifi

You should now be able to disconnect your wired or wireless connections without losing your network connections.

A caveat: using other WiFi networks

This configuration works well when moving around on the specified WiFi network, but when away from this network, the SSID used in the bond is not available. Theoretically, one could add an interface to the bond for every WiFi connection used, but that doesn’t seem reasonable. Instead, you can disable the bonded interface:

sudo nmcli connection down bond0

When back on the defined WiFi network, simply start the bonded interface as above.

Fine-tuning your bond

By default, the bonded interface uses the “load balancing (round-robin)” mode. This spreads the load equally across the interfaces. But if you have a wired and a wireless connection, you may want to prefer the wired connection. The “active-backup” mode enables this. You can specify the mode and primary interface when you are creating the interface, or afterward using this command (the bonded interface should be down):

sudo nmcli connection modify bond0 +bond.options "mode=active-backup,primary=enp12s0u1"

The kernel documentation has much more information about bonding options.

For System Administrators Using Hardware

11 Comments

  1. Arjen Heidinga

    You say you already have VPN…
    You could also just put wireguard on your homeserver, and laptop/phone. Keep it connected at all times, and it is as if you are at home wherever you are.
    Switch back and forth between networks (even outside your home) and all your connection stay alive.
    There is the slight setback of latency. I honestly turn it off when gaming. For all other purposes it is fast enough, even on my puny 200/200 MBit home connection.

  2. Matt

    Awesome stuff !!
    I’ve implemented this and it works flawlessly ..
    thank you !!

  3. Joao Rodrigues

    This only works if the wireless and wired interface are on the same network right?

    Let’s say that I’m at a place where wireless connections go to a 10.0.0.0/8 network that NAT to a public IP address and wired connections go to a 192.168.0.0/24 that NAT to a different public IP address.

  4. This is a fantastic write-up, Ill need to get this working for my desktop that has a spare wireless card in it.

  5. Umair

    Why would you use bonding when you do same thing for teaming. Teaming is recommended as it work far better then bonding and works in user space.

    • @Umair: Why would you merely comment on an article when you could write your own for the Magazine? 🙂

      • Umair

        Was not trying to be rude, just trying to understand logic to use bonding instead of teaming. Yeah I’m thinking to starting writing for Fedora Magazine because I really like fedora 🙂 Couldn’t imagine my work life without it.

    • Marek

      Is it possible to use teamd with wireless interface?

  6. Bruce

    To be clear, bonding for speed would only make sense when an ISP can deliver or receive packets faster than your computer’s NIC can send or receive them or when you are connected to more than one networking simultaneously.

  7. Pranav

    Did not work for me I’m afraid. Traffic does not failover to the backup wireless interface and just stops after I down the primary active ethernet interface.

    /proc/net/bonding/bond0 shows two slave interfaces are up.

    Everything is happy if I bring the primary back up. I’m using wpa-eap/peap/mschapv2 for wireless auth if it makes any difference.

  8. Marek Łukasz

    How creating a bounding connection in ssh?
    For example I have 2 host (meybe with 2 wan network)
    and need send my dynamic package -D from 2 host (round robin).
    Or I can use this same port -R 7000:localhost:443 on 2 different computers and simulating sending transmision from 2 host simultanice
    ?

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions