wsl 和win主机互相访问

参考文档

windows 访问

# windows 以太网 ip 地址
(curl http://192.168.0.100:8090/api/Tool/DateTime).Content
# window 在 WSL 上的 ip 地址
(curl http://172.26.192.1:8090/api/Tool/DateTime).Content
# windows localhost
(curl http://localhost:8090/api/Tool/DateTime).Content
# windows ipv4 127.0.0.1
(curl http://127.0.0.1:8090/api/Tool/DateTime).Content
# windows ipv6 127.0.0.1
(curl http://[::1]:8090/api/Tool/DateTime).Content
# windows wsl unbuntu系统的 ip 地址
(curl http://172.26.198.196:8090/api/Tool/DateTime).Content

# wsl 中查询windows访问wsl的ip地址
hostname -I
172.26.198.196

wsl unbuntu访问

# linux ubuntu localhost
curl -s http://localhost:8090/api/Tool/DateTime | sed '$a\'
# linux ubuntu ipv4 127.0.0.1
curl -s http://127.0.0.1:8090/api/Tool/DateTime | sed '$a\'
# linux ubuntu ipv6 127.0.0.1
curl -s http://[::1]:8090/api/Tool/DateTime | sed '$a\'
# linux ubuntu eth0网卡上的 ip 地址
curl -s http://172.26.198.196:8090/api/Tool/DateTime | sed '$a\'
# host.docker.internal 地址
curl -s http://host.docker.internal:8090/api/Tool/DateTime | sed '$a\'
curl -s http://gateway.docker.internal:8090/api/Tool/DateTime | sed '$a\'
curl -s http://kubernetes.docker.internal:8090/api/Tool/DateTime | sed '$a\'
# window 在 WSL 上的 ip 地址
curl -s http://172.26.192.1:8090/api/Tool/DateTime | sed '$a\'
# windows 以太网 ip 地址(无法访问)
curl -s http://192.168.0.100:8090/api/Tool/DateTime | sed '$a\'

# wsl 中查询wsl访问windows的ip地址
ip route show | grep -i default | awk '{ print $3}'
172.26.192.1

转发 wsl 服务端口到 windows,可通过windows 外部 ip 访问

即除了127.0.0.1外的其他 ip 地址都可以访问。

hostname -I
172.26.198.196

netsh interface portproxy add v4tov4 listenport=<yourPortToForward> listenaddress=0.0.0.0 connectport=<yourPortToConnectToInWSL> connectaddress=(wsl hostname -I)

netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=172.26.198.196
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=0.0.0.0

netsh interface portproxy add v4tov4 listenport=65535 listenaddress=0.0.0.0 connectport=65535 connectaddress=172.26.198.196

netsh interface portproxy show all

windows 防火墙允许 wsl 访问 windows 服务

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值