使用 libvirt 和 KVM 桥接网络,并将物理以太网接口连接到桥接器

使用 libvirt 和 KVM 桥接网络,并将物理以太网接口连接到桥接器

我按照本指南在虚拟机之间设置网络如何使用 libvirt 和 KVM 桥接网络

出现了两个问题:

  1. 系统重启后网络设置不再保留;
  2. 在虚拟客户机上,第二个网卡无法连接到路由器,没有显示网络连接

我在主机上创建了新的桥

sudo ip link add hostbr0 type bridge
sudo ip link show type bridge
# 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 8: mpqemubr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:87:65:df brd ff:ff:ff:ff:ff:ff
# 9: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
#     link/ether 02:42:e9:8e:81:fc brd ff:ff:ff:ff:ff:ff
# 10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff

我在主机上有两个网络接口,都连接到可以访问互联网的路由器。

name -v
#44~20.04.2-Ubuntu SMP Tue Oct 26 18:07:44 UTC 2021

ls -l /sys/class/net/ | grep pci
# enp4s0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:04:00.0/net/enp4s0
# enp5s0 -> ../../devices/pci0000:00/0000:00:1c.7/0000:05:00.0/net/enp5s0

我的路由器上的 DHCP 服务器连接到 Internet 配置:

IP地址池:192.168.0.1--192.168.0.254

默认网关:192.168.0.1

主机 enp4s0:192.168.0.11

hostbr0 enp5s0:192.168.0.10

我将enp5s0物理设备连接到网桥:

sudo ip link set enp5s0 up
sudo ip link set enp5s0 master hostbr0
sudo ip address add dev hostbr0 192.168.2.2/24
ip addr show hostbr0
#10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN #group default qlen 1000
#    link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff
#    inet 192.168.2.2/24 scope global hostbr0
#       valid_lft forever preferred_lft forever

为了使配置持久化,我修改了/etc/network/interfaces。如你所见,我已经配置了几个接口,并添加了一个新接口enp5s0

# /etc/network/interfaces
auto lo
iface lo inet loopback

mapping hotplug
        script grep
        map eth1

iface eth1 inet dhcp

auto eth0
iface eth0 inet static
    address 10.152.187.1
    netmask 255.255.255.0

auto wlan0
  iface wlan0 inet static
    address 192.168.1.1
    netmask 255.255.255.0
    up     /sbin/iwconfig wlan0 mode TTTTTT && /sbin/iwconfig wlan0 enc
restricted && /sbin/iwconfig wlan0 key [Y] XXXXXXXX && /sbin/iwconfig
wlan0 essid SSSSSSSS

auto eth1


# Specify that the physical interface that should be connected to the bridge
# should be configured manually, to avoid conflicts with NetworkManager
iface enp5s0 inet manual
# The hostbr0 bridge settings
auto hostbr0
iface hostbr0 inet static
    bridge_ports enp5s0
        address 192.168.2.11
        broadcast 192.168.2.255
        netmask 255.255.255.0
        gateway 192.168.0.11

建议禁用 netfilter:

sudo nano /etc/sysctl.d/99-netfilter-bridge.conf
# net.bridge.bridge-nf-call-ip6tables = 0
# net.bridge.bridge-nf-call-iptables = 0
# net.bridge.bridge-nf-call-arptables = 0

# load the settings written in the file
sudo modprobe br_netfilter

# to load the module automatically at boot
sudo nano /etc/modules-load.d/br_netfilter.conf
# br_netfilter

# load the settings in the 99-netfilter-bridge.conf
sudo sysctl -p /etc/sysctl.d/99-netfilter-bridge.conf

然后我在libvirt

# create a new virtual network
sudo nano /tmp/host-bridged-network.xml
# <network>
#     <name>host-bridged-network</name>
#     <forward mode="bridge" />
#     <bridge name="hostbr0" />
# </network>

sudo virsh net-define /tmp/host-bridged-network.xml
sudo virsh net-start host-bridged-network
sudo virsh net-autostart host-bridged-network
sudo virsh net-list
#  Name                   State    Autostart   Persistent
# ---------------------------------------------------------
#  default                active   yes         yes
#  host-bridged-network   active   yes         yes

virsh net-edit default
# <network>
#   <name>default</name>
#   <uuid>96dc6685-2006-4d88-9239-20f0b263f14b</uuid>
#   <forward mode='nat'/>
#   <bridge name='virbr0' stp='on' delay='0'/>
#   <mac address='52:54:00:8a:2e:73'/>
#   <ip address='192.168.122.1' netmask='255.255.255.0'>
#     <dhcp>
#       <range start='192.168.122.2' end='192.168.122.254'/>
#     </dhcp>
#   </ip>
# </network>

virsh net-edit host-bridged-network
# <network>
#   <name>host-bridged-network</name>
#   <uuid>762ec061-5a3d-417e-81ae-e06a920e0b13</uuid>
#   <forward mode='bridge'/>
#   <bridge name='hostbr0'/>
# </network>

有关我的主机最终配置的一些信息:

# on the host
ip route
# default via 192.168.0.1 dev enp4s0 proto dhcp metric 100 
# 10.139.212.0/24 dev mpqemubr0 proto kernel scope link src 10.139.212.1 linkdown 
# 169.254.0.0/16 dev virbr0 scope link metric 1000 linkdown 
# 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown 
# 192.168.0.0/24 dev enp4s0 proto kernel scope link src 192.168.0.11 metric 100 
# 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 

ip link
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
#     link/ether 50:3e:aa:0c:97:02 brd ff:ff:ff:ff:ff:ff
# 3: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master hostbr0 state UP mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff
# 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:8a:2e:73 brd ff:ff:ff:ff:ff:ff
# 8: mpqemubr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
#     link/ether 52:54:00:87:65:df brd ff:ff:ff:ff:ff:ff
# 9: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
#     link/ether 02:42:e9:8e:81:fc brd ff:ff:ff:ff:ff:ff
# 10: hostbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
#     link/ether 18:31:bf:cc:08:61 brd ff:ff:ff:ff:ff:ff

然后我向 ubuntu 20.04 客户虚拟机添加了两个网络接口:defaulthost-bridged-network。客户机照常启动,我可以在 Nemo 文件管理器中看到网络环境(仅限客户节点)。enp5s0 接口无法连接,Ubuntu 尝试建立连接,但总是失败。系统消息:接口的“激活网络连接失败” enp5s0。默认libvirt网络显示为enp9s0并正常工作。

在主机上该接口显示为unmanaged

我的问题是:

我应该为虚拟网络分配哪些地址范围?

系统重启后没有hostbr0创建,那么配置是否存在问题?

我也在主机上初始化了 DHCP 服务器,但目前未使用。也许最好将其删除?

我应该将两个网络都添加到客户机吗:defaulthost-bridged-network还是只添加host-bridged-network

桥接物理网络接口必须插入路由器吗?或者它提供主机上的所有功能,而第二个 NIC 通过 NAT 网络提供互联网?

请就地址分配提出改进建议,我怀疑我的配置有问题

答案1

这是我的最终配置,我在路由器上设置了 DHCP 服务器,它将 IP 地址分配给所有虚拟机以及主机。Samba 共享在此配置下运行良好。

我更喜欢通过连接到互联网的路由器上的 DHCP 服务器为主机和虚拟机设置永久 IP 地址。

  # /etc/netplan/01-network-manager-all.yaml
  network:
      version: 2
      renderer: networkd
      ethernets:
          eth0:
            match:
              macaddress: mac1
            set-name: tplink-usb3.0
            dhcp4: true
            dhcp6: false
            optional: true
            mtu: 1500
            nameservers:
              addresses: [8.8.8.8]
          eth-host:
            match:
              macaddress: mac2
            dhcp4: false
            dhcp6: false
      bridges:
          br0:
            interfaces: [eth-host]
            addresses: [192.168.0.9/24]
            gateway4: 192.168.0.1
            mtu: 1500
            nameservers:
              addresses: [8.8.8.8]
            parameters:
              stp: true
              forward-delay: 4
            dhcp4: true
            dhcp6: false

相关内容