我有一个用例,在这个用例中,我在Windows和WSL2上都进行了开发,并且需要使.NET应用程序能够在端口44301的Windows上服务,并且可以从本地主机上的WSL2 (Ubuntu)访问:44301。
我知道Windows主机有一个可以从WSL2内部访问的IP (这可以用grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'发现),但这不是我想要实现的。我想做的是将windows上的44301端口转发到localhost:44301在WSL2上
发布于 2023-05-25 13:07:37
我对WSL2不太了解,但在真正的Linux上,您可能会这样做:
#!/bin/sh
SERVERIP=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}')
# Turn on the IPv4 packet forwarding master switch
sysctl net.ipv4.ip_forward=1
# Change the destination of TCP packets arriving to loopback interface's
# port 44301 to the same port on the actual Windows server
iptables -t nat -A PREROUTING -i lo -p tcp --dport 44301 -j DNAT --to-destination $SERVERIP
# Allow the forwarding of any existing forwarded connections
# (to ensure replies from the Windows server are also allowed)
iptables -t filter -A FORWARD -m conntrack --ctstate ESTABLISHED -j ACCEPT
# Allow the forwarding of connections arriving to loopback TCP port 44301
iptables -t filter -A FORWARD -p tcp -i lo --dport 44301 -j ACCEPThttps://unix.stackexchange.com/questions/747003
复制相似问题