Access WSL from Computers on Same LAN

Access WSL from Computers on Same LAN

script za sve to: https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723

find out WSL2 IP:

wsl hostname -I

port forwarding:

netsh interface portproxy show all

netsh interface portproxy delete v4tov4 8000 0.0.0.0

netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=WSL2_IP netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=172.26.37.85

disable firewall

Set-NetFirewallProfile -Profile Domain, Public, Private -Enabled False

start hugo

hugo server –bind 0.0.0.0 -D -p 8000 –navigateToChanged

hugo server -D -p 8000 –navigateToChanged

hugo server -p 8000 –navigateToChanged -b “http://<MY_LAN_IP>:8000/”

Ustvari, najbolje koristiti script

https://gist.github.com/xmeng1/aae4b223e9ccc089911ee764928f5486

*** WOOW

In Windows, forward your port from the public IP port to the WSL port using netsh interface portproxy add v4tov4 listenport=$PORT listenaddress=0.0.0.0 connectport=$PORT connectaddress=127.0.0.1 in a Powershell with admin rights. This is one of the hard-to-find but necessary WSL specific bits. It look like Windows creates a virtual adapter that isn’t properly bridged with your internet network adapter. I tried playing various bridging tricks, but in the end, I had to manually create a portproxy rule using Windows’ network shell netsh. This listens on all addresses and forwards the connection to the localhost, which seems to be automatically bridged with WSL. You can also try to manually forward it to the WSL adapter. Use ipconfig to find it. However, the WSL IP changes from time to time, so I recommend using local host instead. It might also be wise to listen explicitly on your internet facing IP instead of 0.0.0.0, but this seemed to work. https://www.williamjbowman.com/blog/2020/04/25/running-a-public-server-from-wsl-2/

netsh interface portproxy add v4tov4 listenport=$PORT listenaddress=0.0.0.0 connectport=$PORT connectaddress=127.0.0.1

netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=127.0.0.1

Ili koristi https://github.com/wangyu-/tinyPortMapper Has Windows binary: https://github.com/wangyu-/tinyPortMapper/releases

Ima i ovo rešenje: https://github.com/microsoft/WSL/issues/4150#issuecomment-788553101 Go program koji radi ono isto odozgo: https://github.com/yingshaoxo/fuckwsl2

Ali mislim da je UBEDLJIVO najbolje rešenje: https://github.com/CzBiX/WSLHostPatcher

sudo mkdir -p /etc/profile.d

cat << EOF | sudo tee /etc/profile.d/wsl-host-patch.sh

# https://github.com/CzBiX/WSLHostPatcher
#
# Adjust accordingly based on `root` setting inside `/etc/wsl.conf`
# https://docs.microsoft.com/en-us/windows/wsl/wsl-config#configure-per-distro-launch-settings-with-wslconf
#
/c/bin/wsl-host-patch/WSLHostPatcher.exe

EOF

Disable firewall completely or better yet, allow just specific ports

disable firewall

Set-NetFirewallProfile -All -Enabled False

allow just list of ports

New-NetFirewallRule -DisplayName ‘_Allow_Inbound_Ports’ -Profile @(‘Private’) -Direction Inbound -Action Allow -Protocol TCP -LocalPort @(‘8000’, ‘8001’)


WSL2 problems & solutions

Connect to WSL2 and firewall problems

  • To access from host machine, you can now use “localhost” to connect from the host itself in recent WSL2 versions; There’s no obvious way to get there from a different machine on the network (link)

    You can also use small Windows tool as service to update hosts file with the WSL2 IP address: shayne/go-wsl2-host

  • To access WSL2 from any machine on network, you need to execute this script

  • How to check activity on some port: nc -lk 0.0.0.0 3000

date 20. Feb 2021 | modified 10. Jun 2024
filename: Windows » WSL » LAN Access