iptables學習[08]:iptables自定義鏈的使用
阿新 • • 發佈:2021-08-30
簡介
- 當iptables規則特別多時,我們需要具備模組化思想,需要將不同目的iptables規則進行歸類。
- 將同一型別的iptables規則作為一個模組(web類,mysql類....),使其更加具有條理性,清爽整潔。
- 自定義鏈是除了iptables自帶的5個鏈外,由我們自己定義的新鏈。
- 在自定義鏈中定義的規則不會生效,他需要被內建鏈引用才可以生效。
iptables新增自定義鏈的方法
- -N:new, 自定義一條新的規則鏈 。
- -E:重新命名自定義鏈;引用計數不為0的自定義鏈不能夠被重新命名,也不能被刪除 。
- -X:delete,刪除自定義的空的規則鏈 。
- -P:Policy,設定預設策略;對filter表中的鏈而言,其預設策略有:ACCEPT:接受, DROP:丟棄。
範例一:在filter表上新增一條新的鏈。
iptables -t filter -N chain_name
範例二:重新命名這條鏈。
iptables -t filter -T chain_NAME CHAIN_NAME
範例三:刪除這條鏈。
iptables -t filter -X CHAIN_NAME
iptables使用自定義鏈的方法
步驟一:建立自定義鏈。
iptables -t filter -N WEB_CHAIN
步驟二:檢視建立好的自定義鏈WEB_CHAIN
。
iptables -vnL ....... ....... Chain WEB_CHAIN (0 references) pkts bytes target prot opt in out source destination
步驟三:在自定義鏈中新增規則。
iptables -A WEB_CHAIN -s 192.168.10.10 -p tcp --dport 80 -j REJECT
iptables -A WEB_CHAIN -s 192.168.10.10 -p tcp --dport 8080 -j REJECT
步驟四:檢視自定義鏈(WEB_CHAIN)中定義的規則。
iptables -vnL WEB_CHAIN Chain WEB_CHAIN (0 references) pkts bytes target prot opt in out source destination 0 0 REJECT tcp -- * * 192.168.10.10 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable 0 0 REJECT tcp -- * * 192.168.10.10 0.0.0.0/0 tcp dpt:8080 reject-with icmp-port-unreachable
步驟五:將自定義好的鏈關聯至內建鏈上。
iptables -AINPUT -j WEB_CHAIN
步驟六:關聯後檢視。
iptables -vnL
Chain INPUT (policy ACCEPT 31 packets, 2572 bytes)
pkts bytes target prot opt in out source destination
31 2572 WEB_CHAIN all -- * * 0.0.0.0/0 0.0.0.0/0
......
......
Chain WEB_CHAIN (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 192.168.10.10 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable
0 0 REJECT tcp -- * * 192.168.10.10 0.0.0.0/0 tcp dpt:8080 reject-with icmp-port-unreachable