Skip to content

Instantly share code, notes, and snippets.

@thomaswilley
Created September 19, 2023 02:56
Show Gist options
  • Save thomaswilley/cd23db295f118b1596c0547a5cb098e1 to your computer and use it in GitHub Desktop.
Save thomaswilley/cd23db295f118b1596c0547a5cb098e1 to your computer and use it in GitHub Desktop.
Enable WSL2 Host access from any device on the local network [via SSH tunnel] (e.g., for x-device testing, etc)

WSL2 host access for local development & testing from other devices in the LAN

1. Get some input info

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

2. Setup openssh-server on the WSL2 host

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)

3. Create the local (-L, not -R) SSH tunnel from windows (client) to WSL2 host (server)

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

That's it.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment