问题描述

在 Windows 11 专业版上使用 Hyper-V 虚拟了一台 ubuntu-22.04.3-live-server-amd64 虚拟机,并配置桥接网络。网卡配置内容如下:

leazhi@ubuntu2204-001:~$ cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
eth0:
dhcp4: false
dhcp6: false
bridges:
br0:
interfaces: [eth0]
dhcp4: false
dhcp6: false
addresses: [192.168.137.22/24]
gateway4: 192.168.137.1
nameservers:
addresses:
- 223.5.5.5
- 192.168.137.1
parameters:
stp: true
forward-delay: 4
version: 2

结果按照上述配置后,无法 ping 通网关,自然也不能 ping 通外网。同时,从宿主机 ping 虚拟机也不通:

虚拟机 ping 情况:

宿主机 ping 情况:

问题分析

尝试不配置桥接,直接为虚拟机设置静态IP,如下:

leazhi@ubuntu2204-001:~$ cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
eth0:
dhcp4: false
dhcp6: false
addresses: [192.168.137.23/24]
gateway4: 192.168.137.1
nameservers:
addresses:
- 223.5.5.5
- 192.168.137.1

结果发现,虚拟机 ping 网关正常,也可以 ping 通外网。同时,宿主机也可以 ping 通虚拟机!

解决方法:

最终,通过 google 在网上找到了一篇类似的文章:Ubuntu 22.04 bridging with netplan doesn’t work 。通过查看文章中提到的思路,在配置桥接的时候,加上 macaddress 参数,并将该参数的值指向虚拟机中网卡的 mac 地址,如下:

leazhi@ubuntu2204-001:~$ cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
eth0:
dhcp4: false
dhcp6: false
bridges:
br0:
interfaces: [eth0]
dhcp4: false
dhcp6: false
addresses: [192.168.137.22/24]
gateway4: 192.168.137.1
macaddress: 00:15:5d:03:73:05 # 这里的 MAC 地址是 eth0 的 MAC 地址
nameservers:
addresses:
- 223.5.5.5
- 192.168.137.1
parameters:
stp: true
forward-delay: 4
version: 2

然后重新执行 sudo netplan apply 命令,发现虚拟机可以 ping 通宿主机了!