Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
wxy 2018-04-04 20:12:29 +08:00
commit 759149f5f2

View File

@ -1,51 +1,52 @@
Translating by kennethXia
How to add network bridge with nmcli (NetworkManager) on Linux
如何在Linux里使用nmcli NetworkManager 添加网桥
======
I am using Debian Linux 9 "stretch" on the desktop. I would like to create network bridge with NetworkManager. But, I am unable to find the option to add br0. How can I create or add network bridge with nmcli for NetworkManager on Linux?
我正在电脑上使用Debian Linux 9 “stretch”。 我想用NetworkManager 来建网桥。但是根本就没有添加br0的选项。我该如何在Linux里使用nmcli 来为 NetworkManager 创建或者添加网桥呢?
A bridge is nothing but a device which joins two local networks into one network. It works at the data link layer, i.e., layer 2 of the OSI model. Network bridge often used with virtualization and other software. Disabling NetworkManager for a simple bridge especially on Linux Laptop/desktop doesn't make any sense. The nmcli tool can create Persistent bridge configuration without editing any files. **This page shows how to create a bridge interface using the Network Manager command line tool called nmcli**.
网桥没什么特别的只是把两个网络连在一起。它工作在数据链路层例如OSI模型的第二层。网桥经常被虚拟机和别的一些软件使用。为了使用网桥而关闭桌面Linux上的NetworkManager 显然是不明智的。nmcli可以创建一个永久的网桥而不编辑任何文件。**本文将展示如何使用NetworkManager 的命令行工具nmcli来创建网桥。**
### How to create/add network bridge with nmcli
### 如何使用nmcli来创建/添加网桥
The procedure to add a bridge interface on Linux is as follows when you want to use Network Manager:
使用NetworkManager 在Linux上添加网桥接口的步骤如下
1. Open the Terminal app
2. Get info about the current connection:
1. 打开terminal
2. 获取当前连接状态:
```
nmcli con show
```
3. Add a new bridge:
3. 添加新的网桥:
```
nmcli con add type bridge ifname br0
```
4. Create a slave interface:
4. 创建子网卡:
```
nmcli con add type bridge-slave ifname eno1 master br0
```
5. Turn on br0:
5. 打开br0:
```
nmcli con up br0
```
Let us see how to create a bridge, named br0 in details.
让我们从细节层面看看如何创建一个名为br0的网桥。
### Get current network config
### 获取当前网络配置
You can view connection from the Network Manager GUI in settings:
你可以通过NetworkManager的GUI来了解本机的网络连接:
[![Getting Network Info on Linux][1]][1]
Another option is to type the following command:
也可以使用如下命令行来查看:
```
$ nmcli con show
$ nmcli connection show --active
```
[![View the connections with nmcli][2]][2]
I have a "Wired connection 1" which uses the eno1 Ethernet interface. My system has a VPN interface too. I am going to setup a bridge interface named br0 and add, (or enslave) an interface to eno1.
### How to create a bridge, named br0
我有一个使用网卡eno1的“有线连接”。我的系统还有一个VPN接口。我将要创建一个名为br0的网桥并连接到eno1.
### 如何创建一个名为br0的网桥
```
$ sudo nmcli con add ifname br0 type bridge con-name br0
@ -53,13 +54,14 @@ $ sudo nmcli con add type bridge-slave ifname eno1 master br0
$ nmcli connection show
```
[![Create bridge interface using nmcli on Linux][3]][3]
You can disable STP too:
你也可以禁用STP:
```
$ sudo nmcli con modify br0 bridge.stp no
$ nmcli con show
$ nmcli -f bridge con show br0
```
The last command shows the bridge settings including disabled STP:
最后一条命令展示了禁用STP后的网桥参数:
```
bridge.mac-address: --
bridge.stp: no
@ -72,26 +74,26 @@ bridge.multicast-snooping: yes
```
### How to turn on bridge interface
### 如何打开网桥
You must turn off "Wired connection 1" and turn on br0:
你必须先关闭"Wired connection 1" ,然后打开br0:
```
$ sudo nmcli con down "Wired connection 1"
$ sudo nmcli con up br0
$ nmcli con show
```
Use [ip command][4] to view the IP settings:
使用 [ip 命令][4] 来查看IP信息:
```
$ ip a s
$ ip a s br0
```
[![Build a network bridge with nmcli on Linux][5]][5]
### Optional: How to use br0 with KVM
### 附录: 如何在KVM上使用br0
Now you can connect VMs (virtual machine) created with KVM/VirtualBox/VMware workstation to a network directly without using NAT. Create a file named br0.xml for KVM using vi command or [cat command][6]:
现在你可以使用KVM/VirtualBox/VMware workstation创建的VM虚拟机来直接连接网络而非通过NAT。使用vi或者cat命令[cat command][6]为虚拟机创建一个名为br0.xml的文件:
`$ cat /tmp/br0.xml`
Append the following code:
添加以下代码:
```
<network>
<name>br0</name>
@ -100,14 +102,14 @@ Append the following code:
</network>
```
Run virsh command as follows:
如下所示运行virsh命令:
```
# virsh net-define /tmp/br0.xml
# virsh net-start br0
# virsh net-autostart br0
# virsh net-list --all
```
Sample outputs:
输出:
```
Name State Autostart Persistent
----------------------------------------------------------
@ -116,22 +118,22 @@ Sample outputs:
```
For more info read the following man page:
阅读man页面获取更多信息:
```
$ man ip
$ man nmcli
```
### about the author
### 关于作者
The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on [Twitter][7], [Facebook][8], [Google+][9].
作者是nixCraft的创建者老练的系统管理员和一个Linux/Unix shell脚本编程培训师。他为全球客户和各种公司工作包括IT教育国防空间研究以及非营利组织。 他的联系方式[Twitter][7], [Facebook][8], [Google+][9].
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/how-to-add-network-bridge-with-nmcli-networkmanager-on-linux/
作者:[Vivek Gite][a]
译者:[译者ID](https://github.com/译者ID)
译者:[kennethXia](https://github.com/kennethXia)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出