In this post, I am going to show you how to create a NAT (network address translation) Virtual Switch for Hyper-V on Windows 10 or Windows Server 2016. A virtual NAT switch enables virtual machines to be isolated behind a single shared IP address on the host server. This is very Handy for Windows Server or Hyper-V Containers.
What is NAT?
Network Address Translation (NAT) is a networking technology that has been designed to conserve IP addresses by mapping an external IP address and port to a much larger set of internal IP addresses. Usually, this will be done with a firewall, but in this guide, we are going to be using a Hyper-V Virtual Switch.
For NAT to work it has to use a flow table with rules. These rules basically point the traffic from an external (host) IP Address and port number to an internal IP address on your network (Virtual Machine, Container, Server…) This is very handy when you need to host multiple applications that use the same internal port by mapping them to unique external ports.
Create A NAT Virtual Switch
[themify_box style=”black warning”]
Currently, you can only create one NAT network per host!
[/themify_box]
It is really easy to create a NAT Virtual Switch for Hyper-V. In fact, it is only done via PowerShell and only uses 3 lines of code.
Create Internal Virtual Switch
Open PowerShell with Elevated permissions and type:
New-VMSwitch -SwitchName "NATvSwitch" -SwitchType Internal |
Changing the –SwitchName to something you want to use.
Update Virtual NIC
Open PowerShell with Elevated permissions and type:
New-NetIPAddress -IPAddress 10.10.10.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATvSwitch)" |
Changing the –IPAddress to something you want to use. This IP Address will become the default gateway for your NAT’ed devices.
You can also change the –InterfaceAlias to something you like also.
Configure NAT network Address
Open PowerShell with Elevated permissions and type:
New-NetNAT -Name "NATvNetwork" -InternalIPInterfaceAddressPrefix 10.10.10.0/24 |
Changing the –Name to something you want to use. You also need to change -InternalIPInterfaceAddressPrefix to the IP range you are using. But note the IP Address you set in the previous step has to be in this range.
Some things to note are that any virtual machine that runs on the new NAT virtual switch has to use an IP address in the address range you specified. So in this case 10.10.10.0/24. They will either need to be set manually or via a DHCP Service you install, as by default the NAT Virtual Switch does not have DHCP functionality.
To be able to access the new virtual machines remotely we are going to have to create some NAT rules. I will go over this in another article. I will post a link to it here when it’s ready.
Link: https://pixelrobots.co.uk/2017/07/how-to-create-nat-rules-for-hyper-v-virtual-nat-switch/
And that’s it you have now Created a new NAT Virtual Switch and Network.
[AdSense-A]
2 Comments
How to create NAT rules for Hyper-V Virtual NAT Switch – Pixel Robots. · July 14, 2017 at 2:06 pm
[…] Before you read this post I highly recommend you read this article first: https://pixelrobots.co.uk/2017/06/create-a-hyper-v-nat-virtual-switch/ […]
How To Setup Nested Virtualization In Microsoft Azure - Pixel Robots. · February 18, 2018 at 10:37 pm
[…] can get past this hurdle by creating a NAT Switch in Hyper-V. You can read more about NAT Switches here or follow the rest of this guide to get yourself up and […]