Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access to the web server which running on WSL(Windows Subsystem for Linux) from local network

After installing Ubuntu as WSL(Windows Subsystem for Linux) I've run:

root@teclast:~# python3 -m http.server Serving HTTP on 0.0.0.0 port 8000 ... 

and try to access to this web server from my windows machine http://0.0.0.0:8000 or http://192.168.1.178:8000 but no success, web server available only by the address http://127.0.0.1:8000 or http://localhost:8000 it means that I can't connect to this web server from another pc in my network. Is it possible to getting an access to WSL from outside?

like image 749
Roman Avatar asked Apr 14 '18 19:04

Roman


People also ask

How do I access WSL remotely?

Alternatively, you can open a Remote WSL window directly from VS Code: Start VS Code. Press F1, select Remote-WSL: New Window for the default distro or Remote-WSL: New Window using Distro for a specific distro. Use the File menu to open your folder.

How do I access WSL files from Windows to Linux?

From within the Windows Subsystem for Linux environment you want to browse, run the following command: explorer.exe . This will launch File Explorer showing the current Linux directory — you can browse the Linux environment's file system from there.


Video Answer


2 Answers

None of the existing answers work for me, as WSL2 is running in its own VM and has its own network adapter. You need some sort of bridge or port forwarding for non-localhost requests to work (i.e. from another host on the same network).

I found a script in https://github.com/microsoft/WSL/issues/4150 that worked to resolve the issue:

$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,10000,3000,5000);   #[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"; } 

Running this script from an elevated/admin Powershell session did the trick for me. This allows accessing the WSL2 service running on a port to be accessible from the Windows host IP at that same port.

like image 190
mlibby Avatar answered Sep 25 '22 15:09

mlibby


In your VM, execute ifconfig

You will see your IP in the first section (eth0:) inet x.x.x.x

This x.x.x.x is the IP you have to put in your browser.

like image 20
countach Avatar answered Sep 23 '22 15:09

countach