Windows 10: How to setup NAT network for Hyper-V guests?


Windows 10 Hyper-V has NAT (Network Address Translation) network feature, but it needs to setup using PowerShell now.  I will show you step by step instructions how to do it. NAT Switch provides Internet access to the VM without creating External Switch (linking the switch to physical wired or wireless adaptor).

Before we start, here are the requirements to prepare it ahead of time.

1.  IP Network to be used in Guests. You need to find a subnet that is not being using in your Office network. If you are setting up on your Home, you can choose any subnet that is not used in your home. E.g., 192.168.200.0/24

2. Guest VMs are needed to assign IP address/subnet/gateway manually from the subnet you choose on requirement 1 above. Or you may need to setup in DHCP server in one of the Guest VMs for automatic IP assignment. If you only have one or two guest VMs, you may set the IP address manually on NICs.

Now the step by step instruction.

Step 1: Open PowerShell with Administrative privileges.

Search PowerShell in Start menu search, right click on PowerShell and choose More ==> Run As Administrator.

Step 2: Create Hyper-V internal only switch.

Run this command on the PowerShell.

New-VMSwitch –SwitchName “NAT-Switch” –SwitchType Internal –Verbose

Step 3: Find the Interface Index number

Type this cmdlet and note down the interface index (ifIndex) for NAT-Switch adaptor.

Get-NetAdapter

Step 4: Create NAT Gateway

Run this cmdlet. Replace

  • IP Address (gateway) with your own (should be from the network subnet you chose in the requirements section above)
  • PrefixLength is the subnet mask number for the subnet you chose
  • Interface index you noted down on previous step.

New-NetIPAddress –IPAddress 192.168.200.1 -PrefixLength 24 -InterfaceIndex 16 –Verbose

Step 4.1: Create NAT Network

Run this cmdlet and replace InternelIPInterfaceAddressPrefix with your chosen network.

New-NetNat –Name NATNetwork –InternalIPInterfaceAddressPrefix 192.168.200.0/24 –Verbose

Step 5: Connect your VM to the NAT-Switch

You may manually assign the Guest NIC to the “NAT-Switch” which we created in Step-2.  Or you may run this cmdlet to assign NIC from all Guest VMs to the “NAT-Switch”

Get-VM | Get-VMNetworkAdapter | Connect-VMNetworkAdapter –SwitchName “NAT-Switch”

Step 6: Assign IP Address to the NICs in Guests

Open NIC properties in your Guest VMs, Assign IPv4 addresses from the network subject you setup in Step-4.  Or run DHCP server from one of your Guests to dish IP address automatically.

E.g.,

Guest VM 1:

IP Address: 192.168.200.11
Subnet: 255.255.255.0
Gateway: 192.168.200.1
DNS: 4.2.2.2 and 8.8.8.8 (or your own DNS server from the office network).

Guest VM 2:

IP Address: 192.168.200.12
Subnet: 255.255.255.0
Gateway: 192.168.200.1
DNS: 4.2.2.2 and 8.8.8.8 (or your own DNS server from the office network).

That’s all. By  now you guests should have access to External networks and Internet.

Hope this helped you. Leave me a reply below.

Advertisements

21 thoughts on “Windows 10: How to setup NAT network for Hyper-V guests?

  1. I followed your guide but my guest VM (centos) could not connect to the internet.
    Environment:
    -Windows 10 HyperV
    -CentOS7 (1611)

  2. Worked a treat thank you so the VMs on my Win10 laptop have internet connectivity when I’m on a hotel’s WiFi.

  3. Wonderfully done. One suggestion to minimize errors for the get adapter:

    $natswitch = Get-NetAdapter -Name “*NAT-Switch”
    New-NetIPAddress -InterfaceIndex $natswitch.InterfaceIndex -IPAddress…..

  4. Since this site helped me, I’ll just share the very quick and simple script I wrote so that I don’t have to do a lot of stuff manually except for the individual VM configuration part.

    #### ####
    $Natswitchname = “HyperV-NATSwitch”
    $NATNetwork = “192.168.200.0”
    $NATrouteraddress = “192.168.200.1”
    $NATPrefixLength = “24”
    #### ####

    #### Script ####
    New-VMSwitch –SwitchName $Natswitchname –SwitchType Internal –Verbose
    $natswitch = Get-NetAdapter | where {(($_.name -like (“vEthernet ($Natswitchname)”)))}
    New-NetIPAddress $NATrouteraddress -PrefixLength $NATPrefixLength -InterfaceIndex $natswitch.interfaceindex -Verbose
    $NATNetworkFull = $NATNetwork + “/” + $NATPrefixLength
    New-NetNat -Name HyperV-NatNetwork -InternalIPInterfaceAddressPrefix $NATNetworkFull -Verbose

  5. Great simple instructions. I have been scratching my head with Hyper-V networking NAT. 1 up for Virtualbox when it comes to networking ease.

  6. How do you get IPs one on one, 1:1 NAT, so that public IPs from the outside are mapped one to one on IPs on the inside?

  7. Good Day
    Thank you for your advice. I have struggled and simply cannot get the VM to access internet via my laptop. The internet is either SUPER slow, or doesn’t work AT ALL.

    I have executed your instructions, and now internet doesn’t work on the laptop or the VM.

    Question please:
    How do I undo these commands in Power Shell?

    1. Would this be correct?

      Step 1: Remove-NetNat –Name NATNetwork

      Step 2: Remove-NetIPAddress –IPAddress 192.168.200.1 -InterfaceIndex 18

      Step 3: Remove-VMSwitch “NAT-Switch”

  8. how does this link the Nat network to your current interface? I have the nat internal switch on 192 network but don’t have internet access on host. I turned of ip 6 to make sure that was not doing any thing. Does it mater iff I have more than one switch in hyper v?

  9. Thanks so much! I have Hyper-V on my work laptop and they have disabled connection sharing so NAT was the only way I could get my VMs to work. This worked great for CentOS 7.

Leave a Reply to Asim Khan Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s