windows与wsl互相访问,windows10 / 11与子系统网络互相访问

找出能与WSL2连接的那个IP。
启动WSL2,在子系统Linux键入如下命令:

cat /etc/resolv.conf

如:

nameserver 172.27.160.1

172.27.160.1就是我们要找的Windows的IP Address

找出wsl的ip
启动WSL2,键入如下命令:

$ip a |grep “global eth0”
输出举例:

inet 172.27.170.146/20 brd 172.27.175.255 scope global eth0

172.27.170.146就是我们要找的WSL2的IP Address

防火墙规则
默认情况下Windows的防火墙会阻止WSL2中应用对Windows的网络访问(see: Add “allow” rule to Windows firewall for WSL2 network · Issue #4585 · microsoft/WSL (github.com)),解决办法是添加一条防火墙规则允许WSL2对Windows的访问。请以管理员身份打开PowerShell并键入以下命令:

 PS C:> New-NetFirewallRule -DisplayName WSL -Direction Inbound -InterfaceAlias vEthernet (WSL) -Action Allow


posted @ 2022-09-20 19:33  anakinf  阅读(1343)  评论(0编辑  收藏  举报