Linux或Windows上實現(xiàn)端口映射

通常服務(wù)器會有許多塊網(wǎng)卡,因此也可能會連接到不同的網(wǎng)絡(luò),在隔離的網(wǎng)絡(luò)中,某些服務(wù)可能會需要進(jìn)行通信,此時服務(wù)器經(jīng)過配置就可以承擔(dān)起了轉(zhuǎn)發(fā)數(shù)據(jù)包的功能。
一、Windows下實現(xiàn)端口映射
1. 查詢端口映射情況
netsh interface portproxy show v4tov4
2. 查詢某一個IP的所有端口映射情況
netsh interface portproxy show v4tov4 | find "[IP]"
例:
netsh interface portproxy show v4tov4 | find "192.168.1.1"
3. 增加一個端口映射
netsh interface portproxy add v4tov4 listenaddress=[外網(wǎng)IP] listenport=[外網(wǎng)端口] connectaddress=[內(nèi)網(wǎng)IP] connectport=[內(nèi)網(wǎng)端口]
例:
netsh interface portproxy add v4tov4 listenaddress=2.2.2.2 listenport=8080 connectaddress=192.168.1.50 connectport=80
4. 刪除一個端口映射
netsh interface portproxy delete v4tov4 listenaddress=[外網(wǎng)IP] listenport=[外網(wǎng)端口]
例:
netsh interface portproxy delete v4tov4 listenaddress=2.2.2.2 listenport=8080
二、Linux下實現(xiàn)端口映射
1. 允許數(shù)據(jù)包轉(zhuǎn)發(fā)
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i [內(nèi)網(wǎng)網(wǎng)卡名稱] -j ACCEPT
iptables -t nat -A POSTROUTING -s [內(nèi)網(wǎng)網(wǎng)段] -o [外網(wǎng)網(wǎng)卡名稱] -j MASQUERADE
例:
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
2. 設(shè)置端口映射
iptables -t nat -A PREROUTING -p tcp -m tcp --dport [外網(wǎng)端口] -j DNAT --to-destination [內(nèi)網(wǎng)地址]:[內(nèi)網(wǎng)端口]
例:
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 6080 -j DNAT --to-destination 10.0.0.100:6090
實驗:將部署在內(nèi)網(wǎng)的服務(wù)映射到外網(wǎng)
實驗環(huán)境
VMWare Workstation Pro
5臺最小化安裝的centos 7虛擬機
實驗拓?fù)?/h1>

內(nèi)網(wǎng)和外網(wǎng)是相對Server4
來說的。Server1
和Server2
為內(nèi)網(wǎng)環(huán)境的兩臺服務(wù)器;Server3
為外網(wǎng)環(huán)境下的一臺服務(wù)器;Server4
為一臺雙網(wǎng)卡主機,分別連接192.168.50.0/24
和172.16.2.0/24
兩個網(wǎng)絡(luò)。
配置實驗環(huán)境
1. Server1,2,3上搭建HTTP服務(wù)
用Python在Server1
上搭建一個簡單的HTTP服務(wù)
cd ~
echo "server1" > index.html
python -m SimpleHTTPServer 8080

Server2
、Server3
同理
對照實驗
在client
上訪問Server1
的資源
curl http://192.168.50.11:8080/index.html

在client
上訪問Server2
的資源
curl http://192.168.50.12:8080/index.html

在client
上訪問Server3
的資源
curl http://172.16.2.11:8080/index.html

可以看到,外網(wǎng)的client
是無法訪問內(nèi)網(wǎng)Server1
,Server2
的資源的。
在Server4
上配置端口映射
臨時配置
#允許數(shù)據(jù)包轉(zhuǎn)發(fā)
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
#設(shè)置端口映射
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
永久配置
如果需要永久配置,則將以上命令追加到/etc/rc.local
文件。
檢查效果
在client
上訪問Server1
的資源
curl http://172.16.2.100:8081/index.html

在client
上訪問Server2
的資源
curl http://172.16.2.100:8082/index.html

在client
上訪問Server3
的資源
curl http://172.16.2.11:8080/index.html

如果Server4
為Windows,替換一下相應(yīng)的命令即可
Windows的IP信息如下
網(wǎng)卡IP地址子網(wǎng)掩碼默認(rèn)網(wǎng)關(guān)備注Ethernet0192.168.50.105255.255.255.0-內(nèi)網(wǎng)網(wǎng)卡Ethernet1172.16.2.105255.255.255.0-外網(wǎng)網(wǎng)卡

配置并查看端口映射情況
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 v4tov4

檢查效果
在client
節(jié)點上
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.html