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.

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

Replace the parameters as below.

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.

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 the guest machine(s) should have access to External networks and Internet.

Hope this helped you. Leave me a reply below.

35 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…..

      1. Or like this: $natswitch = Get-NetAdapter | where {(($_.name -match “NAT-Switch”) -and ($_.InterfaceDescription -eq “Hyper-V Virtual Ethernet Adapter”))}

  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.

  10. You have a Hyper-V host named Server1. Server1 connects to your corporate network.
    Server1 has the virtual switches configured (Private1,Internal1,External1).
    Server1 has two virtual machines named VM1 and VM2 that run Windows Server 2016. VM1 connects to Private1. VM2 has two network adapters.
    You need to ensure that VM1 connects to the corporate network by using NAT:
    1) Solution: You connect VM2 to Private1 and External1. You run the New-NetIpAddress and the New-
    NetNat cmdlets on VM2. You configure VM1 to use VM2 as the default gateway.
    YES or NO?

    2) Solution: You connect VM1 to Inernal1. You run the New-NetIpAddress and the New-NetNat cmdlets on
    Server1. You configure VM1 to use Server1 as the default gateway.
    YES or NO?

    3) Solution: You connect VM2 to Private1 and External1. You install the Remote Access server on VM2, and
    you configure NAT in the Routing and Remote Access console. You configure VM1 to use VM2 as the default
    gateway.
    YES or NO?

  11. Hi Anand,
    Below are IP details of my Laptop in Office network:
    IP Address: 139.73.43.21
    Subnet Mask : 255.255.252.0
    Default Gateway : 139.73.40.3

    I have installed Hyper-v, ran the commands provided above(step 2 to step6) however I am not able to connect to Internet inside my Hyper-v VM. The commands that I ran on my local host are below:
    New-VMSwitch -SwitchName “NAT-Switch” -SwitchType Internal -Verbose
    Get-NetAdapter
    New-NetIPAddress –IPAddress 192.168.200.1 -PrefixLength 24 -InterfaceIndex 62 –Verbose
    New-NetNAT –Name NATNetwork –InternalIPInterfaceAddressPrefix 192.168.200.0/24 –Verbose
    Get-VM | Get-VMNetworkAdapter | Connect-VMNetworkAdapter –SwitchName “NAT-Switch”
    Finally I have assigned below IP to my Hyper-v VM:
    IP Address: 192.168.200.11
    Subnet: 255.255.255.0
    Gateway: 192.168.200.1

    Could you please assist?

      1. Hi Anand,
        Thanks for your quick response. I will check it and update the status in few minutes.
        Regards,
        Venkat

  12. Hi Anand,
    I have used 192.168.5.0/255.255.255.0 network and it worked!! Thanks a lot for your inputs!! Greatly appreciate it. However I am able to connect to Internet in Hyper-V VM only when I manually set DNS IP as 8.8.8.8. I will get a DNS IP when I connect to Broadband Internet at home as well as when I work out of my Office. So, how to make my Hyper-V VM to use DNS IPs that are assigned to my Laptop. Is this possible? Without a Internet connection, my VPN will not connect.

  13. Hi Anand,
    Is the only way to make my Hyper-V VM to connect to Internet is to hard code DNS IPs 8.8.8.8 and 8.8.4.4? I am comparing how the VM in VMWare workstation works. In VMWare, I never had the requirement to manually specify the IP, Subnet Mask, Gateway and DNS IPs. Everything is taken care automatically in the background by VMWare Workstation. As long as my underlying physical machine has internet connectivity, my VMWare VM automatically gets internet. Checking if I can have same option in Hyper-V VM.

  14. How id the HyperV-NatNetwork tied to the Physical Ethernet Adapter? I can not ping my PCs gateway from my Linux VMs, or get to the internet. my physical network 192.168.254.x and the virtual network of my VM is 10.0.0.x. Don’t I need a way t bridge these 2 networks?

    1. Mike, Use the PS Command: Get-NetIPConfiguration -InterfaceIndex nn – Where nn is the InterfaceIndex number you used in Step 4 above.

  15. NAT network setup worked for me, my VM gets access to network with the new NAT network. But VM loses access to internet once I connect to my company VPN. my machine IP is in 192.X.X.X range whereas Company ips are 10.X.X.X. I was using the IPs in this example, not sure why I am unable to access internet. I have manually added company DNS servers in the VM.

    1. Because VPN software usually use their own virtual NIC to route the traffic to your company network. That causes VM network traffic is not going through the VPN tunnel.

Leave a comment