DNS Proxy for Hyper-V with NAT Virtual Switch in Windows 10 v1511.
January 24, 2016 | | Tags : Hyper-V Virtual Switch NAT Networking DNS
During the November update, Windows 10 version 1511 Hyper-V received new Virtual Switch type “NAT”. This is a very important addition, since existing Virtual Switch types did not fully address a scenario of running Virtual Machines with Internet access on a laptop.
External switch works great when a Host computer is a part of a static network infrastructure, and you need to make your Virtual Machines transparently become a part of it. But when a laptop is used as a Host, then it becomes a constant nightmare, since every time the laptop connects to a different network, each VM on it would have to negotiate network settings with that network’s DHCP server, and if there is no DHCP, then theit IP addresses, gateways, and DNS settings would have to be set manually. Then they may need to accept terms and conditions of the network, etc. Plus you can choose only one physical adapter to use for the Virtual Switch, so if you use Ethernet in one place, and Wireless in another, then you would have to modify the External Virtual Switch setting every time you change the physical adapter that connects to the Internet. Not a whole lot of fun.
There has been a few workarounds:
-
Internal Virtual Switch with Internet Connection Sharing (ICS). Simply share your physical network adapter with an Internal Vistual Switch, and you’re all set. Of course you will still need to share a different adapter if you connect to the internet using a different one.
-
External Virtual Switch connected to Network Adapter from VMWare Player.
The new type of Virtual Switch is a great step to replace these workarounds. However it does not provide neither DNS nor DHCP services. I usually set static IP addresses for my Virtual Machines, so I don’t need DHCP as long as the IP subnet for my virtual machines have the same address space and gateway. But I do need DNS. Of course I can use Google public DNS servers, but I prefer the ones that are automatically set by the ISP, since they usually have better routing.
In order to provide DNS resolution for my Virtual Machines, I wrote a simple Windows service that determines which Virtual Switch of NAT type, and automatically proxies all DNS requests made to the NAT gateway address to the DNS servers provided by ISP. Here are the steps to set up the NAT Virtual Switch with DNS:
- Create NAT Virtual Switch and Configure NAT object. Execute the following code in Powershell with Admin rights:
New-VMSwitch -Name "{pick a name of your new virtual switch}" -SwitchType NAT -NATSubnetAddress x.x.x.x/x New-NetNat –Name "{pick a name of the NAT object}" –InternalIPInterfaceAddressPrefix x.x.x.x/x
Please note that the IP subnets in both commands should be the same. For example 192.168.138.0/24. If you open the network adapter IPv4 properties you will see that it got assigned the first address from that subnet, for example 192.168.138.1.
- Set up DNS Proxy. You can download and build the service from this Git repository (in order to build the project you will need to also have WiX toolset 3.10.2 installed) or you can download and install a precompiled msi file. The windows service will identify all NAT Virtual Switches and add DNS capabilities to them.
Once you have everything, you can use the NAT Virtual Switch IP address as a DNS server in the virtual machine network configuration. If you add/remove/change NAT Virtual Switch, you will just need to restart the service. Please also note that this service won’t work on a computer with Internet Connection Sharing enabled.