Get the ip address of the wsl2 host. Note: worth considering if you have multiple, to do a
wsl --shutdown && wsl
to clean it up, and also set the preferred host/distro
directly using wsl (see: wsl --help
about setting defaults).
Anyway, to get that ip (note the -I and not the -i):
cmd.exe in Windows:
wsl hostname -I
Open terminal and get into the wsl2 host. Make sure openssh-server is installed, get into /etc/ssh/sshd_config via sudo and uncomment or create the following
/etc/ssh/sshd_config in Linux WSL2 host
Port 2200 # or whatever you prefer (keep this port handy you'll need it in step 3)
ListenAddress 0.0.0.0 # you can probably subnet this if you wanted to (and prob should)
Then replace the wsl2-host-ip with the result of the above and create an SSH tunnel. This should use the SSH Client available in windows 11. (You need to enable this in add/remove windows features or chatgpt the thing for help there).
Let's assume the port you have your app listening on within wsl2 is 0.0.0.0:5000, and then (where wsl2-user is the user account on the wsl2 host and 2200 is the port you setup openssh-server from step 1 and...
cmd.exe in Windows:
ssh -L 0.0.0.0:5000:localhost:5000 wsl2-user@wsl2-host-ip-from-step-1 -p 2200 -N
et voila. get your windows ip (ipconfig) or just use mDNS (windows-hostname.local) on :5000 and should pass through. E.g., http://windows-hostname.local:5000 (from any device from which the windows host is reachable)