I basically avoided this issue by using WSL1 for the past years, but I found a solution that works for me; If you don't encounter this issue often just use $(hostname).local ๐
TLDR; This changes localhost
to point to your Windows IP, 127.0.0.1
is still available for wsl. In windows you can access WSL via localhost
as usual, because windows listens for exposed ports on WSL. Exposes your set windows port(s) via portproxies
.
- Add this to your
/etc/wsl.conf
,
[boot]
command="sed -i \"s/127.0.0.1$(printf '\t')localhost/$(tail -1 /etc/resolv.conf | cut -d' ' -f2)$(printf '\t')localhost/g\" /etc/hosts 2>&1"
This command replaces the default localhost
in /etc/hosts
to point to your Windows IP, 127.0.0.1
is still available for wsl. This might require setting the host in your code to 127.0.0.1
for example in vite
add server: { host: "127.0.0.1" }
. Services running on Windows might need to be bind to 0.0.0.0 or it's local ip to be accessible in WSL.
From windows you can access WSL via localhost
as usual, because windows listens for exposed ports on WSL.
-
a. And use this powershell script to configure portproxies fix-wsl2-port-forwading-to-windows-localhost. The firewall rules, might not be necessary but can be helpful if you encounter issues.
b. While the above script also helps you set the firewall rules. You can also execute the following command to set the portproxies manually in (admin) Powershell. In this example ports 8080,27017. (credits above)
$addr='0.0.0.0'; $ports=@(8080,27017); # set ports according to your use case, what ports from windows should be exposed.
$remoteport = (wsl hostname -I).Trim(); for( $i = 0; $i -lt $ports.length; $i++ ){ $port = $ports[$i]; echo "added portproxy ${addr}:${port} to ${remoteport}:${port}"; iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; }
You can reset these proxies anytime using netsh interface portproxy reset
Because the WSL2 ip address changes every restart you need to add two scheduled tasks;
3. netsh
with arguments interface portproxy reset
to trigger 'On an event', Log 'Security' and Event ID '4647' (log off) + Event ID '1074' (shutdown/restart).
4. The above powershell
command, you can save it to a .ps1
file and execute with powershell
with arguments c:\path_to_script.ps1
to trigger 'At log on'
5. You can test the automated scheduled tasks manually, by simulating a restart using wsl --shutdown
you'll see the ip has changed wsl.exe hostname -I
.
This for me solves many of the main issues why I didn't switch, it's not the same as using WSL1. But does the job since you don't have to change localhost
host configurations for any of your projects. Which is frustrating when using WSL2. Bonus you can enjoy the latest features such as WSLg
and better performance ๐.
For mongodb i've added this function to the script. To not have Mongodb give the folowing error:
Error setting up listener, {"code":9001,"codeName":"SocketException","errmsg":"An attempt was made to access a socket in a way forbidden by its access permissions."}
Source
My full script