windows WSL 端口映射
实际应用中需要将嵌入式开发板的数据通过网络发送出去,如果 WSL 中运行的 client 要与嵌入式开发板进行通讯,则需要将 windows 的端口转发到 WSL 中,反之,如果配置了 windows 端口转发到 WSL 中,则 windows client 无法正常接收开发板的数据,这一点需要注意,需要取消端口转发 windows 端才可以正常接收数据。
From: https://learn.microsoft.com/en-us/windows/wsl/networking
Here’s an example of using the Netsh interface portproxy Windows command to add a port proxy that listens on your host port and connects that port proxy to the IP address for the WSL 2 VM.
netsh interface portproxy add v4tov4 listenport=<yourPortToForward> listenaddress=0.0.0.0 connectport=<yourPortToConnectToInWSL> connectaddress=(wsl hostname -I)
In this example, you will need to update <yourPortToForward>
to a port number, for example listenport=4000
. listenaddress=0.0.0.0
means that incoming requests will be accepted from ANY IP address. The Listen Address specifies the IPv4 address for which to listen and can be changed to values that include: IP address, computer NetBIOS name, or computer DNS name. If an address isn’t specified, the default is the local computer. You need to update the <yourPortToConnectToInWSL>
value to a port number where you want WSL to connect, for example connectport=4000
. Lastly, the connectaddress
value needs to be the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address), which can be found by entering the command: wsl.exe hostname -I
.
So this command may look something like:
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100
To obtain the IP address, use:
wsl hostname -I
for the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)cat /etc/resolv.conf
for the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)
Using listenaddress=0.0.0.0
will listen on all
IPv4 ports
.
Using a lowercase "i" with the hostname command will generate a different result than using an uppercase "I". wsl hostname -i is your local machine (127.0.1.1 is a placeholder diagnostic address), whereas wsl hostname -I will return your local machine's IP address as seen by other machines and should be used to identify the connectaddress of your Linux distribution running via WSL 2.
查询当前已经转发的端口
如何查看当前 windows -> WSL 配置了哪些端口转发? 以管理员身份运行 PowerShell:
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666
确认 WSL IP 地址
WSL 终端运行:
$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:d3ff:fe45:67a4 prefixlen 64 scopeid 0x20<link>
ether 02:42:d3:45:67:a4 txqueuelen 0 (Ethernet)
RX packets 12553 bytes 108393702 (108.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15258 bytes 1786658 (1.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.198.88 netmask 255.255.240.0 broadcast 172.25.207.255
inet6 fe80::215:5dff:feec:a420 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:ec:a4:20 txqueuelen 1000 (Ethernet)
RX packets 201105667 bytes 290122284896 (290.1 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 55298591 bytes 3209600213 (3.2 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 217290 bytes 624612200 (624.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 217290 bytes 624612200 (624.6 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth11e7cb7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::fcfb:17ff:fe7d:ad3 prefixlen 64 scopeid 0x20<link>
ether fe:fb:17:7d:0a:d3 txqueuelen 0 (Ethernet)
RX packets 12553 bytes 108569444 (108.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15279 bytes 1788224 (1.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如上: eth0
172.25.198.88
增加端口4864
映射
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=4864 connectaddress=172.25.198.88 4864
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666
0.0.0.0 4864 172.25.198.88 4864
删除端口4864
映射
netsh interface portproxy delete v4tov4 listenport=4864 listenaddress=0.0.0.0
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666