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)"

posted @   DHclly  阅读(321)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示