How to Create a NAT Switch on Hyper-V

Updated

How to Create a NAT Switch on Hyper-V

In this guide, you will learn how to create a Hyper-V switch that uses NAT for network access.

NAT will provide virtual machines access to network resources (internet access) using the host computers network adapter.

For this guide, I’ll use the below network settings. When the configuration is complete the VMs on the 192.168.100.0/24 network will use the Hyper-V Host IP 192.168.1.4 for accessing the internet.

Diagram of the VM network reaching the internet through the Hyper-V host adapter

Step 1: Create an Internal Virtual Switch

Open PowerShell and run the below command. Change “SWITCHNAME” to whatever you want to name your switch, I’ve called mine “nat-switch”.

New-VMSwitch -Name SWITCHNAME -SwitchType Internal
New-VMSwitch creating an internal virtual switch in PowerShell

Step 2: Get the Interface Index Number (ifIndex)

Next, you need to find the index number of the virtual switch created in step 1. Run the below command.

Get-NetAdapter
Get-NetAdapter output showing the interface index of the new switch

My index number is 28, you will probably have a different number than mine.

Step 3: Configure NAT Gateway

I have my VMs configured on the 192.168.100.0/24 network. I’m going to use the 1st address of this network for the gateway. The gateway will be used n your VMs so they can send traffic outside of their network.

New-NetIPAddress -IPAddress 192.168.100.1 -PrefixLength 24 -InterfaceIndex 28
New-NetIPAddress assigning the gateway address to the switch interface

Step 4: Configure NAT Network

Now configure the network you want to NAT. Again I want to NAT the 192.168.100.0/24 network. You can name anything you like, I named mine “Nat-Switch-Outside”.

New-NetNat -Name Nat-Switch-Outside -InternalIPInterfaceAddressPrefix 192.168.100.0/24
New-NetNat creating the NAT network for the internal subnet

Step 5: Configure VM with New Virtual Switch

In Hyper-v you can now change your VMs to use the new virtual switch.

Hyper-V VM settings with the network adapter pointed at the new virtual switch

Step 6: VM Network Settings

Now configure your VM with an IP address from the network configured in step 4. In my case, this is the 192.168.100.0/24 network.

I’ll give my VM the IP 192.168.100.10 and the gateway 192.168.100.1 (step 3).

VM network settings with a static IP and the NAT gateway address entered

Now your hyper-v VMs will use NAT when accessing a different network. The diagram below is an example of how NAT works. All of the VMs on my 192.168.100.0/24 network will use the Hyper-V host physical Adapter when accessing the internet.

Diagram of several VMs sharing the host's physical adapter through NAT

View NAT Settings

To view existing NAT settings run the below command.

Get-NetNat
Get-NetNat output listing the configured NAT networks

View Hyper-V Virtual Switches

Run this command to view your Hyper-V virtual switch.

Get-VMSwitch