绑定 WiFi 和以太网，以使网络间移动更轻松
网络
![](/data/attachment/album/201907/26/061716j1fgny0udgkyut0e.jpg)


有时一个网络接口是不够的。网络绑定允许将多条网络连接与单个逻辑接口一起工作。你可能因为需要给单条连接更多的带宽而这么做，或者你可能希望在有线和无线网络之间来回切换而不会丢失网络连接。


我是后面一种情况。在家工作的好处之一是，当天气晴朗时，在阳光明媚的阳台而不是在室内工作是很愉快的。但每当我这样做时，我都会失去网络连接。IRC、SSH、VPN，一切都断开了，客户端重连至少需要一会。本文介绍了如何在 Fedora 30 笔记本上设置网络绑定，以便从笔记本扩展坞的有线连接无缝切换到 WiFi。


在 Linux 中，接口绑定由内核模块 `bonding` 处理。默认情况下，Fedora 没有启用此功能，但它包含在 `kernel-core` 软件包中。这意味着启用接口绑定只需一个命令：



```
sudo modprobe bonding
```

请注意，这只会在你重启之前生效。要永久启用接口绑定，请在 `/etc/modules-load.d` 目录中创建一个名为 `bonding.conf` 的文件，该文件仅包含单词 `bonding`。


现在你已启用绑定，现在可以创建绑定接口了。首先，你必须获取要绑定的接口的名称。要列出可用的接口，请运行：



```
sudo nmcli device status
```

你将看到如下输出：



```
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     --
```

在本例中，有两个（有线）以太网接口可用。 `enp12s0u1` 在笔记本电脑扩展坞上，你可以通过 `STATE` 列知道它已连接。另一个是 `enp0s31f6`，是笔记本电脑中的内置端口。还有一个名为 `wlp2s0` 的 WiFi 连接。 `enp12s0u1` 和 `wlp2s0` 是我们在这里感兴趣的两个接口。（请注意，阅读本文无需了解网络设备的命名方式，但如果你感兴趣，可以查看 [systemd.net-naming-scheme 手册页](https://www.freedesktop.org/software/systemd/man/systemd.net-naming-scheme.html)。）


第一步是创建绑定接口：



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

在此示例中，绑定接口名为 `bond0`。`con-name bond0` 将连接名称设置为 `bond0`。直接这样做会有一个名为 `bond-bond0` 的连接。你还可以将连接名设置得更加人性化，例如 “Docking station bond” 或 “Ben”。


下一步是将接口添加到绑定接口：



```
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
```

如上所示，连接名称被设置为[更具描述性](https://en.wikipedia.org/wiki/Master/slave_(technology)#Terminology_concerns)。请务必使用系统上相应的接口名称替换 `enp12s0u1` 和 `wlp2s0`。对于 WiFi 接口，请使用你自己的网络名称 （SSID）替换我的 “Cotton”。如果你的 WiFi 连接有密码（这当然会有！），你也需要将其添加到配置中。以下假设你使用 [WPA2-PSK](https://en.wikipedia.org/wiki/Wi-Fi_Protected_Access#Target_users_(authentication_key_distribution)) 身份验证



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

第二条命令将进入交互式编辑器，你可以在其中输入密码，而无需将其记录在 shell 历史记录中。输入以下内容，将 `password` 替换为你的实际密码。



```
set wifi-sec.psk password
save
quit
```

现在，你可以启动你的绑定接口以及你创建的辅助接口。



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

你现在应该能够在不丢失网络连接的情况下断开有线或无线连接。


### 警告：使用其他 WiFi 网络时


在指定的 WiFi 网络间移动时，此配置很有效，但是当远离此网络时，那么绑定中使用的 SSID 就不可用了。从理论上讲，可以为每个使用的 WiFi 连接添加一个接口，但这似乎并不合理。相反，你可以禁用绑定接口：



```
sudo nmcli connection down bond0
```

回到定义的 WiFi 网络时，只需按上述方式启动绑定接口即可。


### 微调你的绑定


默认情况下，绑定接口使用“<ruby> 轮询 <rt>  round-robin </rt></ruby>”模式。这会在接口上平均分配负载。但是，如果你有有线和无线连接，你可能希望更喜欢有线连接。 `active-backup` 模式能实现此功能。你可以在创建接口时指定模式和主接口，或者之后使用此命令（绑定接口应该关闭）：



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

[内核文档](https://www.kernel.org/doc/Documentation/networking/bonding.txt)提供了有关绑定选项的更多信息。




---


via: <https://fedoramagazine.org/bond-wifi-and-ethernet-for-easier-networking-mobility/>


作者：[Ben Cotton](https://fedoramagazine.org/author/bcotton/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
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](https://www.freedesktop.org/software/systemd/man/systemd.net-naming-scheme.html).)

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](https://en.wikipedia.org/wiki/Master/slave_(technology)#Terminology_concerns). 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](https://en.wikipedia.org/wiki/Wi-Fi_Protected_Access#Target_users_(authentication_key_distribution)) 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](https://www.kernel.org/doc/Documentation/networking/bonding.txt) has much more information about bonding options.

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

## Matt

Awesome stuff !!

I’ve implemented this and it works flawlessly ..

thank you !!

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

## Ryan W.W.

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

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

## Paul W. Frields

@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?

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

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

## 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

?