win11 访问127.0.0.1

Win10 本机访问 WSL2 对于 WSL2 中监听 0.0.0.0 的程序,Win10 可直接通过 localhost:port 访问。如仅监听 127.0.0.1,则 WSL2 需转发请求,以下命令可完成该需求。

➜ LOGI cat » ~/.profile «EOF function expose_local() { sysctl -w net.ipv4.conf.all.route_localnet=1 iptables -t nat -A PREROUTING -j DNAT –to 127.0.0.1 }

function protect_local() { sysctl -w net.ipv4.conf.all.route_localnet=0 iptables -t nat -D PREROUTING 1 } EOF ➜ LOGI source ~/.profile ➜ LOGI expose_local # 转发 ➜ LOGI protect_local# 取消转发

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up