Windows 10 上使用 Hyper-V 虚拟机进行端口转发

Windows 10 上使用 Hyper-V 虚拟机进行端口转发

我在 Windows 10 上的 Hyper-V 虚拟机中安装了 Ubuntu 18.04,我需要让它的端口 51235 可以从互联网上访问。

还有各种相关的帖子,例如:

  1. Hyper-V 和 Ubuntu VM 的静态 IP 地址
  2. Hyper-V VM 静态 IP - 2 个 IP 地址
  3. 使动态IP地址固定吗?

但还是不够清楚。我应该配置Ethernet adapter vEthernet (Default Switch)虚拟机所连接的网络适配器吗?还是应该创建一个新的特定网络适配器?

如何启用 NAT?我的静态 IP 地址有什么用?

配置 Ubuntu 以使其具有静态(固定)IP 地址的最简单方法是什么?

下面我提供了使用命令获取的网络适配器列表ipconfig /all

Windows IP Configuration

   Host Name . . . . . . . . . . . . : DESKTOP-L5J499D
   Primary Dns Suffix  . . . . . . . :
   Node Type . . . . . . . . . . . . : Hybrid
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
   Physical Address. . . . . . . . . : 00-15-5D-00-68-05
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::34b5:c9a0:8af:f4d2%5(Preferred)
   IPv4 Address. . . . . . . . . . . : 169.254.80.80(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 637539677
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter vEthernet (Internal Ethernet Port Microsoft Emulator NAT Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #3
   Physical Address. . . . . . . . . : 00-15-5D-00-68-06
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::d4d9:73c7:599c:f657%15(Preferred)
   IPv4 Address. . . . . . . . . . . : 172.16.80.1(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 738202973
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Intel(R) Ethernet Connection (2) I219-V
   Physical Address. . . . . . . . . : B0-6E-BF-C0-EE-1C
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::e015:e896:a9c:5ef3%8(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.0.104(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Wednesday, June 5, 2019 4:38:09 PM
   Lease Expires . . . . . . . . . . : Wednesday, June 5, 2019 10:38:08 PM
   Default Gateway . . . . . . . . . : 192.168.0.1
   DHCP Server . . . . . . . . . . . : 192.168.0.1
   DHCPv6 IAID . . . . . . . . . . . : 61894335
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : 192.168.0.1
   NetBIOS over Tcpip. . . . . . . . : Enabled

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
   Physical Address. . . . . . . . . : 00-15-5D-01-1A-46
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::502b:1b91:2c6:5a6a%27(Preferred)
   IPv4 Address. . . . . . . . . . . : 192.168.244.65(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . :
   DHCPv6 IAID . . . . . . . . . . . : 452990301
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-33-5B-4C-B0-6E-BF-C0-EE-1C
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

编辑1:

使用 Ubuntu 上的动态 IP 地址,我有以下信息:

$ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group defaul                          t 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
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group defa                          ult qlen 1000
    link/ether 00:15:5d:00:68:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.103/24 brd 192.168.0.255 scope global dynamic noprefixroute e                          th0
       valid_lft 5759sec preferred_lft 5759sec
    inet6 fe80::6476:961e:3158:86f5/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

在 Windows 端:

Windows IP Configuration

Ethernet adapter vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::34b5:c9a0:8af:f4d2%5
   IPv4 Address. . . . . . . . . . . : 169.254.80.80
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (Internal Ethernet Port Microsoft Emulator NAT Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::d4d9:73c7:599c:f657%15
   IPv4 Address. . . . . . . . . . . : 172.16.80.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (External Virtual Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::b453:fe72:12d3:5982%37
   IPv4 Address. . . . . . . . . . . : 192.168.0.104
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::502b:1b91:2c6:5a6a%27
   IPv4 Address. . . . . . . . . . . : 192.168.244.65
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . :

我尝试了以下 Ubuntu 设置,但它们中断了互联网连接:

Ubuntu 有线网络设置

编辑2:

我的外部虚拟交换机配置如下:

Hyper-V 虚拟交换机管理器

Intel(R) Ethernet Connection (2) I219-V我的网卡是否在设备管理器中列出。我的外部虚拟交换机被选为虚拟机的网络适配器:

Hyper-V 虚拟机网络设置

答案1

Hyper-V 网络适配器:如果尚未设置,您需要在 Hyper-V 中设置外部虚拟交换机,并将其分配给您的 VM。这将允许您的 VM 访问主机的网络。您不想在 Hyper-V 中使用默认 NAT 适配器,因为它不可配置并且不允许端口转发。

要设置外部虚拟交换机:
在 Hyper-V 管理器中,单击虚拟交换机管理器, 选择外部的, 点击创建虚拟交换机。 在下面外部网络,选择连接到网络的网卡,允许管理操作系统共享此网络适配器应保持选中状态。点击好的. 将这个新的虚拟交换机分配给 Ubuntu VM。 注意:安装虚拟交换机时,这将暂时断开主机与网络的连接。

NAT:您应该已经在防火墙/路由器上启用了 NAT。直接连接到互联网并不是一个好主意。您不需要在 Windows 10 主机上启用 NAT 来实现此功能,这只会增加复杂性。

静态IP:您可以根据您的网络选择此项。您应该选择一个超出 DHCP 范围但在同一子网 (192.168.0.x) 内的值。

转发端口:使用路由器/防火墙上的端口转发来处理端口转发。使用外部虚拟交换机并为您的虚拟机分配常规网络的 IP。为路由器/防火墙中的端口/IP 地址配置端口转发。

配置 Ubuntu 为静态 IP:您在虚拟机中执行此操作,而不是在 Hyper-V 组件中。在 Ubuntu 中,转到设置-->网络。单击有线网络连接旁边的齿轮图标。单击 IPv4 选项卡,单击手动,然后输入特定于您的网络的 IP 设置。子网掩码、网关和 DNS 值应与主机的相同。

请参阅此链接查看上述图片:https://linuxconfig.org/how-to-configure-static-ip-address-on-ubuntu-18-04-bionic-beaver-linux

答案2

在仔细阅读了 Alexis Tupeck 的回答中链接的文章后,我能够设置 Hyper-V 内部交换机,为其提供新的私有 IP/子网,并在该子网上创建新的 NAT。然后,我可以在子网上设置具有静态 IP 的虚拟机,并在新的 NAT 上将端口转发给它们。

以下示例将设置 Hyper-V 主机的内部网络172.16.10.254 /24,以及客户虚拟机172.16.10.100

1. 创建新的 Hyper-V 内部交换机

这可以通过 Hyper-V 管理器或 PowerShell 完成:

New-VMSwitch  -SwitchName "NAT Switch"  -SwitchType Internal

这将创建一个名为“vEthernet(NAT 交换机)”的新网络适配器,我们在下面使用它。

2. 在新适配器上为 Hyper-V 主机设置新的 IP 地址/子网

我们将使用172.16.10.254这里有一个 /24 子网(255.255.255.0)。

New-NetIPAddress  -IPAddress 172.16.10.254  -PrefixLength 24  -InterfaceAlias "vEthernet (NAT Switch)"

这个新的 Hyper-V 主机 IP 将成为虚拟机的网关。

3. 在新子网上设置 NAT 网络:

New-NetNat  -Name NATNetwork  -InternalIPInterfaceAddressPrefix 172.16.10.0/24

这将设置 Windows 对往返于该子网的数据包执行网络地址转换。

4. 在虚拟交换机上设置虚拟机。

为其指定一个静态 IP172.16.10.100- 使用 /24 或 255.255.255.0 作为子网掩码 - 并将其网关设置为 Hyper-V 主机172.16.10.254

(假设 Hyper-V 主机没有在您的新子网上提供 DNS,您可能只需将其 DNS 服务器指向主机自身正在使用的服务器,或者指向外部服务器,例如 1.1.1.1 或 8.8.8.8。)

现在您应该能够使用 NAT 通过 Hyper-V 主机从 VM 访问互联网。

5. 在 NAT 网络上设置到虚拟机的端口转发(例如TCP 1443->172.16.10.100:443):

Add-NetNatStaticMapping  -NatName NATNetwork  -Protocol TCP  -ExternalIPAddress 0.0.0.0/24  -ExternalPort 1443  -InternalIPAddress 172.16.10.100  -InternalPort 443

此后,您应该能够通过localhost:1443Hyper-V 主机访问 VM 的端口 443。

答案3

需要注意的是,您可以执行静态 NAT 映射,而无需将您的 VM 放在物理网络上,这使您可以使用您的主机作为物理网络上的唯一设备,同时将您的其他 VM 保留在主机的防火墙/分段后面并与主机共享相同的 MAC,这在大学、学校、宿舍等中通常是需要的。

https://petri.com/create-nat-rules-hyper-v-nat-virtual-switch

相关内容