If you ever need to create a NAT Network using Hyper-V, follow these steps.
Create a new Hyper-V Virtual Switch
New-VMSwitch –SwitchName “NATSwitch” –SwitchType Internal
Configure the NAT Gateway IP Address: This configures the Virtual Network Adapter which was created while creating the Internal Virtual Hyper-V Switch.
New-NetIPAddress –IPAddress 10.10.10.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATSwitch)"
Now you can configure the NAT rule
New-NetNat –Name MyNATnetwork –InternalIPInterfaceAddressPrefix 10.10.10.0/24
After that you have finally created your NAT network and you can now use that network to connect your virtual machines and use IP Address from 172.21.21.2-172.21.21.254.
Create a new NAT forwarding
- To forward specific ports from the Host to the guest VMs you can use the following commands. This example creates a mapping between port 80 of the host to port 80 of a Virtual Machine with an IP address of 10.10.10.2.
Add-NetNatStaticMapping -NatName "VMSwitchNat" -Protocol TCP -ExternalIPAddress 0.0.0.0 -InternalIPAddress 10.10.10.2 -InternalPort 80 -ExternalPort 80
- This example creates a mapping between port 82 of the Virtual Machine host to port 80 of a Virtual Machine with an IP address of 10.10.10.3.
Add-NetNatStaticMapping -NatName "VMSwitchNat" -Protocol TCP -ExternalIPAddress 0.0.0.0 -InternalIPAddress 10.10.10.3 -InternalPort 80 -ExternalPort 82