在Ubuntu20上创建桥接网络和虚拟机

1) 网络管理相关命令

之前常用的包含在 net-tools 工具包里的命令 ifconfig、netstat 已经被 iproute2 工具包里的 ip 等命令所取代。
所以以后在非遗留系统,就没必要再使用不被维护的老命令了。

ip link show
ip link show eth0
ip link set eth0 up|down

ip addr show
ip addr show eth0
ip add add|del 192.168.0.99/24 eth0

ip route
ip neigh

ss -l
ss -a

lsof -i // -i 网络 -u 用户  -p 进程ID -c 进程名

2) Ubuntu 网络管理

如果是 Ubuntu Desktop 环境,就不妨使用 NetworkManager 和 nm-connection-editor 这些 GUI 程序或者 nmcli、nmtui 来管理网络及设备。
如果是 Ubuntu Server 环境,还是用 systemd-netword 比较好。
NetworkManager 和 networkd 是有冲突的,选择使用一个之后另一种方式需要被禁用。下面的命令是操作相关服务所用的命令,其中 enable/disable 用于设置是否开机启动而 mask/unmask 则用于设置服务是否可用。选择使用 NetworkManager 之后检查以下 cat /etc/NetworkManager/NetworkManager.conf,确保 managered 被设置为 true。

sudo systemctl stop NetworkManager
sudo systemctl disable NetworkManager
sudo systemctl mask NetworkManager

sudo systemctl unmask systemd-networkd.service
sudo systemctl enable systemd-networkd.service
sudo systemctl start systemd-networkd.service

在 NetworkManager 和 networkd 之上,我们可以使用 netplan 来进行更高层次的控制。参考:https://netplan.io/

Netplan is a utility for easily configuring networking on a linux system. You simply create a YAML description of the required network interfaces and what each should be configured to do. From this description Netplan will generate all the necessary configuration for your chosen renderer tool.

3)创建网桥、虚拟机,设置虚拟机桥接网卡。

实验环境使用 Ubuntu20 Desktop 做宿主机,感觉用 nm-connection-editor 创建网桥、配置网桥出口也很方便、顺利。具体步骤可参考http://www.zrway.com/news/8366.html。

下图是通过 nm-connection-editor 创建出网桥,并把物理网卡插到这个网桥上。注意,把网卡作为网桥的 salver 设备之后,需要通过 GUI 删除这个网络网卡。

创建网桥

通过 virt-manager 创建出虚拟机后,需要设置虚拟机的网卡。这里有个 QEMU 前端设备、后端后端设备(backend network)的概念。所谓前端设备就是 Guest 虚拟机看到的设备,后端设备、后端网络就是在 Host 宿主机上的设备、网络。下图实际上大致对应这样的命令: qemu -net nic,model=e1000 -net bridge,br=bridge1 …

设置网桥

下图是/etc/netplan 目录里的配置文件。

参考:
netplan: https://ubuntu.com/blog/ubuntu-bionic-netplan
图形化管理工具: https://ubuntu.com/core/docs/networkmanager
网络管理服务: https://www.configserverfirewall.com/ubuntu-linux/ubuntu-network-manager/
创建网桥: http://www.zrway.com/news/8366.html