|NO.Z.00049|——————————|Applications|——|防火牆.V3|----------------------------------------------|3臺server|
阿新 • • 發佈:2022-03-22
[Applications:防火牆.V3] [Applications.LBC&HAC$HPC] [|安全防護|防火牆|SNAT轉換|DNAT轉換|iptables指令碼|firewalld修改為iptables|3臺server|]
一、SNAT環境
二、未啟用的SNAT
三、啟用的SNAT
### --- 前提條件 ~~~ 區域網各主機正確設定IP地址/子網掩碼 ~~~ 區域網各主機正確設定預設閘道器地址 ~~~ Linux閘道器支援IP路由轉發
### --- 實現方法:編寫SNAT轉換規則 ~~~ iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 218.29.30.31 ~~~ MASQUERADE —— 地址偽裝 ~~~ 適用於外網IP地址,非固定的情況 ~~~ 對於ADSL撥號連線,介面通常為ppp0、ppp1 ~~~ 將SNAT規則改為MASQUERADE 即可 ~~~ iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
四、實驗專題.SNAT轉換<——通過SNAT轉換讓內網主機訪問公網。
### --- HA-server1:10.10.10.11:私有客戶端內網 ### --- HA-server2:10.10.10.12:第一塊網絡卡:僅主機模式。第二塊網絡卡:NAT模式;可以訪問外網 ### --- 新增路由交給10.10.10.12 [root@server11 ~]# echo "GATEWAY=10.10.10.12" >>/etc/sysconfig/network-scripts/ifcfg-eth0 [root@server11 ~]# service network restart [root@server11 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0 0.0.0.0 10.10.10.12 0.0.0.0 UG 0 0 0 eth0
### --- 設定HA-server2第二塊網絡卡為DHCP可以訪問公網
[root@server12 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
[root@server12 ~]# service network restart
[root@server12 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 // 開啟路由轉發
[root@server12 ~]# sysctl -p
net.ipv4.ip_forward = 1 // 重新整理核心規則
### --- 開啟防火牆並清除預設規則
[root@server12 ~]# service iptables start
[root@server12 ~]# chkconfig iptables on
[root@server12 ~]# iptables -L
[root@server12 ~]# iptables -F
### --- 增加SNAT轉換規則
[root@server12 ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth1 -j SNAT --to-source 192.168.120.129 //-t nat:nat表 -A POSTROUTING:路由後 -s 10.10.10.0/24內網地址 -o eth1:出口網絡卡 -j SNAT:動作SNAT轉換 --to-source 192.168.120.129出口地址
[root@server12 ~]# iptables -t nat -L
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 10.10.10.0/24 anywhere to:192.168.120.129
[root@server12 ~]# service iptables restart // 重啟iptables這條規則就消失,因為是臨時生效的
[root@server12 ~]# iptables -t nat -L
### --- 新增SNAT規則永久生效;持久化
[root@server12 ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth1 -j SNAT --to-source 192.168.120.129
[root@server12 ~]# iptables -t nat -L
[root@server12 ~]# service iptables save // 儲存永久生效
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@server12 ~]# cat /etc/sysconfig/iptables
*nat // 代表那張表
:PREROUTING ACCEPT [0:0] // PREROUTING 預設規則是ACCEPT[0過濾的資料包數:0過濾的位元組數]
:POSTROUTING ACCEPT [1:69] // POSTROUTING 預設規則時ACCEPT
:OUTPUT ACCEPT [1:69] // OUTPUT 預設規則是ACCEPT
-A POSTROUTING -s 10.10.10.0/24 -o eth1 -j SNAT --to-source 192.168.120.129 // 剛才新增的規則
COMMIT // COMMIT 表示這條規則結束
[root@server12 ~]# vim /etc/sysconfig/iptables
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [1:69]
:OUTPUT ACCEPT [1:69]
-A POSTROUTING -s 10.10.10.0/24 -o eth1 -j SNAT --to-source 192.168.120.129
COMMIT
[root@server12 ~]# service iptables save
[root@server12 ~]# service iptables restart
[root@server12 ~]# iptables -t nat -L
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 10.10.10.0/24 anywhere to:192.168.120.129
### --- 驗證:在HA-server1:10.10.10.11通過HA-server2:10.10.10.12上網。
[root@server11 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@server11 ~]# service network restart
[root@server11 ~]# ping baidu.com // HA-server1:10.10.10.11為僅主機模式,不可訪問公網,新增路由設定SNAT轉換可以訪問公網。
64 bytes from 39.156.69.79: icmp_seq=6 ttl=127 time=40.9 ms
五、通過MASQUERADE自動的識別並新增公網地址
### --- 在真正的生產環境中,做的SNAT轉換,需要些固定的公網地址,若是每次都需要抓取公網地址,動態的轉換機制MASQUERADE:地址偽裝
~~~ MASQUERADE自動的判斷你的公網地址是多少,而不需要去指定公網地址。
### --- 新增一條動態的SNAT轉化並驗證
[root@server12 ~]# iptables -t nat -F // 清空NAT表
[root@server12 ~]# iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth1 -j MASQUERADE
[root@server12 ~]# iptables -t nat -L
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 10.10.10.0/24 anywhere // 自動的判斷地址是多少且新增上,其實這條規則就是家裡的路由器設定的規則。 若是這臺伺服器安裝了DHCP且添加了這條規則,那麼就可以當做家裡的路由器來使用了。
[root@server11 ~]# ping baidu.com // 可以正常訪問
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148: icmp_seq=11 ttl=127 time=746 ms
六、DNAT策略
### --- DNAT策略的典型應用環境
### --- 在Internet中釋出位於企業區域網內的伺服器
~~~ DNAT策略的原理
~~~ 目標地址轉換:Destination Network Address Translation
~~~ 修改資料包的目標地址
### --- DNAT策略的典型應用環境
~~~ 內網中的伺服器肯定是不可以被客戶端直接訪問到的。
~~~ 只能訪問到路由器上,路由器通過DNAT轉化把資料包傳輸到伺服器。
### --- 前提條件
~~~ 區域網的web伺服器能夠訪問Internet
~~~ 閘道器的外網IP地址有正確的DNS解析記錄
~~~ Linux閘道器支援IP路由轉發
~~~ 實現方法
~~~ 編寫DNAT轉換規則
~~~ iptables -t nat -A PREROUTING -i eht0 -d 218.29.30.31 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.6
### --- 釋出時修改目標埠
~~~ 在DNAT規則中以“IP:Port”的形式指定目標地址
~~~ iptables -t nat -A PREROUTING -i eth0 -d 218.29.30.31 -p tcp --dport 2346 -j DNAT -to-destination 192.168.1.6:22
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)