WSL2 Hyper-V Firewall: Allow a Port from One LAN Subnet
Mirrored networking makes a service inside WSL2 look like it is running on the Windows host, but the packets still pass through a separate firewall layer that many guides skip. The Hyper-V firewall sits in front of WSL,
Mirrored networking makes a service inside WSL2 look like it is running on the Windows host, but the packets still pass through a separate firewall layer that many guides skip. The Hyper-V firewall sits in front of WSL, and its documented default inbound action is Block. Changing that default to Allow is the usual shortcut, and it exposes every WSL listener to every network that can reach the host.
This tutorial takes the narrower route: one service port, one allowed source subnet, one auditable Hyper-V firewall rule, plus a positive test, a negative test, and a rollback command you can put in your project runbook.
Table of Contents
- What the Hyper-V firewall does (and what it does not do)
- Before you start: mirrored mode, version, and the service port
- Step 1: Confirm the Linux service is listening on the right interface
- Step 2: Find the WSL creator ID and inspect the current defaults
- Step 3: Create a rule for one port and one LAN subnet
- Step 4: Test from the LAN and from outside the allowed subnet
- Step 5: Audit, disable, and remove the rule
- Troubleshooting: when the rule does not admit traffic
- Conclusion
What the Hyper-V firewall does (and what it does not do)
The Hyper-V firewall is a network firewall for containers hosted by Windows, including WSL. It filters inbound and outbound traffic to and from the WSL VM, and it is managed with the *-NetFirewallHyperV* PowerShell cmdlets rather than the usual New-NetFirewallRule cmdlet. Microsoft's Hyper-V firewall documentation notes that it is available starting in Windows 11, version 22H2, and that rules are enabled per VMCreatorId. For WSL, that identifier is {40E0AC32-46A5-438A-A0B2-2B479E8F2E90}.
The important setting is DefaultInboundAction. It is the fallback action for inbound traffic that does not match a rule. The New-NetFirewallHyperVVMSetting cmdlet reference lists its default as Block. That means a specific allow rule is the intended way to admit a specific service, rather than changing the fallback to Allow.
There is also a host-policy interaction to understand. AllowHostPolicyMerge defaults to True in the same cmdlet documentation, and when it is enabled, eligible Windows Defender Firewall rules and profile settings are merged into the Hyper-V policy. The merge only covers IP 5-tuple host rules, not rules with local conditions such as an application. That explains why some guides use New-NetFirewallRule and can still see results, but the explicit Hyper-V rule is the cleaner, more direct scope for WSL traffic.
Two practical consequences follow:
- The rule is keyed to the WSL VM creator, not to a named distribution. Any WSL distribution listening on the same port can match it, so keep the port specific.
- Loopback traffic between the Windows host and WSL is allowed separately. WSL enables
LoopbackEnabledby default so the host can talk to WSL without a Hyper-V rule. A successfullocalhosttest therefore does not prove that your LAN rule works.
This layer also does not replace the Linux service's own authentication, and it does not create a port proxy. It only decides whether packets matching the rule's conditions are admitted.
Before you start: mirrored mode, version, and the service port
You need three things in place before the firewall rule is useful:
- Windows 11 22H2 or later and WSL 2.0.9 or later. Microsoft's WSL networking documentation describes mirrored mode as available on Windows 11 22H2 and higher, and states that the Hyper-V firewall is turned on by default with WSL 2.0.9 and higher.
- Mirrored networking mode, so the Linux VM shares the host's LAN identity and LAN clients can connect directly. In NAT mode, this rule will not make the service reachable at the host's LAN IP; NAT requires a port proxy instead.
- A known port and protocol for the Linux service, and confirmation that the service is listening beyond loopback.
Check your WSL version in Windows PowerShell:
wsl --version
Then edit %UserProfile%\.wslconfig. Current WSL documentation places these keys under [wsl2]:
[wsl2]
networkingMode=mirrored
firewall=true
The firewall key defaults to true, and Microsoft's advanced WSL settings documentation describes it as the switch that lets Windows Firewall rules and Hyper-V-specific rules filter WSL traffic. If your file already has a [wsl2] section, add the keys there rather than creating a second section.
Restart WSL so the settings take effect:
wsl --shutdown
Wait a few seconds, then start Ubuntu again. wsl --shutdown stops the distribution, so restart your development server afterward.
Inside Ubuntu, compare the Linux addresses with the Windows LAN address:
ip -4 addr show
On Windows, list the IPv4 addresses and prefix lengths:
Get-NetIPAddress -AddressFamily IPv4 |
Where-Object { $_.IPAddress -notlike '127.*' } |
Select-Object InterfaceAlias, IPAddress, PrefixLength
If the LAN IPv4 address shown by Windows also appears inside Ubuntu, mirrored mode is active. If Ubuntu shows a 172.x.x.x address that does not match Windows, you are probably still in NAT mode.
If the machine is managed, also check the per-profile policy:
Get-NetFirewallHyperVProfile -PolicyStore ActiveStore
If AllowLocalFirewallRules is False, a locally created Hyper-V rule will not be applied. The exception has to come from the organization's CSP or Intune policy; local PowerShell changes are not enough.
Step 1: Confirm the Linux service is listening on the right interface
A firewall rule cannot fix a service that only listens on 127.0.0.1. Microsoft's WSL networking guidance explains that connections arriving through a remote IP address are treated as LAN connections, so the application must bind to 0.0.0.0 or to the mirrored LAN address rather than loopback.
Check a TCP listener inside Ubuntu:
ss -ltnp | grep ':8080'
A service reachable from the LAN should show a wildcard or LAN address, for example:
LISTEN 0 4096 0.0.0.0:8080 0.0.0.0:*
If it shows 127.0.0.1:8080, only local processes can connect. Change the application's bind address. For a quick test with Python's built-in server:
python3 -m http.server 8080 --bind 0.0.0.0
For UDP, use the UDP socket listing instead:
ss -lunp | grep ':19132'
Record the exact protocol and port. TCP and UDP are separate rules, and a TCP test tells you nothing about a UDP service.
Step 2: Find the WSL creator ID and inspect the current defaults
Open an elevated Windows PowerShell window. Start WSL once if it is not already running, then query the creators:
$vmCreatorId = (Get-NetFirewallHyperVVMCreator |
Where-Object FriendlyName -eq 'WSL' |
Select-Object -First 1).VMCreatorId
$vmCreatorId
You should see the WSL GUID:
{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}
Now inspect the active per-VM settings:
Get-NetFirewallHyperVVMSetting -PolicyStore ActiveStore -Name $vmCreatorId |
Format-List Enabled, DefaultInboundAction, LoopbackEnabled, AllowHostPolicyMerge
A typical WSL result looks similar to this, although values can differ by build and policy:
Enabled : True
DefaultInboundAction : Block
LoopbackEnabled : True
AllowHostPolicyMerge : True
Do not change DefaultInboundAction here. If it already reads Allow, record that in your runbook because your fallback is broader than the documented default. The scoped rule below can still be added, but you should review the global setting separately rather than widening it further.
Step 3: Create a rule for one port and one LAN subnet
First, identify the LAN prefix. Use the adapter that owns your route to the local network, not a VPN or loopback adapter. For example:
| Windows host IP | Prefix length | LAN subnet to allow |
|---|---|---|
192.168.1.42 |
/24 |
192.168.1.0/24 |
Use the network address in -RemoteAddresses, not the host's own address. If you need two LAN subnets, pass both as an array rather than widening to Any.
The native cmdlet is New-NetFirewallHyperVRule. The cmdlet reference documents the port, protocol, remote-address, and action parameters used here. Preview the rule first with -WhatIf:
$ruleName = 'WSL-DevServer-8080'
$lanSubnet = '192.168.1.0/24'
New-NetFirewallHyperVRule `
-Name $ruleName `
-DisplayName 'WSL dev server TCP 8080 from LAN' `
-Direction Inbound `
-VMCreatorId $vmCreatorId `
-Protocol TCP `
-LocalPorts 8080 `
-RemoteAddresses $lanSubnet `
-Action Allow `
-WhatIf
If the preview looks right, run the same command without -WhatIf:
New-NetFirewallHyperVRule `
-Name $ruleName `
-DisplayName 'WSL dev server TCP 8080 from LAN' `
-Direction Inbound `
-VMCreatorId $vmCreatorId `
-Protocol TCP `
-LocalPorts 8080 `
-RemoteAddresses $lanSubnet `
-Action Allow
Here is what each condition is doing:
| Parameter | Effect in this recipe |
|---|---|
-Name |
Stable unique identifier for scripts and cleanup |
-DisplayName |
Human-readable label; it is localized, so prefer -Name in automation |
-Direction Inbound |
Matches traffic coming from the LAN toward the WSL service |
-VMCreatorId |
Limits the rule to the WSL VM creator |
-Protocol TCP |
Matches only the protocol your service uses |
-LocalPorts 8080 |
Matches the Linux service's listening port |
-RemoteAddresses 192.168.1.0/24 |
Limits the allowed source to one LAN subnet |
-Action Allow |
Admits traffic that matches every condition |
Two optional tightenings are worth knowing:
- Add
-LocalAddresses '192.168.1.42'to match only traffic destined to that host address. This is tighter, but it breaks if DHCP changes the address; use a reservation or omit it. - Add
-Profiles Privateonly if you have confirmed that the LAN adapter's Windows network profile is actually Private. The default isAny, and a profile-scoped rule is inactive when another profile is active.
Mirrored networking has a few moving parts beyond the firewall rule. If you are still assembling the .wslconfig side, this walkthrough of WSL2 firewall and mirrored-networking settings shows how networkingMode=mirrored, firewall=true, and the related keys fit together.
Do not run this as a shortcut:
# Avoid this unless you intentionally want a global fallback change.
Set-NetFirewallHyperVVMSetting -Name $vmCreatorId -DefaultInboundAction Allow
That changes the fallback for every unmatched inbound connection to every WSL listener. The scoped rule above is the exception; the default should stay at its least-privilege value.
Step 4: Test from the LAN and from outside the allowed subnet
Test from an allowed LAN host
Use a second device on the allowed subnet. From a Windows LAN client:
Test-NetConnection -ComputerName 192.168.1.42 -Port 8080
Look for:
TcpTestSucceeded : True
For an HTTP service, a request gives a stronger signal that the application is responding, not just that the port is open:
curl -v http://192.168.1.42:8080/health
Do not use the Windows host itself as the positive test. A connection through localhost can succeed through the loopback path without proving the LAN rule. The second device is what exercises the LAN path.
Test from outside the allowed subnet
From a host on a different routed subnet or guest VLAN:
Test-NetConnection -ComputerName 192.168.1.42 -Port 8080
Expect TcpTestSucceeded : False, typically as a timeout or refusal. One caveat: if that subnet cannot route to the Windows host at all, the failure is inconclusive. The test only proves the firewall's scoping if the disallowed host can otherwise reach the host's network.
If you only have one flat LAN
You can still prove the source scoping by narrowing and then widening the rule. This uses Set-NetFirewallHyperVRule to change the remote address condition:
- Choose a test client on the LAN, for example
192.168.1.50. - Narrow the rule to that single address:
Set-NetFirewallHyperVRule -Name 'WSL-DevServer-8080' `
-RemoteAddresses '192.168.1.50/32'
- From
192.168.1.50, confirm the service is reachable. - From a second LAN client, for example
192.168.1.51, confirm the service is not reachable. If it is reachable, another broader rule or anAllowdefault is probably involved. - Widen the rule back to the LAN subnet:
Set-NetFirewallHyperVRule -Name 'WSL-DevServer-8080' `
-RemoteAddresses '192.168.1.0/24'
- From
192.168.1.51, confirm the service is now reachable. That is expected: the final rule intentionally allows the whole/24.
For UDP services, use the application's own client or a UDP-aware probe. Test-NetConnection -Port tests TCP only.
Step 5: Audit, disable, and remove the rule
Audit the rule from the active policy store:
Get-NetFirewallHyperVRule -Name 'WSL-DevServer-8080' -PolicyStore ActiveStore |
Format-List Name, DisplayName, Enabled, Direction, Action, Protocol,
LocalPorts, RemoteAddresses, VMCreatorId, EnforcementStatus
The Get-NetFirewallHyperVRule reference explains that the cmdlet can query by name and that the output includes PortStatuses, which shows the WSL ports the rule is currently applied to. Check for:
Enabled : TrueDirection : InboundAction : AllowProtocol : TCP-
LocalPortscontaining8080 -
RemoteAddressescontaining192.168.1.0/24 -
VMCreatorIdmatching the WSL GUID EnforcementStatus : OK
If EnforcementStatus is not OK, or PortStatuses is empty, the rule may exist but not be attached to an active WSL port. That usually points back to the Linux listener or a policy conflict.
To pause the exception without deleting it:
Set-NetFirewallHyperVRule -Name 'WSL-DevServer-8080' -Enabled 'False'
To remove it permanently:
Remove-NetFirewallHyperVRule -Name 'WSL-DevServer-8080' -Confirm:$false
The Remove-NetFirewallHyperVRule reference includes an important warning: running the cmdlet with no parameters removes all Hyper-V firewall rules on the target computer. Always target the rule by -Name or -DisplayName.
If you changed DefaultInboundAction to Allow during earlier troubleshooting, restore the value you recorded. The documented default is:
Set-NetFirewallHyperVVMSetting -Name $vmCreatorId -DefaultInboundAction Block
Finally, record the exception in the project runbook:
Service: Docs preview on TCP 8080
Host LAN IP: 192.168.1.42
Allowed source: 192.168.1.0/24
WSL networking: mirrored, firewall=true
Hyper-V rule: WSL-DevServer-8080
Create: New-NetFirewallHyperVRule -Name WSL-DevServer-8080 ...
Verify: Get-NetFirewallHyperVRule -Name WSL-DevServer-8080 -PolicyStore ActiveStore
Pause: Set-NetFirewallHyperVRule -Name WSL-DevServer-8080 -Enabled 'False'
Remove: Remove-NetFirewallHyperVRule -Name WSL-DevServer-8080
Troubleshooting: when the rule does not admit traffic
-
The cmdlet is not recognized, or
Get-NetFirewallHyperVVMCreatorreturns nothing. Confirm you are in elevated Windows PowerShell on Windows 11 22H2 or later, with WSL 2.0.9 or later, and that WSL has started at least once. If the cmdlet is missing entirely, the Hyper-V firewall is not available on that build. -
The Windows host can connect, but the LAN client cannot. Check the Linux listener with
ss. A service bound to127.0.0.1will never accept LAN traffic, regardless of the firewall rule. Also compareip -4 addr showinside Ubuntu with the Windows LAN address to confirm mirrored mode is active. -
The rule exists but
EnforcementStatusis notOK. InspectPortStatuses. An empty result usually means no matching WSL port is active, so the Linux service may not be listening on that port.PartiallyEnforcedoften indicates a policy conflict. - A block rule wins. List the WSL rules and sort by priority:
Get-NetFirewallHyperVRule -VMCreatorId $vmCreatorId |
Sort-Object RulePriority |
Format-Table Name, Action, RulePriority, Protocol, LocalPorts, RemoteAddresses
By default, block rules receive priority 1 and allow rules priority 2, and lower numbers are evaluated first. A matching block rule with a lower number can prevent the allow rule from taking effect.
-
The rule stopped working after a network change. The host's DHCP address may have changed, or the rule may be scoped to a profile that is no longer active. Check
Get-NetConnectionProfileandGet-NetFirewallHyperVProfile -PolicyStore ActiveStore. A-Profiles Privaterule will not apply on a Public or Domain profile, and a local rule will not apply ifAllowLocalFirewallRulesis False. -
You are looking for the rule with
Get-NetFirewallRule. These are Hyper-V firewall rules. Audit them withGet-NetFirewallHyperVRule;Get-NetFirewallRulequeries the Windows Defender Firewall rule set, which is a different layer.
Conclusion
Mirrored networking lets LAN clients reach a WSL2 service at the Windows host's LAN address, but the Hyper-V firewall still decides whether the packets are admitted. The least-privilege pattern is straightforward: leave DefaultInboundAction at Block, create one inbound rule for the exact protocol and port, scope -RemoteAddresses to the LAN subnet, and verify it from a second device rather than from the Windows host.
Use Get-NetFirewallHyperVRule to audit the rule, Set-NetFirewallHyperVRule -Enabled 'False' to pause it, and Remove-NetFirewallHyperVRule -Name ... to clean it up. Record the rule name, port, subnet, and cleanup command in the project runbook so the exception does not outlive the service.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.