WSL-2: Which ports are automatically forwarded?

When you run WSL-2, a machine like a vitural machin run on your window device. Windows will create a local network, same your LAN, and connect WLS-2 to this network.

  • On your WSL2, you can run ip a | grep eth0, result look like:

5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 172.27.5.124/20 brd 172.27.15.255 scope global eth0

  • And on terminal (or PowerShell) on your windows, run ipconfig, find in the result, look like:
Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::1532:a8ea:6b34:8df2%73
   IPv4 Address. . . . . . . . . . . : 172.27.0.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

WSL-2 and Windows device on a same network, and WSL-2 not connect to your LAN.

My solution (use port forwarding on Windows)

Open terminal with Admin on Windows, and run script:

netsh interface portproxy set v4tov4 listenport=8888 listenaddress=0.0.0.0 connectport=8888 connectaddress=$(wsl hostname -I)

Leave a Comment

tech