How to Set Up Cross‑Network Port Mapping on Windows and Linux
This guide explains how to configure port forwarding on both Windows and Linux servers using netsh and iptables, demonstrates temporary and permanent setups, and shows how to verify the mappings with curl in a multi‑machine lab environment.
1. Windows Port Mapping
1. Query existing mappings
netsh interface portproxy show v4tov42. Query mappings for a specific IP
netsh interface portproxy show v4tov4 | find "[IP]"3. Add a new mapping
netsh interface portproxy add v4tov4 listenaddress=[ExternalIP] listenport=[ExternalPort] connectaddress=[InternalIP] connectport=[InternalPort]4. Delete a mapping
netsh interface portproxy delete v4tov4 listenaddress=[ExternalIP] listenport=[ExternalPort]2. Linux Port Mapping
1. Enable packet forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i [InternalInterface] -j ACCEPT
iptables -t nat -A POSTROUTING -s [InternalSubnet] -o [ExternalInterface] -j MASQUERADE2. Set up a port forwarding rule
iptables -t nat -A PREROUTING -p tcp --dport [ExternalPort] -j DNAT --to-destination [InternalIP]:[InternalPort]3. Example topology
4. Configure temporary mapping on Server4 (Linux)
# Enable forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i ens33 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens37 -j MASQUERADE
# Port mappings
iptables -t nat -A PREROUTING -p tcp --dport 8081 -j DNAT --to-destination 192.168.50.11:8080
iptables -t nat -A PREROUTING -p tcp --dport 8082 -j DNAT --to-destination 192.168.50.12:80805. Permanent configuration (Linux)
Append the same commands to /etc/rc.local to make them survive reboots.
6. Verify the mappings
curl http://172.16.2.100:8081/index.html
curl http://172.16.2.100:8082/index.html3. Windows Equivalent Commands
Replace the Linux iptables commands with the corresponding netsh interface portproxy commands shown in section 1.
4. Sample Windows network settings
Two NICs: Ethernet0 – 192.168.50.105/24 (internal), Ethernet1 – 172.16.2.105/24 (external).
5. Windows permanent mapping example
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8081 connectaddress=192.168.50.11 connectport=8080
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8082 connectaddress=192.168.50.12 connectport=8080
netsh interface portproxy show v4tov46. Test from a client machine
curl http://172.16.2.105:8081/index.html
curl http://172.16.2.105:8082/index.html
curl http://172.16.2.11:8080/index.htmlSuccessful responses confirm that the external client can reach services hosted on the internal network through the configured port mappings.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
