Configuring Hyper-V VMs Network Adapter for WSL (Hyper-V Firewall)
In this article, we will cover the key concepts and steps required to configure a Hyper-V virtual machine's (VM) network adapter for use with the Windows Subsystem for Linux (WSL) and the Hyper-V firewall. This guide will help you troubleshoot and resolve any internet connection issues that may arise when launching a VM after changing the network adapter settings.
Understanding the Default Switch
The default switch in Hyper-V is a virtual switch that is created automatically when you install the Hyper-V role. This switch allows virtual machines to connect to the physical network using the host computer's network adapter. By default, when you create a new virtual machine in Hyper-V, it is connected to the default switch.
Configuring the Network Adapter for WSL
In order to use WSL with a Hyper-V virtual machine, you will need to configure the virtual machine's network adapter to use the "Windows Subsystem for Linux" virtual switch. This virtual switch is created automatically when you enable WSL on your host computer. To change the virtual machine's network adapter settings, follow these steps:
- Open the Hyper-V Manager.
- Select the virtual machine that you want to configure.
- Click on the "Settings" option in the right-hand menu.
- In the settings window, click on the "Network Adapter" option.
- Use the drop-down menu to select the "Windows Subsystem for Linux" virtual switch.
- Click "Apply" and then "OK" to save the changes.
Configuring the Hyper-V Firewall
When you change the network adapter settings for a Hyper-V virtual machine, it may trigger the Hyper-V firewall to block incoming and outgoing traffic. To ensure that your virtual machine has internet connectivity, you will need to add a rule to the Hyper-V firewall to allow this traffic.
Here are the steps to create a firewall rule to allow incoming and outgoing traffic for a Hyper-V virtual machine:
- Open the Windows Defender Firewall with Advanced Security.
- Click on the "Inbound Rules" option and then click on "New Rule" in the right-hand menu.
- In the rule creation window, select the "Custom" option and then click "Next".
- In the "Program" tab, select the "All programs" option and then click "Next".
- In the "Protocol and Ports" tab, select the "Any" option for both the protocol and port and then click "Next".
- In the "Scope" tab, select the "Any IP address" option for both the local and remote IP addresses and then click "Next".
- In the "Action" tab, select the "Allow the connection" option and then click "Next".
- In the "Profile" tab, select the appropriate profile for your environment (domain, private, or public) and then click "Next".
- In the "Name" tab, enter a name for the rule (e.g. "Hyper-V Virtual Machine Traffic") and then click "Finish" to create the rule.
Repeat the above steps for Outbound rules.
#allows inbound traffic for WSL
New-NetFirewallRule -DisplayName 'Hyper-V Virtual Machine Traffic Inbound' -Direction Inbound -Protocol ANY -LocalPort ANY -Action Allow
#allows outbound traffic for WSL
New-NetFirewallRule -DisplayName 'Hyper-V Virtual Machine Traffic Outbound' -Direction Outbound -Protocol ANY -LocalPort ANY -Action Allow
- Understand the default switch in Hyper-V and its role in providing internet connectivity to VMs.
- Learn how to configure the network adapter of a Hyper-V VM for use with WSL.
- Understand the potential impact of the Hyper-V firewall on VM connectivity and learn how to create firewall rules to allow traffic for VMs.
References
- Microsoft Docs: Connect to the network in Hyper-V on Windows 10
- Microsoft Docs: Create an inbound port rule
- Microsoft Docs: New-NetFirewallRule