Objective is to Make WSL2-hosted server listening on Win10 external IP (by default, WSL server listens on “localhost”)

WSL2 inside Windows10 (Build 18980 and more)

Intro

WSL2

Windows Subsystem for Linux is aimed to be a Linux (POSIX like runtime environment inside Windows10) In WSL1 it is a redirection of OS calls to underlying OS In WSL2 it is a Linux kernel (real distribution) , acting a VM inside hosting OS (W10) , localhost on Win10 redirects on the internal VNET address

for the need of out-of-the-box hostnames for IP addresses generated inside /etc/hosts with legacy hosts file as an additional option of /etc/wsl.conf and inside C:\Windows\System32\Drivers\etc\hosts

Proposal of naming conventions , in order to register theses names insise mDNS / multiCast /UDP DNS :

wsl.host wsl.host.local could be the IP to the windows host ( found in /etc/resolv.conf) wsl.debian wsl.ubuntu …. wsl.<distriName> could be the IP for each distribution.(found with ip addr ) At the glance:

-------------------------------------------
|    Win10 : The **WSL Host**             |
|     for several distributions           |
|                                         |
|       wsl.distributionName              |
|        |                                |
|     ---v-----------------               |
|    | WSL distribution1 | ---> wsl.host  |
|     ---------------------               |
|                                         |
-------------------------------------------

Dynamic IP for WSL2 - Windows10 network bridge

https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes#accessing-windows-applications-from-linux

to connect to the Windows hosted (inside VS Code) XDebug server , PHP server (php-fpm10) must contact the hosting windows. The IP is the nameserver inside /etc/resolv.conf

gui@SAGIS-09:~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.20.0.1

Dev workstation side (VS Code on windows10)

Because WSL2 is boxed, WSL-hosted servers must listen on 0.0.0.0 instead of default localhost. https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes#other-networking-considerations

Some scripts ( found here) and some reads about WSL IP address & Subnet is never deterministic (Constantly changing): https://github.com/microsoft/WSL/issues/4467

for registering inside the hosts file

#!/bin/sh
export WINIP=$(cat /etc/resolv.conf | grep 'nameserver' | cut -f 2 -d ' ') 
export WSLIP=$(ip addr show eth0 | grep 'inet ' | cut -f 6 -d ' ' | cut -f 1 -d '/')
echo $WINIP
echo $WSLIP
echo "<Password>" | sudo -S sed -i "/winip/c $WINIP\twinip" /etc/hosts
echo "%s/^.*wslip/$WSLIP\t\twslip/g|wq" | vim -E /mnt/c/Windows/System32/drivers/etc/hosts

mDNS on Windows10 (or Apple Bonjour?)

to dig, because seems not very mature on WIN10 https://www.ctrl.blog/entry/windows-mdns-dnssd.html

would be fine to register entries for vNET ressources ….

Listening on the external public IP

PORT RELAY / PROXY of Windows 10 on WSL2 (internal VNET)

A server listening inside WLS2 could be accessed by localhost on Win10. https://localhost would bridge to the dynamic VNET IP address inside WSL2.

For being able to redirect external IP to this internal dynamic VNET IP address, Win10 provides Port Proxy :

Powershell:

netsh interface portproxy add v4tov4 listenaddress=<ip or dns> listenport=80 connectaddress=localhost connectport=80
netsh interface portproxy show all

Écouter sur ipv4 :             Connecter à ipv4 :

Adresse         Port        Adresse         Port
--------------- ----------  --------------- ----------
<ip or dns>             80          localhost       80

Remove the port proxy

netsh interface portproxy delete v4tov4 listenaddress=<ip> listenport=9000

more info: Docs from Microsoft