我在为主机和虚拟机设置网桥时遇到了很多问题。
主机(服务器操作系统)是 Ubuntu 20.04.4 LTS。虚拟机(使用 KVM 和 virt-manager)是 TrueNAS Core (FreeBSD)。我的服务器(主板上)上有一个以太网端口,我想将其用于操作系统和虚拟机。操作系统需要获取 192.168.2.15 的 IP,并将 DNS 设置为 1.1.1.1,而虚拟机需要获取 192.168.2.16 的 IP。它们都需要从“外部”访问。因此,据我所知,我需要设置一个网桥。
一切都不对劲。我开始这视频,但没有成功。然后我尝试这视频,但也没有成功。然后我尝试这指南,但再次没有成功。然后我转向 SO 并尝试了这些(1,2) 但均未成功。
每次都会出错。大多数时候 DNS 不起作用,或者 IP 地址混乱(完全不同的子网),或者操作系统有正确的 IP 但无法 ping 任何其他设备(不是 1.1.1.1,也不是 192.168.2.79,这是我的静态 IP win10 计算机)或 /etc/network/interfaces 被忽略(该文件尚不存在)。而且大多数时候人们建议使用或sudo /etc/init.d/networking restart
类似使用 systemd,但每次我都会收到不存在的消息networking
,我觉得这很奇怪,因为其他人都做有它。
一切都乱糟糟的,我没法让它工作。我真的很需要帮助!
该文件/etc/network/interfaces
目前不存在。
ifconfig -a
输出:
enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.15 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::8506:f97f:df7c:f66b prefixlen 64 scopeid 0x20<link>
ether 18:c0:4d:9b:3c:12 txqueuelen 1000 (Ethernet)
RX packets 10390 bytes 3524106 (3.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2534 bytes 330756 (330.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0xfc600000-fc61ffff
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 7172 bytes 712208 (712.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7172 bytes 712208 (712.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:06:8d:b0 txqueuelen 1000 (Ethernet)
RX packets 142 bytes 12152 (12.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40 bytes 4136 (4.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:06:8d:b0 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::fc54:ff:fee6:a823 prefixlen 64 scopeid 0x20<link>
ether fe:54:00:e6:a8:23 txqueuelen 1000 (Ethernet)
RX packets 142 bytes 14140 (14.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 939 bytes 55015 (55.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
编辑:里面/etc/netplan
有一个文件:01-network-manager-all.yaml
。内容:
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
我不在乎网络管理器。如果有某种解决方案可行但会破坏网络管理器,我完全可以接受。
答案1
我仅使用过 networkd 作为渲染器,而没有使用 NetManager,所以我不知道这个答案的价值。
保存/etc/netplan/01-network-manager-all.yaml
在某处,这样如果事情不起作用,你可以恢复。
创建/etc/netplan/01-netcfg.yaml
(假定网关地址):
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp4s0:
dhcp4: no
bridges:
br0:
interfaces: [ enp4s0 ]
dhcp4: no
addresses: [192.168.2.15/24]
gateway4: 192.168.2.1
nameservers:
addresses: [1.1.1.1]
dhcp6: no
link-local: [ ]
parameters:
stp: true
forward-delay: 4
做:
sudo netplan apply
转到/etc/libvirt/qemu/networks
。应该有一个host-bridge.xml
文件。保存一份副本:
sudo cp host-bridge.xml host-bridge.xml.original
如果你没有host-bridge.xml
文件,我的原文是:
doug@s19:~/config/etc/libvirt/qemu/networks$ cat host-bridge.xml.original
<network>
<name>host-bridge</name>
<bridge name='br0'/>
<forward mode="bridge"/>
</network>
运行以下命令:
virsh net-define host-bridge.xml
virsh net-autostart host-bridge
virsh net-start host-bridge
virsh net-list --all
如果遇到问题并需要撤消,请注意:
virsh net-destroy br0
virsh net-undefine br0
例子:
doug@s19:~/config/etc/libvirt/qemu/networks$ virsh net-list --all
Name State Autostart Persistent
------------------------------------------------
host-bridge active yes yes
例子:
doug@s19:~/config/etc/libvirt/qemu/networks$ sudo cat /etc/libvirt/qemu/networks/host-bridge.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit host-bridge
or other application using the libvirt API.
-->
<network>
<name>host-bridge</name>
<uuid>d474d859-8448-4055-9fc0-bb0b6cb83c34</uuid>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>
现在,修改任何现有的虚拟机以使用桥接器:
注意:可能有一种方法可以使用来完成这一步virt-manager
,但是我没有使用它并且不知道如何操作。
使用virsh edit
并更改接口定义行以使用桥接而不是当前正在使用的。如下所示:
前:
<interface type='network'>
<mac address='52:54:00:06:8d:b0'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
后:
<interface type='bridge'>
<mac address='52:54:00:06:8d:b0'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
要创建一个新的虚拟机,取决于你的方法,例如:
virt-install -n serv-jj -r 8192 \
--disk path=/home/doug/vm/serv-jj.img,bus=virtio,size=50 \
-c jammy-live-server-amd64-2022-04-21.iso \
--network bridge=br0,model=virtio,mac=52:54:00:27:1c:6e \
--graphics vnc,listen=0.0.0.0 --noautoconsole -v --vcpus=4 --cpu SandyBridge
运行 3 个虚拟机的示例:
doug@s19:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether 3c:7c:3f:0d:99:83 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 3c:7c:3f:0d:99:83 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.136/24 brd 192.168.111.255 scope global dynamic br0
valid_lft 81610sec preferred_lft 81610sec
4: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:22:2f:dc brd ff:ff:ff:ff:ff:ff
5: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:60:ea:3e brd ff:ff:ff:ff:ff:ff
6: vnet2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000
link/ether fe:54:00:60:ea:5e brd ff:ff:ff:ff:ff:ff
doug@s19:~$ networkctl
IDX LINK TYPE OPERATIONAL SETUP
1 lo loopback carrier unmanaged
2 enp3s0 ether enslaved configured
3 br0 bridge routable configured
4 vnet0 ether carrier unmanaged
5 vnet1 ether carrier unmanaged
6 vnet2 ether carrier unmanaged
doug@s19:~$ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.3c7c3f0d9983 no enp3s0
vnet0
vnet1
vnet2
本文未涉及:如何在 VM 上设置静态地址 192.168.2.16,因为 VM 被声明为 FreeBSD,而我不知道如何操作。
参考:
https://netplan.io/examples/#configuring-network-bridges
KVM 网络桥分配静态 IP
答案2
这可能会有所帮助,如果它没有被阻止,它还没有经过彻底的测试,还需要进行一些调整,欢迎更新它,只需让我知道你所做的任何更改和修复。
可能不需要生成整个文件,只是当时比较容易(阅读代码你就会明白你的意思)而且用户名 sam 和 ip 地址也必须更改。