一、Windows 下實(shí)現(xiàn)端口映射
查詢端口映射情況
netshinterfaceportproxyshowv4tov4
查詢某一個(gè) IP 的所有端口映射情況
netshinterfaceportproxyshowv4tov4|find"[IP]"
例:
netshinterfaceportproxyshowv4tov4|find"192.168.1.1"
增加一個(gè)端口映射
netshinterfaceportproxyaddv4tov4listenaddress=[外網(wǎng)IP]listenport=[外網(wǎng)端口]connectaddress=[內(nèi)網(wǎng)IP]connectport=[內(nèi)網(wǎng)端口]
例:
netshinterfaceportproxyaddv4tov4listenaddress=2.2.2.2listenport=8080connectaddress=192.168.1.50connectport=80
刪除一個(gè)端口映射
netshinterfaceportproxydeletev4tov4listenaddress=[外網(wǎng)IP]listenport=[外網(wǎng)端口]
例:
netshinterfaceportproxydeletev4tov4listenaddress=2.2.2.2listenport=8080
二、Linux 下實(shí)現(xiàn)端口映射
允許數(shù)據(jù)包轉(zhuǎn)發(fā)
echo1>/proc/sys/net/ipv4/ip_forward iptables-tnat-APOSTROUTING-jMASQUERADE iptables-AFORWARD-i[內(nèi)網(wǎng)網(wǎng)卡名稱]-jACCEPT iptables-tnat-APOSTROUTING-s[內(nèi)網(wǎng)網(wǎng)段]-o[外網(wǎng)網(wǎng)卡名稱]-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
設(shè)置端口映射
iptables-tnat-APREROUTING-ptcp-mtcp--dport[外網(wǎng)端口]-jDNAT--to-destination[內(nèi)網(wǎng)地址]:[內(nèi)網(wǎng)端口]
例:
iptables-tnat-APREROUTING-ptcp-mtcp--dport6080-jDNAT--to-destination10.0.0.100:6090
實(shí)驗(yàn):將部署在內(nèi)網(wǎng)的服務(wù)映射到外網(wǎng)
實(shí)驗(yàn)環(huán)境
VMWare Workstation Pro
5 臺(tái)最小化安裝的 centos 7 虛擬機(jī)
實(shí)驗(yàn)拓?fù)?/p>
內(nèi)網(wǎng)和外網(wǎng)是相對Server4來說的。Server1和Server2為內(nèi)網(wǎng)環(huán)境的兩臺(tái)服務(wù)器;Server3為外網(wǎng)環(huán)境下的一臺(tái)服務(wù)器;Server4為一臺(tái)雙網(wǎng)卡主機(jī),分別連接192.168.50.0/24和172.16.2.0/24兩個(gè)網(wǎng)絡(luò)。
配置實(shí)驗(yàn)環(huán)境
1. Server1,2,3 上搭建 HTTP 服務(wù)
用 Python 在Server1上搭建一個(gè)簡單的 HTTP 服務(wù)
cd~ echo"server1">index.html python-mSimpleHTTPServer8080 Server2、Server3同理
對照實(shí)驗(yàn)
在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
可以看到,外網(wǎng)的client是無法訪問內(nèi)網(wǎng)Server1,Server2的資源的。
在Server4上配置端口映射
臨時(shí)配置
#允許數(shù)據(jù)包轉(zhuǎn)發(fā) 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 #設(shè)置端口映射 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,替換一下相應(yīng)的命令即可
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節(jié)點(diǎn)上
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
+關(guān)注
關(guān)注
87文章
11509瀏覽量
213724 -
WINDOWS
+關(guān)注
關(guān)注
4文章
3613瀏覽量
91387 -
Server
+關(guān)注
關(guān)注
0文章
94瀏覽量
24693 -
端口
+關(guān)注
關(guān)注
4文章
1046瀏覽量
32933 -
映射
+關(guān)注
關(guān)注
0文章
48瀏覽量
16180
原文標(biāo)題:Linux 或 Windows 上實(shí)現(xiàn)端口映射
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
[分享]免費(fèi)小軟件——天銳端口映射器V1.0
Linux如何配置本地端口映射
NAT端口映射功能在CDMA無線數(shù)傳模塊組網(wǎng)中的應(yīng)用是什么?
路由器端口映射的原理及設(shè)置
使用IdMappedPortTCP進(jìn)行端口映射_Delphi教程
關(guān)于自動(dòng)端口映射功能實(shí)現(xiàn)步驟和調(diào)試

端口映射和端口轉(zhuǎn)發(fā)的區(qū)別?

如何通過路由器設(shè)置端口映射

評論