How to Forward Ports and Use Port Proxies
So I came across a need to Forward a port from one windows machine to another windows machine and found a nice solution using netsh and port proxies. In this example I will use port proxies to forward a Remote Desktop Port 3389 (RDP) from one server to another to allow access to this server via RDP by connecting to a local port like localhost:5000.
- Create the Port Proxy
- Open Admin Command Prompt:
netsh interface portproxy add v4tov4 listenport=freeport listenaddress=localip connectport=rdpport(default rdp is 3389) connectaddress=remoteip
- Example:
netsh interface portproxy add v4tov4 listenport=5000 listenaddress=192.168.1.25 connectport=3389 connectaddress=192.269.1.57
- Open Admin Command Prompt:
- Create the Firewall Rule:
- You will need to choose a local port that is not in use. If you need to know how to check if a port is in use check out my Quick Tip Post on how to do that here: How to check if a port is in Use and What Process is running on it
- Command:
netsh advfirewall firewall add rule name="Custom Port Rule" dir={in |out} action={allow | block} localport={number| n-n} protocol={TCP | UDP} remoteip={any | ip | range} profile={any | domain | public | private }
Example:
netsh advfirewall firewall add rule name="Custom Port Forward 5000" dir=in action=allow localport=5000 protocol=TCP remoteip=any profile="Domain"
- Output:
- Use the Port Proxy:
- Syntax:
{localhost | ip or hostname you created the rule on}:{port number you chose}
- Example:
mstsc locahost:5000 /admin
- Syntax:
- Delete the Port Proxy:
- Syntax:
netsh interface portproxy delete v4tov4 listenport=portyouchose listenaddress=localip
Example:
netsh interface portproxy delete v4tov4 listenport=5000 listenaddress=192.168.1.25
- Syntax:
- Delete the Firewall Rule:
- Syntax:
netsh advfirewall firewall delete rule name="firewall rule name"
- Example:
netsh advfirewall firewall delete rule name="Custom Port Forward 5000"
- Output:
- Syntax: