一、Windows 下實現端口映射
查詢端口映射情況
netshinterfaceportproxyshowv4tov4
查詢某一個 IP 的所有端口映射情況
netshinterfaceportproxyshowv4tov4|find"[IP]"
例:
netshinterfaceportproxyshowv4tov4|find"192.168.1.1"
增加一個端口映射
netshinterfaceportproxyaddv4tov4listenaddress=[外網IP]listenport=[外網端口]connectaddress=[內網IP]connectport=[內網端口]
例:
netshinterfaceportproxyaddv4tov4listenaddress=2.2.2.2listenport=8080connectaddress=192.168.1.50connectport=80
刪除一個端口映射
netshinterfaceportproxydeletev4tov4listenaddress=[外網IP]listenport=[外網端口]
例:
netshinterfaceportproxydeletev4tov4listenaddress=2.2.2.2listenport=8080
二、Linux 下實現端口映射
允許數據包轉發
echo1>/proc/sys/net/ipv4/ip_forward iptables-tnat-APOSTROUTING-jMASQUERADE iptables-AFORWARD-i[內網網卡名稱]-jACCEPT iptables-tnat-APOSTROUTING-s[內網網段]-o[外網網卡名稱]-jMASQUERADE
例:
echo1>/proc/sys/net/ipv4/ip_forward iptables-tnat-APOSTROUTING-jMASQUERADE iptables-AFORWARD-iens33-jACCEPT iptables-tnat-APOSTROUTING-s192.168.50.0/24-oens37-jMASQUERADE
設置端口映射
iptables-tnat-APREROUTING-ptcp-mtcp--dport[外網端口]-jDNAT--to-destination[內網地址]:[內網端口]
例:
iptables-tnat-APREROUTING-ptcp-mtcp--dport6080-jDNAT--to-destination10.0.0.100:6090
實驗:將部署在內網的服務映射到外網
實驗環境
VMWare Workstation Pro
5 臺最小化安裝的 centos 7 虛擬機
實驗拓撲
內網和外網是相對Server4來說的。Server1和Server2為內網環境的兩臺服務器;Server3為外網環境下的一臺服務器;Server4為一臺雙網卡主機,分別連接192.168.50.0/24和172.16.2.0/24兩個網絡。
配置實驗環境
1. Server1,2,3 上搭建 HTTP 服務
用 Python 在Server1上搭建一個簡單的 HTTP 服務
cd~ echo"server1">index.html python-mSimpleHTTPServer8080 Server2、Server3同理
對照實驗
在client上訪問Server1的資源
curlhttp://192.168.50.11:8080/index.html
在client上訪問Server2的資源
curlhttp://192.168.50.12:8080/index.html
在client上訪問Server3的資源
curlhttp://172.16.2.11:8080/index.html
可以看到,外網的client是無法訪問內網Server1,Server2的資源的。
在Server4上配置端口映射
臨時配置
#允許數據包轉發 echo1>/proc/sys/net/ipv4/ip_forward iptables-tnat-APOSTROUTING-jMASQUERADE iptables-AFORWARD-iens33-jACCEPT iptables-tnat-APOSTROUTING-s192.168.50.0/24-oens37-jMASQUERADE #設置端口映射 iptables-tnat-APREROUTING-ptcp-mtcp--dport8081-jDNAT--to-destination192.168.50.11:8080 iptables-tnat-APREROUTING-ptcp-mtcp--dport8082-jDNAT--to-destination192.168.50.12:8080
永久配置
如果需要永久配置,則將以上命令追加到/etc/rc.local文件。
檢查效果
在client上訪問Server1的資源
curlhttp://172.16.2.100:8081/index.html
在client上訪問Server2的資源
curlhttp://172.16.2.100:8082/index.html
在client上訪問Server3的資源
curlhttp://172.16.2.11:8080/index.html
如果Server4為 Windows,替換一下相應的命令即可
Windows 的 IP 信息如下
配置并查看端口映射情況
netshinterfaceportproxyaddv4tov4listenaddress=172.16.2.105listenport=8081connectaddress=192.168.50.11connectport=8080 netshinterfaceportproxyaddv4tov4listenaddress=172.16.2.105listenport=8082connectaddress=192.168.50.12connectport=8080 netshinterfaceportproxyshowv4tov4
檢查效果
在client節點上
curlhttp://172.16.2.105:8081/index.html curlhttp://172.16.2.105:8082/index.html curlhttp://172.16.2.11:8080/index.html
審核編輯:湯梓紅
-
Linux
+關注
關注
87文章
11420瀏覽量
212298 -
WINDOWS
+關注
關注
4文章
3598瀏覽量
90684 -
Server
+關注
關注
0文章
94瀏覽量
24426 -
端口
+關注
關注
4文章
1006瀏覽量
32634 -
映射
+關注
關注
0文章
48瀏覽量
16018
原文標題:Linux 或 Windows 上實現端口映射
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
[分享]免費小軟件——天銳端口映射器V1.0
Linux如何配置本地端口映射
NAT端口映射功能在CDMA無線數傳模塊組網中的應用是什么?
路由器端口映射的原理及設置
使用IdMappedPortTCP進行端口映射_Delphi教程
端口映射和端口轉發的區別?

如何通過路由器設置端口映射

評論