建议去看看hyper-v的官方文档,写得很清楚。虚拟机网络拓扑问题而已。似乎wsl2默认
是nat,而你要的是host上端口映射到vm,或者是vm的网卡桥接到host的lan。
https://docs.microsoft.com/en-us/windows/wsl/networking#accessing-a-wsl-2-di
stribution-from-your-local-area-network-lan
When using a WSL 1 distribution, if your computer was set up to be accessed
by your LAN, then applications run in WSL could be accessed on your LAN as w
ell.
This isn't the default case in WSL 2. WSL 2 has a virtualized ethernet adapt
er with its own unique IP address. Currently, to enable this workflow you wi
ll need to go through the same steps as you would for a regular virtual mach
ine. (We are looking into ways to improve this experience.)
Here's an example PowerShell command to add a port proxy that listens on por
t 4000 on the host and connects it to port 4000 to the WSL 2 VM with IP addr
ess 192.168.101.100.
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 c
onnectport=4000 connectaddress=192.168.101.100
【 在 misslost 的大作中提到: 】
: 我的希望是局域网端口转发即可。主要在开发中使用的,并不是正式的对外服务的。
: 主要是不熟悉路由表的配置,而且ip变动每次手动配置似乎也太麻烦。
--
FROM 119.129.53.*