windowsnetwork-programmingwindows-subsystem-for-linux

WSL Server not accessible from Windows or external device


I have search the whole internet, ChatGPT, Perplexity ... and I still didn't manage to make my server hosted in WSL2 accessible with the Windows Host IP.

The problem :

WSL 2 : Ubuntu 24.04
Windows host IP : 192.168.X.X
WSL IP : 192.168.X.X (with network mirror)
Server PORT : 8080

http://localhost:8080 OK
http://192.168.X.X ERROR (but accessible within WSL)

# without network mirror, so WSL IP 172....., server also not accessible with 192.168.X.X, but ok with 172 IP on Windows

What I have already done :

netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=8080 connectaddress=192.168.X.X

The weird thing is this work just once ! I manage to reach my server through my phone (same network), then I decided to restart the server and bam, nothing again.

Also on my old laptop, I have my server in WSL 2 Ubuntu 20 working, but when I duplicate the WSL to move it to another drive, the server became not reachable again, even though I try to netsh and so on...

I really struggle, if someone can help ! Or maybe I should use a proxy or something like that ? But does that support development server communication ?


Solution

  • Ok I find the answer ! Nobody say to do that on all videos or forum or ChatGPT ... that's crazy but just reading the documentation here https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking

    I found this command that configure the Hyper-v firewall, in networkMode=mirrored

    Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
    

    Full note :

    Run the following command in PowerShell window with admin privileges to Configure Hyper-V firewall settings to allow inbound connections: Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow or New-NetFirewallHyperVRule -Name "MyWebServer" -DisplayName "My Web Server" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 80.

    So now, any interface on the windows can access to the server with localhost:PORT or 127.0.0.1:PORT, and any external devices wihtin the LAN/local network can access to the wsl server with Windows IP !