iTranslated by AI
How to Access WSL2 Web Apps from a Smartphone on the Same LAN
I want to immediately check a web app under development in WSL2 on a smartphone
It is well known that when your smartphone and PC are connected to the same Wi-Fi, you can verify your web app by accessing http://<PC_IP_Address>:<Web_App_Port> from your smartphone's browser.
I usually develop business systems and libraries for PCs, so I rarely have the chance to use this method, but I recently needed to check something on a smartphone for the first time in a while.
Since specific steps were required when the development environment is WSL2, I'm publishing this as a memo.
Conclusion for those who only want to know WSL2-specific processing
Please open PowerShell with administrator privileges.
You can enter the following line by line, or it should work if you write it in a .ps1 file and execute it.
I'm not very familiar with PowerShell, so I type them in one by one.
Start
$ports = @(3000, 8000) # Specify ports used by the web app in an array
$fwRuleName = "WSL 2 Firewall Unlock"
$wsl2Address = wsl -e hostname -I | ForEach-Object { $_.trim() }
New-NetFireWallRule -DisplayName $fwRuleName -Direction Inbound -LocalPort $ports -Action Allow -Protocol TCP
for ($i = 0; $i -lt $ports.length; $i++) {
$port = $ports[$i]
netsh interface portproxy add v4tov4 listenport=$port listenaddress=* connectport=$port connectaddress=$wsl2Address
}
Verify
Get-NetFireWallRule -DisplayName $fwRuleName | Get-NetFireWallPortFilter
netsh interface portproxy show v4tov4
End
Remove-NetFireWallRule -DisplayName $fwRuleName
for ($i = 0; $i -lt $ports.length; $i++) {
$port = $ports[$i]
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=*
}
If you only use one port, you can define $port = 3000 instead of $ports = @(3000) and dismantle the for loop.
Start
$port = 3000
$fwRuleName = "WSL 2 Firewall Unlock"
$wsl2Address = wsl -e hostname -I | ForEach-Object { $_.trim() }
New-NetFireWallRule -DisplayName $fwRuleName -Direction Inbound -LocalPort $port -Action Allow -Protocol TCP
netsh interface portproxy add v4tov4 listenport=$port listenaddress=* connectport=$port connectaddress=$wsl2Address
End
Remove-NetFireWallRule -DisplayName $fwRuleName
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=*
Following the official MS documentation below resulted in smartphone access being blocked by the firewall, so New-NetFireWallRule was necessary.
When assigning the standard output to $wsl2Address, I used ForEach-Object to trim trailing whitespace; please let me know if there's a shorter way to write this.
(I avoided the % alias because it's hard to understand if you're not familiar with PowerShell.)
Reference sites
Additional information for those who couldn't connect with the above alone
Checking the PC's IP address
Please search for "windows ipconfig" on Google.
Checking the WSL2 IP address
It is displayed by the command below, which is also included in the start commands above:
wsl -e hostname -I
Wi-Fi router settings
If a feature called "Privacy Separator" is enabled, your smartphone will not be able to find your PC.
Web framework settings
Modern web frameworks sometimes block access from addresses other than "localhost" or "127.0.0.1" as a countermeasure against DNS rebinding.
Please check your web framework's documentation and set it to host: '0.0.0.0'.
Debugging steps
If you're not sure where it's failing, it's a good idea to check using the following steps:
- Install the web framework directly on the Windows side.
- Set
host: '0.0.0.0'. - Start the server.
- Access
http://localhost:<port>in your PC's browser and confirm the display. - Check the PC's IP address with
ipconfig. - Access
http://<PC_IP_address>:<port>from your smartphone. - If it's not displayed, the Wi-Fi router's privacy separator setting might be the cause, so check it.
- If it's displayed, stop the server and remove the web framework.
- Install the web framework directly on the WSL2 side.
- Set
host: '0.0.0.0'. - Start the server.
- Enter the start commands provided at the top of this article.
- Access
http://<PC_IP_address>:<port>in your PC's browser. - If it's not displayed, the
netsh interface portproxy add ~command is incorrect; check the settings withnetsh interface portproxy show v4tov4. - If it is displayed on the PC, access
http://<PC_IP_address>:<port>from your smartphone. - If it's still not displayed on the smartphone, the
New-NetFireWallRule -DisplayName ~command is incorrect; check the details withGet-NetFireWallRule -DisplayName $fwRuleName | Get-NetFireWallPortFilter. - Success!
Have a great WSL2 life!
Discussion