Using a NAT Virtual Switch with Hyper-V
In this post, I will show how you can create a Windows 10 or Windows Server 2016 (WS2016) Hyper-V virtual switch that uses network address translation (NAT), enabling virtual machines to be isolated behind a single shared IP address on the host.
Scaling and Isolation
What is NAT? NAT is a system that is used in just about every Internet-connected home and business, which we non-network types rarely think about. The system allows a private network to connect to a larger network using a single IP address; this means that we can have many machines in the private network, consuming just a single address on the larger network and this increases the scalability of the larger network.
An added benefit of scaling out the possible number of machines on the network is that NAT effectively isolates the machines in the smaller private network. We can only access machines in the NAT’d network by creating NAT rules, translating an external TCP or UDP port on the external interface into a private TCP or UDP port listening on a NIC or IP address of a machine on the private network.
It is these two features that make NAT interesting in WS2016 Hyper-V networking. We can create a NAT virtual switch and use it in a few interesting scenarios, including:
- Deploying Windows Server or Hyper-V containers on a virtual switch with a private address range, enabling many containers to be hosted on a single IP address that is assigned to the host.
- Creating a classroom full Windows 10 PCs, running Client Hyper-V, with identical virtual machines running on each PC.
I recently had a need to deploy that second example at work when we built a new training room for teaching Microsoft server and cloud solutions. I needed to provide each attendee with a set of machines, including domain controllers, file servers, SQL Server instances, and so on. Instead of creating a unique domain for each host, with each machine having a unique LAN address, I created 1 set of machines, copied them to each host, and imported them into Hyper-V. Networking is provided by a NAT-enabled virtual switch, meaning that the virtual machines can connect to the LAN and Internet via the host’s LAN IP address, but the machines on one PC cannot connect to or interfere with those on another PC.
Deploying a NAT Virtual Switch
You only need three lines of PowerShell to deploy an NAT-enabled virtual switch on a Windows 10 or WS2016 Hyper-V machine.
The first step is to create an Internal virtual switch; this is a switch that is not connected to a physical NIC on the host; instead, the host management OS has a virtual NIC that is connected to the virtual switch; the end result is that (to begin with) that virtual machines on the internal virtual switch can talk to the host, but they cannot talk to the network that the host is connected to.
New-VMSwitch -SwitchName “NATSwitch” -SwitchType Internal
The next step updates the virtual NIC that connects the host management OS to the internal virtual switch. The following command will assign an IP address to this virtual NIC, and this IPv4 address will be the default gateway for the network on NAT network that we are creating.
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceAlias “vEthernet (NATSwitch)”
The final step in the process will configure the network address of the NAT network that will run on the virtual switch; this is the private range of addresses that the virtual machines will use in the abstracted virtual switch; note that the IPv4 address in the previous step must be in this range.
New-NetNAT -Name “NATNetwork” -InternalIPInterfaceAddressPrefix 192.168.0.0/24
The resulting deployment is depicted in the following illustration.
By default, there is no way to remotely access the machines from the LAN, but you can create NAT rules to enable access via port translation (forward TCP 50002 from the host IP address to TCP 3389 on 192.168.0.2).
There is no DHCP functionality in the virtual switch. If you want DHCP, then you must build one or more DHCP servers as machines on the switch. Otherwise, you can assign static IP addresses to the machines.
More in Hyper-V
How to Install Hyper-V on Windows Server Core
Mar 16, 2022 | Michael Reinders
Microsoft Says There Will Be No Hyper-V Server 2022 Free Edition
Dec 13, 2021 | Michael Reinders
How to Stop an Unresponsive Hyper-V Virtual Machine
Jun 29, 2021 | Russell Smith
How to Enable Nested Virtualization in Hyper-V
Jun 28, 2021 | Russell Smith
Disaster Recovery with Azure Backup for SQL Server VMs
May 28, 2019 | Michael Otey
Leveraging Virtualization for Improved Availability and Disaster Recovery
Apr 29, 2019 | Michael Otey
Most popular on petri