通常服務(wù)器會(huì)有許多塊網(wǎng)卡,因此也可能會(huì)連接到不同的網(wǎng)絡(luò),在隔離的網(wǎng)絡(luò)中,某些服務(wù)可能會(huì)需要進(jìn)行通信,此時(shí)服務(wù)器經(jīng)過(guò)配置就可以承擔(dān)起了轉(zhuǎn)發(fā)數(shù)據(jù)包的功能。
一、windows 下實(shí)現(xiàn)端口映射
1.? 查詢端口映射情況
netsh?interface?portproxy?show?v4tov4
2. 查詢某一個(gè) IP 的所有端口映射情況
netsh?interface?portproxy?show?v4tov4?|?find?"[IP]"例:netsh?interface?portproxy?show?v4tov4?|?find?"192.168.1.1"
3. 增加一個(gè)端口映射
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. 刪除一個(gè)端口映射
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 下端口映射
1. 允許數(shù)據(jù)包轉(zhuǎn)發(fā)
echo?1?>/proc/sys/net/ipv4/ip_forwardiptables?-t?nat?-A?POSTROUTING?-j?MASQUERADEiptables?-A?FORWARD?-i?[內(nèi)網(wǎng)網(wǎng)卡名稱]?-j?ACCEPTiptables?-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_forwardiptables?-t?nat?-A?POSTROUTING?-j?MASQUERADEiptables?-A?FORWARD?-i?ens33?-j?ACCEPTiptables?-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
實(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ù)?/span>

內(nèi)網(wǎng)和外網(wǎng)是相對(duì)Server4來(lái)說(shuō)的。
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è)簡(jiǎn)單的 HTTP 服務(wù)
cd?~echo?"server1"?>?index.htmlpython?-m?SimpleHTTPServer?8080

Server2、Server3同理
對(duì)照實(shí)驗(yàn)
在client上訪問(wèn)Server1的資源
curl?http://192.168.50.11:8080/index.html

在client上訪問(wèn)Server2的資源
curl?http://192.168.50.12:8080/index.htm

在client上訪問(wèn)Server3的資源
curl?http://172.16.2.11:8080/index.html

可以看到,外網(wǎng)的client是無(wú)法訪問(wèn)內(nèi)網(wǎng)Server1,Server2的資源的。
在Server4上配置端口映射
臨時(shí)配置
#允許數(shù)據(jù)包轉(zhuǎn)發(fā)echo?1?>/proc/sys/net/ipv4/ip_forwardiptables?-t?nat?-A?POSTROUTING?-j?MASQUERADEiptables?-A?FORWARD?-i?ens33?-j?ACCEPTiptables?-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:8080iptables?-t?nat?-A?PREROUTING?-p?tcp?-m?tcp?--dport?8082?-j?DNAT?--to-destination?192.168.50.12:8080
永久配置
如果需要永久配置,則將以上命令追加到/etc/rc.local文件。
檢查效果
在client上訪問(wèn) Server1 的資源
curl?http://172.16.2.100:8081/index.html
在client上訪問(wèn)Server2的資源
curl?http://172.16.2.100:8082/index.html

在client上訪問(wèn)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) | 備注 |
---|---|---|---|---|
Ethernet0 | 192.168.50.105 | 255.255.255.0 | – | 內(nèi)網(wǎng)網(wǎng)卡 |
Ethernet1 | 172.16.2.105 | 255.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=8080netsh?interface?portproxy?add?v4tov4?listenaddress=172.16.2.105?listenport=8082?connectaddress=192.168.50.12?connectport=8080netsh?interface?portproxy?show?v4tov4
檢查效果
在client節(jié)點(diǎn)上
curl?http://172.16.2.105:8081/index.htmlcurl?http://172.16.2.105:8082/index.htmlcurl?http://172.16.2.11:8080/index.html
