Hyper-V添加内部NAT网络

By | November 25, 2023

使用powershell (管理员权限)执行
1、创建虚拟交换机,等同于在Hyper-V管理器界面中新建虚拟网络交换机

<#
说明:
New-VMSwitch    是创建虚拟交换机的指令
-SwitchName     是指定创建交换机的名字
"NAT-VM"        是交换机的名字 (名字可以随意起,但是需要能看懂和记住)
-SwitchType     指定虚拟交换机的类型(内部,专用)
Internal        交换机的类型,分别有(Internal,Private)
#>
<#
-SwitchType说明:
Specifies the type of the switch to be created. Allowed values are Internal and Private. To create an External virtual switch, specify either the NetAdapterInterfaceDescription or the NetAdapterName parameter, which implicitly set the type of the virtual switch to External.
#>
PS C:\Windows\system32>New-VMSwitch -SwitchName "NAT-VM" -SwitchType Internal

2、查看 NAT-VM 的 ifindex

<#
说明:
获取网卡信息, 然后关注ifindex 列的值(取-SwitchName的名字"NAT-VM"行的值 )
#>
PS C:\Windows\system32>Get-NetAdapter

3、创建ip,InterfaceIndex参数自行调整为上一步获取到的ifindex。这一步等同于在 控制面版-网卡属性 中设置ip

<#
说明:
New-NetIPAddress        设置IP地址命令
-IPAddress      指定IP
-PrefixLength   指定子网掩码长度;比如:255.255.255.0=24
-InterfaceIndex     配置第二步获取到的"NAT-VM"行Ifindex值
#>
PS C:\Windows\system32>New-NetIPAddress -IPAddress 192.168.1.1 -PrefixLength 24 -InterfaceIndex 27

4、创建NAT网络,允许访问外部网络,24为子网掩码位数,即:255.255.255.0

<#
New-NetNat      创建nat网络命令
-Name       指定刚才创建取"NAT-VM"(按自己创建的来)的名字
-InternalIPInterfaceAddressPrefix   配置允许访问外部的网络
192.168.1.0/24      指定IP段,并配置子网长度 24=255.255.255.0
#>
PS C:\Windows\system32>New-NetNat -Name NAT-VM -InternalIPInterfaceAddressPrefix 192.168.1.1/24

5、在Hyper V管理器中设置该虚拟机的网络适配器为 NAT-VM(按自己创建的选择)

6、Hyper-V除Default-Switch,其他都没有dhcp服务,所以需要自己手动进虚拟机进行填写相应网段的IP地址(在系统中设置好ip、网关、dns即可),进行使用!!!

外网远程连接虚拟机,使用端口转发,参考

netsh interface portproxy add v4tov4 listenport=8899 connectaddress=192.168.1.110 connectport=22

Windows 2016 配置端口转发

Leave a Reply