iptables
定義:iptables並不是只防火牆,而是一種客戶端工具。用戶通過這個客戶端(iptables)將用戶的安全設定執行到對應的“安全框架”中。這個安全框架纔是真正的防火牆。框架名字叫netfilter。
就是通過iptables來操作netfilter。
規則(Rules)
- 規則就是網絡管理員預定義的條件。規則一般的定義爲“如果數據報頭符合這樣的條件,就這樣處理這個數據包“”。
- 規則存儲在內核空間的信息包過濾表中。
- 規則分別指定了源地址,目的地址,傳輸協議(TCP,UDP,ICMP),和服務類型(HTTP,FTP,SMTP)。
- 當數據包與規則匹配時,Iptables就根據規則所以定義的方法來處理這些數據包,如,放行(accept),拒絕(reject),丟棄(drop) 等。
- 配置防火牆的主要工作就是添加,修改和刪除這些規則。
關鍵:
- Rules包括一個條件和一個目標(target)
- 如果滿足條件,就執行目標(target)中的規則或者特定值。
- 如果不滿足條件,就判斷下一條Rules。
目標值(Target Values),動作
下面是你可以在target裏指定的特殊值:
- ACCEPT – 允許防火牆接收數據包
- DROP – 防火牆丟棄包
- QUEUE – 防火牆將數據包移交到用戶空間
- RETURN – 防火牆停止執行當前鏈中的後續Rules,並返回到調用鏈(the calling chain)中。
四表五鏈
五鏈
鏈(chain)
- 當客戶端訪問服務器的Web服務時,客戶端發送報文到網卡。信息會通過內核的TCP協議傳輸到用戶空間的Web服務中。
- 當Web服務需要響應客戶端請求時,Web服務發出的響應報文的目標終點則爲客戶端。Web服務所監聽的ip與端口變成了原點。
- 防火牆要達到目的就需要在內核中設置關卡。所有進出的報文都需要通過這個關卡,符合條件放行不符合則被阻止。就出現了input關卡和output關卡,這些關卡又被稱爲“鏈”。
五鏈分別是
- PREOUTING:路由前
- INPUT
- POSTROUTING :路由後
- OUTPU
- FORWARD:轉發
- 數據包經過一個鏈時,會將鏈上的所有規則匹配一邊,匹配時按照順序,一條一條匹配。
四表
四表
- 把具有相同功能的規則集合叫做“表”
iptables具有Filter, NAT, Mangle, Raw四種內建表:
Filter表 | 過濾功能 |
---|---|
NAT表 | 網絡地址轉換功能 |
Mangle表 | 拆解報文,做出修改,並重新封裝的功能 |
Raw表 | 關閉net表上啓用的鏈接追蹤機制,不再讓iptables對數據包進行跟蹤,提高性能 |
表與鏈的關係
鏈 | 表 |
---|---|
PREOUTING | raw,mangle,nat |
INPUT | mangle,filter |
POSTROUTING | mangle,nat |
OUTPU | raw,mangle,nat,filter |
FORWARD | mangle,filter |
- 四張表處於同一條鏈時,執行優先級
- raw–>mangle–>nat–>filter
表 | 鏈 |
---|---|
filter | INPUT鏈 ,OUTPUT鏈,FORWARD鏈 |
NAT | PREROUTING鏈,POSTROUTING鏈,OUTPUT鏈\ |
Mangle | PREROUTING,OUTPUT,FORWARD,INPUT,POSTROUTING |
Raw | PREROUTING ,OUTPUT , |
iptables基本用法
iptables的規則:根據指定的匹配條件來嘗試匹配每個流經此處的報文。一旦匹配成功,則由規則後面指定的處理動作進行處理。
匹配條件
基本匹配條件 | 擴展匹配條件 |
---|---|
源地址Source IP | 源端口Source Port |
目標地址Destintion ip | 目標端口Destination Port |
處理動作
動作 | 含義 |
---|---|
ACCEPT | 接收數據包,允許數據包通過 |
DROP | 丟棄數據包,不給出任何信息。 |
REJECT | 拒絕數據通過,必要時發出拒接信息 |
SNAT | 源地址轉換, 解決內網用戶用同一個公網上網的問題。 |
DNAT | 目標地址轉換。 |
REDIRECT | 重定向、本機端口映射、透明代理。 |
MASQUERADE | IP僞裝(NAT),用於動態,臨時會變的ip |
LOG | 日誌記錄,在/var/log/messages文件中記錄。 |
常見選項
常用操作命令 | 說明 |
---|---|
-A | 在指定鏈尾部添加規則 |
-D | 刪除匹配的規則 |
-R | 替換匹配的規則 |
-I | 在指定位置插入規則(例:iptables -I INPUT 1 --dport 80 -j ACCEPT(將規則插入到filter表INPUT鏈中的第一位上) |
-L/S | 列出指定鏈或者所有鏈的規則 |
-F | 刪除指定鏈或所有鏈的規則 |
-N | 創建用戶自定義鏈[例:iptables -N allowed] |
-X | 刪除指定的用戶自定義鏈 |
-P | 爲指定鏈設置默認規則策略,對自定義鏈不起作用 |
-Z | 將指定鏈或所有鏈的計數器清零 |
-E | 更改自定義鏈的名稱[例:iptables -E allowed disallowed] |
-n | ip地址和端口號以數字方式顯示[例:iptables -nL] |
-v | 列出詳細信息 |
–line-number | 最左側顯示規則序號 |
-t | 指定要操作的表,不指定默認filter |
常用規則匹配 | 說明 |
---|---|
-p tcp/udp/icmp/all | 匹配協議,all會匹配所有協議 |
-s addr[/mask] | 匹配源地址 |
-d addr[/mask] | 匹配目標地址 |
–sport port1[:port2] | 匹配源端口(可指定連續的端口) |
–dport port1[:port2] | 匹配目的端口(可指定連續的端口) |
-o interface | 匹配出口網卡,只適用FORWARD、POSTROUTING、OUTPUT(例:iptables -A FORWARD -o eth0) |
-i interface | 匹配入口網卡,只使用PREROUTING、INPUT、FORWARD。 |
–icmp-type | 匹配icmp類型(使用iptables -p icmp -h可查看可用的ICMP類型) |
–tcp-flags mask comp | 匹配TCP標記,mask表示檢查範圍,comp表示匹配mask中的哪些標記。(例:iptables -A FORWARD -p tcp --tcp-flags |
ALL SYN,ACK -j ACCEPT | 表示匹配SYN和ACK標記的數據包) |
iptables下各項名詞解釋
[root@xiaoagiao ~]# iptables -t filter -L -v #-t指定要操作的表,不加默認filter
#-L列出指定鏈或所有鏈的規則
#-v顯示詳情
Chain INPUT (policy ACCEPT 90 packets, 5544 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 52 packets, 4224 bytes)
pkts bytes target prot opt in out source destination
獲得信息解釋:
- pkts:對應規則匹配到的報文個數。
- bytes:對應規則匹配到的報文大小總和。
- target :前面提到的target的特殊值,動作,規則匹配成功後要採取的措施。
- prot – 協議:tcp, udp, icmp等
- opt:規則對應的選項。
- in:數據包由哪個接口流入,(可以設置通過哪塊網卡流入的報文,需要匹配當前規則)
- out:數據包由哪個接口流出,(可以設置通過哪塊網卡流出的報文,需要匹配當前規則)
- source – 數據包的源IP地址,或網段
- destination – 數據包的目標IP地址,或網段
iptables規則配置
增加規則
- 拒絕所有來自192.168.126.132 的報文
[root@fei ~]# ping 192.168.126.128 #從另一臺機器(192.168.126.132)ping192.168.126.128,可以通
PING 192.168.126.128 (192.168.126.128) 56(84) bytes of data.
64 bytes from 192.168.126.128: icmp_seq=1 ttl=64 time=7.36 ms
64 bytes from 192.168.126.128: icmp_seq=2 ttl=64 time=0.172 ms
64 bytes from 192.168.126.128: icmp_seq=3 ttl=64 time=0.364 ms
64 bytes from 192.168.126.128: icmp_seq=4 ttl=64 time=0.407 ms
[root@xiaoagiao ~]# iptables -t filter -I INPUT -s 192.168.126.132 -j DROP
#-t filter :指定表filter
#-I INPUT:指定鏈INPUT
#-s:指定匹配的源地址
#-j:指定匹配動作 DROP
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.126.132 0.0.0.0/0 #才INPYT鏈上增加規則
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@fei ~]# ping 192.168.126.128
PING 192.168.126.128 (192.168.126.128) 56(84) bytes of data.
^C
#現在ping就不通了
- 允許所有來自192.168.126.132 的報文
[root@xiaoagiao ~]# iptables -t filter -I INPUT -s 192.168.126.132 -j ACCEPT
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 192.168.126.132 0.0.0.0/0
DROP all -- 192.168.126.132 0.0.0.0/0
# 現在用另一臺機器ping 也是可以ping 通的。
刪除規則
- 方法1.iptables -t filter -D INPUT 1
- 方法2.iptables -D INPUT -s 192.168.126.132 -j DROP
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 192.168.126.132 0.0.0.0/0
DROP all -- 192.168.126.132 0.0.0.0/0
[root@xiaoagiao ~]# iptables -t filter -D INPUT 1 #刪除第一條
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.126.132 0.0.0.0/0
[root@xiaoagiao ~]# iptables -D INPUT -s 192.168.126.132 -j DROP #指定一個刪除
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
修改規則
[root@xiaoagiao ~]# iptables -t filter -I INPUT -s 192.168.126.132 -j ACCEPT
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 192.168.126.132 0.0.0.0/0
[root@xiaoagiao ~]# iptables -t filter -R INPUT 1 -s 192.168.126.132 -j DROP # -R替換匹配的規則,需要在規則鏈名後加上規則編號。
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.126.132 0.0.0.0/0
- 修改默認動作
iptables -t filter -P INPUT DROP
# -P :爲指定鏈設置默認規則策略,對自定義鏈不起作用
保存規則
- service iptables save
- 在centos7上需要安裝iptables-services,因爲centos7上默認使用firewall
[root@xiaoagiao ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@xiaoagiao ~]# cat /etc/sysconfig/iptables #保存後文件存在此處
# Generated by iptables-save v1.4.21 on Sun Apr 26 21:05:20 2020
*filter
:INPUT ACCEPT [74:4760]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [49:3500]
-A INPUT -s 192.168.126.132/32 -j DROP #已經保存的規則
COMMIT
# Completed on Sun Apr 26 21:05:20 2020
- 或者使用iptables-save > /etc/sysconfig/iptables
使用iptables-save 並不能保存規則,但可以將已經寫好的規則輸出來,用重定向輸出到指定文件。
基本配置語法總結
匹配條件
-s | 匹配報文的源地址可以同時指定多個原地址。每個ip之間用逗號隔開,也可以指定爲一個網段。 |
---|---|
-d | 匹配報文的目標地址可以同時匹配多個目標地址。每個ip之間用逗號隔開,也可以指定爲一個網段。 |
-p | 匹配報文的協議類型,可以匹配的協議類型有(tcp,udp,udplite,icmp,esp,ah,sctp) |
-i | 匹配報文流入本機的網卡接口。由於匹配條件是網卡。所以在Output鏈和postrouting鏈中不能使用此選項。 |
-o | 匹配流出網卡。INPUT,PREROUTING中不可用 |
舉例:
- -s
- iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT :允許本地迴環接口(即運行本機訪問本機)
- -d
- iptables -A INPUT -d 192.168.12.11,192.168.2.23 -d 192.168.1.1 -j ACCEPT
- -p
- iptables -A INPUT -p tcp --dport 22 -j ACCEPT :允許訪問22端口
- iptables -A INPUT -p tcp --dport 21 -j ACCEPT :允許ftp服務的21端口
- -i
- iptables -A INPUT -p icmp -i ens32 -j DROP
- iptables -A INPUT -p icmp ! -i ens32 -j DROP
- -o
- iptables -A INPUT -p icmp -o ens32 -j DROP
擴展匹配條件
- 隱式擴展:在使用選項-p指明瞭特定的協議時,無需再同時使用選項-m指明擴展模塊的擴展機制。
- 顯式擴展:必須使用-m選項指明要調用的擴展模塊的擴展機制。
1. tcp擴展模塊:
- -p tcp -m tcp --sport:用於匹配協議報文的源端口,可以使用冒號指定一個連續的端口範圍。
- -p tcp -m tcp --dport:用於匹配協議報文的目標端口,可以使用冒號指定一個連續的端口範圍
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m tcp --sport 22 -j REJECT
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m tcp --sport 22:24 -j REJECT
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m tcp ! --dport 22:24 -j ACCEPT
2. multiport擴展模塊
- -p tcp -m multiport --sports:用於匹配報文的源端口,可以指定離散的多個端口號,端口之間用逗號隔開。
- -p tcp -m multiport --dports:用於匹配報文的目標端口,可以指定離散的多個端口號,端口之間用逗號隔開。
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m multiport --sports 122,124 -j REJECT
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m multiport --dports 12,45 -j REJECT
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m multiport ! --dports 12,45:46 -j REJECT
#!:取反
iptables擴展模塊
iprange擴展模塊
- 可以指定一段連續的ip地址範圍,用於匹配報文的源地址和目標地址。
- -src-range: 源地址範圍
- -dst-range: 目標地址範圍
[root@xiaoagiao ~]# iptables -t filter -I INPUT -m iprange --src-rang192.168.1.12-192.168.1.30 -j DROP
[root@xiaoagiao ~]# iptables -nvL
Chain INPUT (policy ACCEPT 68 packets, 3980 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.1.12-192.168.1.30
string擴展模塊
- 可以指定要匹配的字符串,如果報文中包含對應的字符串,則符合匹配條件。
-m string:表示string擴展模塊
--algo:用於指定匹配數算法,可選的算法有bm和kmp,此選項爲必需選項。
-string:用於指定匹配的字符串。
[root@xiaoagiao ~]# iptables -t filter -I INPUT -m string --algo kmp --string "bit" -j ACCEPT
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 STRING match "bit" ALGO name kmp TO 65535
DROP all -- 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.1.12-192.168.1.30
time擴展模塊
- 根據時間段匹配報文,報文到達的時間在指定時間範圍內,則符合匹配條件。
-m time:使用time模塊。
-timestart:選項用於指定起始時間。00:00:00格式。
-timestop:選項用於指定結束時間。
-weekdays:用於指定星期幾,可以用數字還能用縮寫表示。Mon,Tue,Wed,Thu,Fri,Sat,Sun
-monthday,-datestart:用於指定日期範圍
[root@xiaoagiao ~]# iptables -t filter -I OUTPUT -p TCP --dport 80 -m time --timestart 08:55:00 --timestop 17:00:00 -j REJECT
[root@xiaoagiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 TIME from 08:55:00 to 17:00:00 UTC reject-with icmp-port-unreachable
connlimit 擴展模塊
- 限制每個ip地址同時連接到的server端的鏈接數量。
- 不用指定ip,默認就是針對每個客戶端ip。即對單ip的併發連接數限制,一般和協議端口配合使用。
--connlimit-above:限制連接上限
--connlimit-mask:限制連接下限
[root@xiao ~]# iptables -t filter -I OUTPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 --connlimit-mask 24 -j REJECT
[root@xiao ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 TIME from 08:55:00 to 17:00:00 UTC reject-with icmp-port-unrea chable
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 TIME from 08:55:00 to 17:00:00 UTC reject-with icmp-port-unrea chable
limit擴展模塊
- 對報文到達速率進行限制,可以以秒,分鐘,小時,天爲單位進行限制,(就是單位時間內流入的數據包的數量)。
[root@xiaoagiao ~]# iptables -t filter -I INPUT -m limit --limit 10/min -j REJECT
#每六秒鐘。有一個包不能流入,並且默認開始可以容納五個包不能流入。
[root@xiaoagiao ~]# iptables -nvL
Chain INPUT (policy ACCEPT 32 packets, 1857 bytes)
pkts bytes target prot opt in out source destination
17 1160 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 10/min burst 5
335 32564 REJECT icmp -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
[root@xiaoagiao ~]# ping 192.168.126.128 #用另一臺機器ping
PING 192.168.126.128 (192.168.126.128) 56(84) bytes of data.
From 192.168.126.128 icmp_seq=1 Destination Port Unreachable
From 192.168.126.128 icmp_seq=2 Destination Port Unreachable
From 192.168.126.128 icmp_seq=3 Destination Port Unreachable
From 192.168.126.128 icmp_seq=4 Destination Port Unreachable
From 192.168.126.128 icmp_seq=5 Destination Port Unreachable
64 bytes from 192.168.126.128: icmp_seq=6 ttl=64 time=0.378 ms
From 192.168.126.128 icmp_seq=7 Destination Port Unreachable
64 bytes from 192.168.126.128: icmp_seq=8 ttl=64 time=0.390 ms
64 bytes from 192.168.126.128: icmp_seq=9 ttl=64 time=0.373 ms
64 bytes from 192.168.126.128: icmp_seq=10 ttl=64 time=0.311 ms
64 bytes from 192.168.126.128: icmp_seq=11 ttl=64 time=0.326 ms
64 bytes from 192.168.126.128: icmp_seq=12 ttl=64 time=0.446 ms
From 192.168.126.128 icmp_seq=13 Destination Port Unreachable
64 bytes from 192.168.126.128: icmp_seq=14 ttl=64 time=0.380 ms
64 bytes from 192.168.126.128: icmp_seq=15 ttl=64 time=0.356 ms
64 bytes from 192.168.126.128: icmp_seq=16 ttl=64 time=0.416 ms
64 bytes from 192.168.126.128: icmp_seq=17 ttl=64 time=0.591 ms
64 bytes from 192.168.126.128: icmp_seq=18 ttl=64 time=0.472 ms
From 192.168.126.128 icmp_seq=19 Destination Port Unreachable
64 bytes from 192.168.126.128: icmp_seq=20 ttl=64 time=0.418 ms
64 bytes from 192.168.126.128: icmp_seq=21 ttl=64 time=0.430 ms
^C
--- 192.168.126.128 ping statistics ---
21 packets transmitted, 13 received, +8 errors, 38% packet loss, time 20252ms
rtt min/avg/max/mdev = 0.311/0.406/0.591/0.072 ms
tcp-flags模塊
state擴展
- 可以基於連接追蹤功能去查看每一個報文當前所處的狀態。
無論什麼協議,客戶端第一次訪問時服務器會去內核內存中的追蹤表查看他之前是否來過,查不到就證明是第一次來,記錄入追蹤表,如果查到以前來過就不檢測規則直接允許訪問。這稱爲連接追蹤機制。比如負載均衡服務器,不建議開啓,追蹤表最大隻能記錄6萬多的條目。訪問數超過就會無法記錄出錯,導致所有的連接失敗。 - 報文狀態
- NEW:第一次連接
- ESTABLISHED:已建立連接
- INVALID:無法識別的連接
- RELATED:相關聯連接,當前連接是一個新請求,但附屬於已存在的連接
- UNTRACKED:row表上關閉連接追蹤功能
[root@xiaoagiao ~]# iptables -t filter -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@xiaoagiao ~]# iptables -t filter -A INPUT -j REJECT
[root@xiaoagiao ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
110 6452 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
#原有連接不受影響
#新建連接被拒絕
網絡防火牆
實例:iptables之forward轉發
- 實現外部網絡主機與內部網絡主機通訊
當外部網絡中的主機與網絡內部主機通訊時,不管是由外部主機向內部主機,還是由內向外發送報文。都需要經過防火牆所在的主機,其作用是進行過濾轉發。
實驗環境:
分類 | 外網主機 | 防火牆主機 | 內網主機 |
---|---|---|---|
ip | ens32 192.168.126.131 | ens33,192.168.126.128 ens32,192.168.248.128 | eth0,192.168.248.129 |
GATEWAY | 192.168.126.2 | 192.168.126.2,192.168.248.2 | 192.168.244.128 |
實驗環境:
三臺機器分別充當外網,防火牆,內網。
外網和內網由於不在同一網段,所以相互不能通訊。
設置:
- 外網和防火牆的ens33網卡設置爲nat模式,ens32和內網設置爲僅主機模式,用於區分網段。
實驗開始
- 測試內網和外網是否相同。
[root@xiaoagiao ~]# ping 192.168.248.129 #外網ping內網並不能ping通。
PING 192.168.248.129 (192.168.248.129) 56(84) bytes of data.
^C
--- 192.168.248.129 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3008ms
#不光內網,防火牆的ens32也ping不通
[root@xiaoagiao ~]# ping 192.168.248.128
PING 192.168.248.128 (192.168.248.128) 56(84) bytes of data.
^C
--- 192.168.248.128 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4040ms
# 只能ping通防火牆ens33
[root@xiaoagiao ~]# ping 192.168.126.128
PING 192.168.126.128 (192.168.126.128) 56(84) bytes of data.
64 bytes from 192.168.126.128: icmp_seq=1 ttl=64 time=0.168 ms
64 bytes from 192.168.126.128: icmp_seq=2 ttl=64 time=0.429 ms
用內網ping外網不同,ping防火牆的ens32可以ping通(因爲在同一網段)
實驗中,在設置內網時,要將虛擬機中,虛擬網卡編輯器中的僅主機模式下的‘將主機虛擬適配器連接到此網絡去掉勾選’
- 在外網中新增一條路由,讓訪問內網(192.168.248.0/24)都發送給ens33上
[root@xiaoagiao ~]# route add -net 192.168.248.0/24 gw 192.168.126.128
[root@xiaoagiao ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.126.2 0.0.0.0 UG 100 0 0 ens32
192.168.126.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32
192.168.248.0 192.168.126.128 255.255.255.0 UG 0 0 0 ens32
- 此時發現外網可以ping通防火牆的兩塊網卡了,但是還是ping不通內網
[root@xiaoagiao ~]# ping 192.168.126.128 #ping防火牆ens33,通
PING 192.168.126.128 (192.168.126.128) 56(84) bytes of data.
64 bytes from 192.168.126.128: icmp_seq=1 ttl=64 time=0.251 ms
64 bytes from 192.168.126.128: icmp_seq=2 ttl=64 time=0.499 ms
64 bytes from 192.168.126.128: icmp_seq=3 ttl=64 time=0.403 ms
^C
--- 192.168.126.128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2012ms
rtt min/avg/max/mdev = 0.251/0.384/0.499/0.103 ms
[root@xiaoagiao ~]# ping 192.168.248.128 #ping防火牆ens32,通
PING 192.168.248.128 (192.168.248.128) 56(84) bytes of data.
64 bytes from 192.168.248.128: icmp_seq=1 ttl=64 time=0.290 ms
64 bytes from 192.168.248.128: icmp_seq=2 ttl=64 time=0.431 ms
^C
--- 192.168.248.128 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1009ms
rtt min/avg/max/mdev = 0.290/0.360/0.431/0.073 ms
[root@xiaoagiao ~]# ping 192.168.248.129 #ping防火牆ens33,不通
PING 192.168.248.129 (192.168.248.129) 56(84) bytes of data.
^C
--- 192.168.248.129 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1013ms
爲什麼會這樣:
當用外網ping 192.168.248.128時,防火牆收到這個報文,發現這個地址是自己的,所以給了外網迴應。(這就是新增路由的作用,192.168.248.128把報文給了192.168.126.128)
當用外網ping192.168.248.129時,防火牆收到這個報文,發現這個地址不是自己的,所以不給外網迴應。
- 此時:就應該將防火牆的轉發功能打開,讓防火牆的ens32將報文轉發給內網。
- 打開防火牆轉發功能。
- 臨時修改
[root@xiaoagiao ~]# cat /proc/sys/net/ipv4/ip_forward
0 #0表示關閉
[root@xiaoagiao ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@xiaoagiao ~]# cat /proc/sys/net/ipv4/ip_forward
1 #將0改爲1
- 永久次改
#centos7中
/usr/lib/sysctl.d/00-system.conf 中加入 net.ipv4.ip_forward=1
#centos6中
/etc/sysctl.conf 中修改 net.ipv4.ip_forward=1
- 將內網的網關設置爲防火牆ens32的網址,實現相互通訊
- 完成之後重啓網卡
- 現在使用外網來ping內網
[root@xiaoagiao ~]# ping 192.168.248.129
PING 192.168.248.129 (192.168.248.129) 56(84) bytes of data.
64 bytes from 192.168.248.129: icmp_seq=1 ttl=63 time=0.541 ms
64 bytes from 192.168.248.129: icmp_seq=2 ttl=63 time=262 ms
64 bytes from 192.168.248.129: icmp_seq=3 ttl=63 time=0.429 ms
^C
--- 192.168.248.129 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2024ms
rtt min/avg/max/mdev = 0.429/87.757/262.303/123.422 ms
可以ping通 ,實現了防火牆網絡的轉發。
整個報文流向:
- 外網將報文發給內網,防火牆對該網段進行攔截
- 通過路由轉發,防火牆發現對方請求的是自己的ip,實行放行,但此時數據不能到達內網。
- 防火牆開啓轉發功能,將報文轉發給內網。