These days, I generally use an Azure VM for dev work but sometimes it's helpful to run a local VM on my laptop. Previously, making use of the wifi connection from the VM had always seemed a bit hit-and-miss and overly complicated.
Thankfully, new functionality in Windows 10 now makes configuration simpler and the connection completely reliable.
You can read the background and details of what I describe here: MSDN blog
Cleanup Existing Networks
My laptop had various virtual switches configured - as was required using the old method of internet sharing. My first job was to purge these, which I did as follows:
From Hyper-V Manager:
• Shutdown running VM • Selected "Virtual Switch Manager" • Removed all virtual switches • Selected my VM • Selected "Settings" • Selected each "Virtual switch" (drop-down box) and Removed
Create the new NAT Switch
I ran the following PowerShell commands on my host (Win10 laptop):
• New-VMSwitch -SwitchName "NatSwitch" -SwitchType Internal • Get-NetAdapter • New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex 63 • Get-NetNat • New-NetNat -Name RobsNATNetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
I then had the following connections on the host:
My host LAN was configured on the 192.168.1.* range but when I created the New-NetIPAddress with PowerShell, I assigned in the range 192.168.0.1. This means, when I configure my VM's LAN I must use the .0.1 range. This is shown in the following screen grab:
As, can be seen in the screen grab - the VM is using the address of the physical router from my Host's LAN for DNS.
With the connection from my VM to the "NatSwitch" of the host configured - I was able to browse the web web from my VM. When I unplugged the ethernet cable from my laptop, the VM automatically switched to using the WiFi - result!
What if Sharing a Connection from a Mobile Phone
Today I tried running my VM when my host's only internet connection was WiFi from a personal hotspot of my iPhone. The only change I had to make was to find the DNS server being used by the host, by running "ipconfig /all", then entering the same DNS ip address into the network settings on the guest VM. I suspect there may be a smart way of achieving this, perhaps it's possible to enter a "Preferred DNS server" address for the guest as the IP address of the host - and the host passes through the DNS details? If anyone knows how - please comment!