iptables详解
iptables中基本的命令參數
iptables是一款基于命令行的防火墻策略管理工具,具有大量參數,學習難度較大。好在對于日常的防火墻策略配置來講,大家無需深入了解諸如“四表五鏈”的理論概念,只需要掌握常用的參數并做到靈活搭配即可,這就足以應對日常工作了。
iptables命令可以根據流量的源地址、目的地址、傳輸協議、服務類型等信息進行匹配,一旦匹配成功,iptables就會根據策略規則所預設的動作來處理這些流量。另外,再次提醒一下,防火墻策略規則的匹配順序是從上至下的,因此要把較為嚴格、優先級較高的策略規則放到前面,以免發生錯誤。表8-1總結歸納了常用的iptables命令參數。再次強調,我們無需死記硬背這些參數,只需借助下面的實驗來理解掌握即可。
表8-1 iptables中常用的參數以及作用
|
參數 |
作用 |
|
-P |
設置默認策略 |
|
-F |
清空規則鏈 |
|
-L |
查看規則鏈 |
|
-A |
在規則鏈的末尾加入新規則 |
|
-I num |
在規則鏈的頭部加入新規則 |
|
-D num |
刪除某一條規則 |
|
-s |
匹配來源地址IP/MASK,加嘆號“!”表示除這個IP外 |
|
-d |
匹配目標地址 |
|
-i網卡名稱 |
匹配從這塊網卡流入的數據 |
|
-o網卡名稱 |
匹配從這塊網卡流出的數據 |
|
-p |
匹配協議,如TCP、UDP、ICMP |
|
--dport num |
匹配目標端口號 |
|
--sport num |
匹配來源端口號 |
在iptables命令后添加-L參數查看已有的防火墻規則鏈:
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
ACCEPTall--anywhereanywherectstateRELATED,ESTABLISHED
ACCEPTall--anywhereanywhere
INPUT_directall--anywhereanywhere
INPUT_ZONES_SOURCEall--anywhereanywhere
INPUT_ZONESall--anywhereanywhere
ACCEPTicmp--anywhereanywhere
REJECTall--anywhereanywherereject-withicmp-host-prohibited
………………省略部分輸出信息………………
在iptables命令后添加-F參數清空已有的防火墻規則鏈:
[root@xiejun~]#iptables-F
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
ChainFORWARD(policyACCEPT)
targetprotoptsourcedestination
ChainOUTPUT(policyACCEPT)
targetprotoptsourcedestination
………………省略部分輸出信息………………
把INPUT規則鏈的默認策略設置為拒絕:
[root@xiejun~]#iptables-PINPUTDROP
[root@xiejun~]#iptables-L
ChainINPUT(policyDROP)
targetprotoptsourcedestination
…………省略部分輸出信息………………
前文提到,防火墻策略規則的設置有兩種:通和堵。當把INPUT鏈設置為默認拒絕后,就要在防火墻策略中寫入允許策略了,否則所有到來的流量都會被拒絕掉。另外,需要注意的是,規則鏈的默認拒絕動作只能是DROP,而不能是REJECT。
向INPUT鏈中添加允許ICMP流量進入的策略規則:
在日常運維工作中,經常會使用ping命令來檢查對方主機是否在線,而向防火墻的INPUT規則鏈中添加一條允許ICMP流量進入的策略規則就默認允許了這種ping命令檢測行為。
[root@xiejun~]#iptables-IINPUT-picmp-jACCEPT
[root@xiejun~]#ping-c4192.168.10.10
PING192.168.10.10(192.168.10.10)56(84)bytesofdata.
64bytesfrom192.168.10.10:icmp_seq=1ttl=64time=0.156ms
64bytesfrom192.168.10.10:icmp_seq=2ttl=64time=0.117ms
64bytesfrom192.168.10.10:icmp_seq=3ttl=64time=0.099ms
64bytesfrom192.168.10.10:icmp_seq=4ttl=64time=0.090ms
---192.168.10.10pingstatistics---
4packetstransmitted,4received,0%packetloss,time2999ms
rttmin/avg/max/mdev=0.090/0.115/0.156/0.027ms
刪除INPUT規則鏈中剛剛加入的那條策略(允許ICMP流量),并把默認策略設置為允許:
[root@xiejun~]#iptables-DINPUT1
[root@xiejun~]#iptables-PINPUTACCEPT
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
………………省略部分輸出信息………………
將INPUT規則鏈設置為只允許指定網段的主機訪問本機的22端口,拒絕來自其他所有主機的流量:
[root@xiejun~]#iptables-IINPUT-s192.168.10.0/24-ptcp--dport22-j
ACCEPT
[root@xiejun~]#iptables-AINPUT-ptcp--dport22-jREJECT
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
ACCEPTtcp--192.168.10.0/24anywheretcpdpt:ssh
REJECTtcp--anywhereanywheretcpdpt:sshreject-withicmp-port-unreachable
………………省略部分輸出信息………………
再次重申,防火墻策略規則是按照從上到下的順序匹配的,因此一定要把允許動作放到拒絕動作前面,否則所有的流量就將被拒絕掉,從而導致任何主機都無法訪問我們的服務。另外,這里提到的22號端口是ssh服務使用的(有關ssh服務,請見下一章),劉遄老師先在這里挖坑,等大家學完第9章后可再驗證這個實驗的效果。
在設置完上述INPUT規則鏈之后,我們使用IP地址在192.168.10.0/24網段內的主機訪問服務器(即前面提到的設置了INPUT規則鏈的主機)的22端口,效果如下:
[root@ClientA~]#ssh192.168.10.10
Theauthenticityofhost'192.168.10.10(192.168.10.10)'can'tbeestablished.
ECDSAkeyfingerprintis70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c.
Areyousureyouwanttocontinueconnecting(yes/no)?yes
Warning:Permanentlyadded'192.168.10.10'(ECDSA)tothelistofknownhosts.
root@192.168.10.10'spassword:此處輸入對方主機的root管理員密碼
Lastlogin:SunFeb1201:50:252017
[root@ClientA~]#
然后,我們再使用IP地址在192.168.20.0/24網段外的主機訪問服務器的22端口,效果如下,就不會提示連接請求被拒絕了(Connectionfailed):
[root@ClientB~]#ssh192.168.10.10
Connectingto192.168.10.10:22...
Couldnotconnectto'192.168.10.10'(port22):Connectionfailed.
向INPUT規則鏈中添加拒絕所有人訪問本機12345端口的策略規則:
[root@xiejun~]#iptables-IINPUT-ptcp--dport12345-jREJECT
[root@xiejun~]#iptables-IINPUT-pudp--dport12345-jREJECT
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
REJECTudp--anywhereanywhereudpdpt:italkreject-withicmp-port-unreachable
REJECTtcp--anywhereanywheretcpdpt:italkreject-withicmp-port-unreachable
ACCEPTtcp--192.168.10.0/24anywheretcpdpt:ssh
REJECTtcp--anywhereanywheretcpdpt:sshreject-withicmp-port-unreachable
………………省略部分輸出信息………………
向INPUT規則鏈中添加拒絕192.168.10.5主機訪問本機80端口(Web服務)的策略規則:
[root@xiejun~]#iptables-IINPUT-ptcp-s192.168.10.5--dport80-jREJECT
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
REJECTtcp--192.168.10.5anywheretcpdpt:httpreject-withicmp-port-unreachable
REJECTudp--anywhereanywhereudpdpt:italkreject-withicmp-port-unreachable
REJECTtcp--anywhereanywheretcpdpt:italkreject-withicmp-port-unreachable
ACCEPTtcp--192.168.10.0/24anywheretcpdpt:ssh
REJECTtcp--anywhereanywheretcpdpt:sshreject-withicmp-port-unreachable
………………省略部分輸出信息………………
向INPUT規則鏈中添加拒絕所有主機訪問本機1000~1024端口的策略規則:
[root@xiejun~]#iptables-AINPUT-ptcp--dport1000:1024-jREJECT
[root@xiejun~]#iptables-AINPUT-pudp--dport1000:1024-jREJECT
[root@xiejun~]#iptables-L
ChainINPUT(policyACCEPT)
targetprotoptsourcedestination
REJECTtcp--192.168.10.5anywheretcpdpt:httpreject-withicmp-port-unreachable
REJECTudp--anywhereanywhereudpdpt:italkreject-withicmp-port-unreachable
REJECTtcp--anywhereanywheretcpdpt:italkreject-withicmp-port-unreachable
ACCEPTtcp--192.168.10.0/24anywheretcpdpt:ssh
REJECTtcp--anywhereanywheretcpdpt:sshreject-withicmp-port-unreachable
REJECTtcp--anywhereanywheretcpdpts:cadlock2:1024reject-withicmp-port-
unreachable
REJECTudp--anywhereanywhereudpdpts:cadlock2:1024reject-withicmp-port-
unreachable
………………省略部分輸出信息………………
有關iptables命令的知識講解到此就結束了,大家是不是意猶未盡?考慮到Linux防火墻的發展趨勢,大家只要能把上面的實例吸收消化,就可以完全搞定日常的iptables配置工作了。但是請特別注意,使用iptables命令配置的防火墻規則默認會在系統下一次重啟時失效,如果想讓配置的防火墻策略永久生效,還要執行保存命令:
[root@xiejun~]#serviceiptablessave
iptables:Savingfirewallrulesto/etc/sysconfig/iptables:[OK]
總結
以上是生活随笔為你收集整理的iptables详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】W32Dasm反汇编工具使用详解
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?