自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

系統(tǒng) Linux
通常服務(wù)器會有許多塊網(wǎng)卡,因此也可能會連接到不同的網(wǎng)絡(luò),在隔離的網(wǎng)絡(luò)中,某些服務(wù)可能會需要進(jìn)行通信,此時服務(wù)器經(jīng)過配置就可以承擔(dān)起了轉(zhuǎn)發(fā)數(shù)據(jù)包的功能。

 [[353045]]

通常服務(wù)器會有許多塊網(wǎng)卡,因此也可能會連接到不同的網(wǎng)絡(luò),在隔離的網(wǎng)絡(luò)中,某些服務(wù)可能會需要進(jìn)行通信,此時服務(wù)器經(jīng)過配置就可以承擔(dān)起了轉(zhuǎn)發(fā)數(shù)據(jù)包的功能。

一、Windows下實(shí)現(xiàn)端口映射

1.  查詢端口映射情況 

  1. netsh interface portproxy show v4tov4 

2. 查詢某一個IP的所有端口映射情況 

  1. netsh interface portproxy show v4tov4 | find "[IP]"  
  2. 例:  
  3. netsh interface portproxy show v4tov4 | find "192.168.1.1" 

3. 增加一個端口映射 

  1. netsh interface portproxy add v4tov4 listenaddress=[外網(wǎng)IP] listenport=[外網(wǎng)端口] connectaddress=[內(nèi)網(wǎng)IP] connectport=[內(nèi)網(wǎng)端口]  
  2. 例:  
  3. netsh interface portproxy add v4tov4 listenaddress=2.2.2.2 listenport=8080 connectaddress=192.168.1.50 connectport=80 

4. 刪除一個端口映射 

  1. netsh interface portproxy delete v4tov4 listenaddress=[外網(wǎng)IP] listenport=[外網(wǎng)端口]  
  2. 例:  
  3. netsh interface portproxy delete v4tov4 listenaddress=2.2.2.2 listenport=8080 

二、Linux下端口映射

1. 允許數(shù)據(jù)包轉(zhuǎn)發(fā) 

  1. echo 1 >/proc/sys/net/ipv4/ip_forward  
  2. iptables -t nat -A POSTROUTING -j MASQUERADE  
  3. iptables -A FORWARD -i [內(nèi)網(wǎng)網(wǎng)卡名稱] -j ACCEPT  
  4. iptables -t nat -A POSTROUTING -s [內(nèi)網(wǎng)網(wǎng)段] -o [外網(wǎng)網(wǎng)卡名稱] -j MASQUERADE  
  5. 例:  
  6. echo 1 >/proc/sys/net/ipv4/ip_forward  
  7. iptables -t nat -A POSTROUTING -j MASQUERADE  
  8. iptables -A FORWARD -i ens33 -j ACCEPT  
  9. iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens37 -j MASQUERADE 

2. 設(shè)置端口映射 

  1. iptables -t nat -A PREROUTING -p tcp -m tcp --dport [外網(wǎng)端口] -j DNAT --to-destination [內(nèi)網(wǎng)地址]:[內(nèi)網(wǎng)端口]  
  2. 例: 
  3. 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)境

  1.  VMWare Workstation Pro
  2.  5臺最小化安裝的centos 7虛擬機(jī)

實(shí)驗(yàn)拓?fù)?/strong>

內(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)卡主機(jī),分別連接192.168.50.0/24和172.16.2.0/24兩個網(wǎng)絡(luò)。

配置實(shí)驗(yàn)環(huán)境

1. Server1,2,3上搭建HTTP服務(wù)

用Python在Server1上搭建一個簡單的HTTP服務(wù) 

  1. cd ~  
  2. echo "server1" > index.html  
  3. python -m SimpleHTTPServer 8080 

Server2、Server3同理

對照實(shí)驗(yàn)

在client上訪問Server1的資源 

  1. curl http://192.168.50.11:8080/index.html 

在client上訪問Server2的資源 

  1. curl http://192.168.50.12:8080/index.htm 

在client上訪問Server3的資源 

  1. curl http://172.16.2.11:8080/index.html 

可以看到,外網(wǎng)的client是無法訪問內(nèi)網(wǎng)Server1,Server2的資源的。

在Server4上配置端口映射

臨時配置 

  1. #允許數(shù)據(jù)包轉(zhuǎn)發(fā)  
  2. echo 1 >/proc/sys/net/ipv4/ip_forward  
  3. iptables -t nat -A POSTROUTING -j MASQUERADE  
  4. iptables -A FORWARD -i ens33 -j ACCEPT  
  5. iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens37 -j MASQUERADE  
  6. #設(shè)置端口映射  
  7. iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8081 -j DNAT --to-destination 192.168.50.11:8080  
  8. 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的資源 

  1. curl http://172.16.2.100:8081/index.html 

在client上訪問Server2的資源 

  1. curl http://172.16.2.100:8082/index.html 

在client上訪問Server3的資源 

  1. 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)卡


配置并查看端口映射情況 

  1. netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8081 connectaddress=192.168.50.11 connectport=8080  
  2. netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8082 connectaddress=192.168.50.12 connectport=8080  
  3. netsh interface portproxy show v4tov4 

檢查效果

在client節(jié)點(diǎn)上 

  1. curl http://172.16.2.105:8081/index.html  
  2. curl http://172.16.2.105:8082/index.html  
  3. curl http://172.16.2.11:8080/index.html 

 

 

責(zé)任編輯:龐桂玉 來源: 良許Linux
相關(guān)推薦

2021-08-17 00:02:11

LinuxWindows服務(wù)器

2009-09-16 08:49:29

linux端口映射linux端口linux

2011-03-16 13:29:33

iptables 端口

2019-07-25 15:15:54

端口映射服務(wù)器

2011-03-16 12:55:00

iptables 端口

2011-03-16 13:09:10

iptables 端口

2011-03-17 13:55:23

iptablesNAT端口映射

2011-03-16 10:43:36

2011-03-17 09:35:35

iptables 映射Linux內(nèi)核

2010-09-02 14:47:56

2009-12-15 16:21:53

水星路由器端口映射

2011-08-25 14:35:04

Nth端口映射ftp服務(wù)器

2009-12-02 18:51:11

2009-12-15 16:36:12

路由器端口映射

2013-06-07 17:25:46

路由技術(shù)路由器

2009-12-15 16:15:56

2009-12-15 16:09:54

水星MR804端口映射

2010-08-05 10:39:32

路由端口

2010-08-02 12:00:06

ADSL Modem端口映射

2009-12-15 15:50:22

路由器端口映射
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號