There have been lots of improvements to the Windows Subsystem for Linux over the past few years, but, as with any tech in development, some issues can crop up. Recently, for example, you were unable to use 'localhost' to access any processes running on your Linux instance.
I do occasionally have problems with being unable to access processes from Windows. I test most of my work through web browsers, but in the case of external devices, I need Windows to route the traffic properly, as well. I did find a helpful trick and script to speed this up.
You can run Windows programs and tools from the WSL2 terminal, which I found handy for 1) opening explorer windows so I can copy/email/upload files and 2) getting some system info from Windows, e.g.
REACT_NATIVE_PACKAGER_HOSTNAME=`cmd.exe /c ipconfig | grep IPv4 | grep -oP -e '\d+\.\d+\.\d+\.\d+' | tail -n 1` expo start
It works the other way too, which helps an intermittent problem I have with the Windows Firewall. This Powershell script, which I got from a Microsoft site, automatically updates firewall rules so I can route traffic to the WSL machine.
$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(80,443);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
My list of ports is a little more extensive, but I run this once I boot up my machine, setting up my environment for another day of work. So far, this is the only real inconvenience I've had to address with WSL2, and I'm looking forward to including it in more projects from now on.
Cover Photo by Tracy Adams on Unsplash
Comments