要解决的问题
有一个虚拟机运行在 VMWare workstation 中,如何让这个虚拟机桥接到宿主机的网络上的某个 VLAN?
常见使用场景
只有一个网口的单臂软路由,同时宿主机为 Windows,虚拟机软件为 VMware Workstation,在虚拟机中运行 OpenWrt
需要在一个网口上,利用 VLAN 实现虚拟 WAN 口上的 PPPoE 拨号和 虚拟 LAN 口上的上网服务。
或者是电脑接入在交换机的trunk口,VMware Workstation的各虚拟机要接入对应的vlan(没多网卡和网卡支持VLAN的情况下)
方法
根据宿主机网卡的品牌,下载对应的 VLAN 设置软件。已知 Realtek 和 Intel 网卡都提供这样的软件。Realtek 网卡的软件为 Realtek Ethernet Diagnostic Utility
下文以 Realtek 网卡为例。如果你的网卡厂商不提供 VLAN 设置软件,请直接转到文末
在宿主机上设置你想要使用的 VLAN 编号,软件将会创建一个虚拟网卡。记住网卡名称和适配器名称

打开 VMWare 虚拟网络编辑器,新建一个网络,例如 VMnet12
桥接新建的网络到刚才添加的 VLAN 虚拟网卡上

给虚拟机添加一张连接到 VMnet12 网卡。另外我推荐修改 vmx 配置文件把网卡类型改成
vmxnet3
以获得最佳性能(vmxnet3为万兆半虚拟化网卡,默认的e1000性能很烂)

在虚拟机使用新加的网卡。注意在虚拟机里面就像一个普通的网卡一样用它就行了,千万不要带上vlan tag
如果网卡厂商没提供 VLAN 设置软件 (很多普通网卡都不支持咯)
具体请machines/" target="_blank" rel="noopener" style="text-shadow: rgba(0, 0, 0, 0.08) 0px 0px 1px; border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1) 0s; color: rgb(3, 102, 214); text-decoration-line: none; background-color: transparent; box-sizing: border-box; -webkit-font-smoothing: antialiased !important;">参阅本文
思路是:利用 Hyper-V 命令行工具(不需要启用 Hyper-V 平台),来创建虚拟 VLAN 网卡,实现和上面步骤3一样的效果
# "Ethernet" is the available NIC that you want the VSwitch to be based on //新建一个 虚拟机交换机,桥接到 Ethernet 网卡 New-VMSwitch -Name "新建虚拟交换机名称" -NetAdapterName "Ethernet" # Create virtual adapters for each vlan you want to assign. //新建一个虚拟网卡,属于 某个虚拟交换机 Add-VMNetworkAdapter -ManagementOS -Name 虚拟网卡名称 -SwitchName 交换机名称 # Then configure the interface you just created to listen for the vlan tag needed // 把虚拟网卡加入 某个VLAN Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName 虚拟网卡名称 -Access VlanID 10 # Now you have a virtual interface that is configured for a specific vlan. To have access to that vlan inside a virtual machine, simply change the settings from VMware Workstation Virtual Network Editor and have a vmnet that is bridged with the Hyper-V virtual NIC. //vmware Workstation 把相关vmnet桥接到这个虚拟网卡上
实战命令:
PS C:\WINDOWS\system32>New-VMSwitch -Name "trunk" -NetAdapterName "Ethernet"
//以本地网卡Ethernet创建交换机trunk,默认不打标签 Untagged
PS C:\WINDOWS\system32> Add-VMNetworkAdapter -ManagementOS -Name trunk-x -SwitchName trunk
//在交换机trunk下创建虚拟网卡trunk-x
PS C:\WINDOWS\system32> Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName trunk-x -Trunk -AllowedVlanIdList 1-4094 -NativeVlanId 1
//交换机trunk下的虚拟网卡trunk-x配置成trunk模式,通过所有Vlan ID,默认Pvlan1
PS C:\WINDOWS\system32> Get-VMNetworkAdapterVlan -ManagementOS
VMName VMNetworkAdapterName Mode VlanList
------ -------------------- ---- --------
vlan1 Access 1
Container NIC c9ffa239 Untagged
trunk-x Trunk 1,1-4094
trunk Untagged
//显示hyper-v主机下,所有网卡的vlan情况
PS C:\WINDOWS\system32> Get-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName trunk-x
VMName VMNetworkAdapterName Mode VlanList
------ -------------------- ---- --------
trunk-x Trunk 1,1-4094
//显示hyper-v主机下,trunk-x网卡的vlan情况
注意
本方法的实质是桥接虚拟机网卡到 VLAN 虚拟网卡上,这样做是因为 VMware 根本没法桥接带有 VLAN Tag 的 Ethernet 帧而不得已为之。
这样做的缺点是无法让虚拟机感知到 VLAN 的存在,也就是说你不能像 eth0.2
这样直接在虚拟机里面指定 VLAN 编号,要使用某个 VLAN 必须先在宿主机设置桥接。
本文链接:https://www.kinber.cn/post/3712.html 转载需授权!
推荐本站淘宝优惠价购买喜欢的宝贝: