Operations 7 min read

How to Set Up Port Forwarding on Windows and Linux: Step‑by‑Step Guide

This tutorial explains how to configure port forwarding on both Windows (using netsh) and Linux (using iptables), covering query, addition, and deletion of mappings, enabling packet forwarding, and a practical experiment that maps internal services to the external network.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Set Up Port Forwarding on Windows and Linux: Step‑by‑Step Guide
Usually servers have many NICs and may connect to different networks; in isolated networks some services need communication, and the server can be configured to forward packets.

1. Implement Port Mapping on Windows

1. Query port mapping status netsh interface portproxy show v4tov4 2. Query mapping for a specific IP

netsh interface portproxy show v4tov4 | find "[IP]"

3. Add a port mapping

netsh interface portproxy add v4tov4 listenaddress=[external IP] listenport=[external port] connectaddress=[internal IP] connectport=[internal port]

4. Delete a port mapping

netsh interface portproxy delete v4tov4 listenaddress=[external IP] listenport=[external port]

2. Port Mapping on Linux

1. Enable packet forwarding

echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i [internal NIC] -j ACCEPT
iptables -t nat -A POSTROUTING -s [internal subnet] -o [external NIC] -j MASQUERADE

2. Set up port forwarding

iptables -t nat -A PREROUTING -p tcp -m tcp --dport [external port] -j DNAT --to-destination [internal IP]:[internal port]

Experiment: Map an Internal Service to the External Network

Experiment Environment

VMware Workstation Pro

5 minimal CentOS 7 virtual machines

Experiment Topology

Server1, Server2, Server3 are internal servers (192.168.50.0/24). Server4 is a dual‑NIC host connecting both the internal network (192.168.50.0/24) and the external network (172.16.2.0/24).

Configure Experiment Environment

1. Deploy HTTP service on Server1, Server2, Server3

On Server1 run:

cd ~
echo "server1" > index.html
python -m SimpleHTTPServer 8080

Similarly set up services on Server2 and Server3.

Control Experiment

From the client, curl the services:

curl http://192.168.50.11:8080/index.html
curl http://192.168.50.12:8080/index.htm
curl http://172.16.2.11:8080/index.html
The client cannot access the internal servers (Server1, Server2) directly.

Configure Port Mapping on Server4

Temporary configuration

# Enable packet 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
# Set port mapping
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8081 -j DNAT --to-destination 192.168.50.11:8080
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8082 -j DNAT --to-destination 192.168.50.12:8080

Permanent configuration

Append the above commands to /etc/rc.local to make them persistent.

Check Effect

From the client, access the mapped services:

curl http://172.16.2.100:8081/index.html
curl http://172.16.2.100:8082/index.html
curl http://172.16.2.11:8080/index.html

Windows Alternative

For a Windows Server4, replace the Linux commands with the equivalent netsh commands shown earlier.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxWindowsiptablesNetwork ConfigurationServer Setupport forwardingnetsh
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.