Using NAT with Hyper-V

By | October 27, 2016

Many articles seem to instruct users to create a NAT using the VMSwitch type of NAT.  Unfortunately, Microsoft has since removed this type of switch.  All commands that follow require the use of PowerShell.  Here is how to use NAT with VMs running under Hyper-V currently:

  1. Create a new Hyper-V Switch
    New-VMSwitch -SwitchName "NAT" -SwitchType Internal
  2. Obtain the ifIndex from the switch that was just created.
    Get-NetAdapter
    get-netadapter
    
    In this case, the ifIndex is 21.
  3. Associate an IP address with the new switch.  In this case, I will be using the private IP range of 192.168.128.0/24.
    New-NetIPAddress -IPAddress 192.168.128.1 -PrefixLength 24 -ifIndex 21
  4. Create a NAT rule to forward traffic.
    New-NetNat -Name NATNetwork -InternalIPInterfaceAddressPrefix 192.168.128.0/24

You can now associate the NAT switch with any VMs you wish to be NAT’d.  There are a few caveats however:

You can only have one NAT network currently.
VM IP addresses need to be configured manually.  No DHCP server exists by default.
The connection seems to work with only hardware network connections (wifi, ethernet, etc) and not VPN tunnels.

Now enjoy NAT in Hyper-V!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.