原文地址:http://www.excelib.com/article/294/showspa
學生在前面已經給你們介紹過了Firewalld中direct的做用,使用他能夠直接使用iptables、ip6tables
中的規則進行配置,下面學生就給你們介紹direct的具體用法。excel
咱們仍是先從配置文件入手,direct的配置文件爲/etc/firewalld/direct.xml文件,結構以下code
1 <?xml version="1.0" encoding="utf-8"?> 2 <direct> 3 [ <chain ipv="ipv4|ipv6" table="table" chain="chain"/> ] 4 [ <rule ipv="ipv4|ipv6" table="table" chain="chain" priority="priority"> args </rule> ] 5 [ <passthrough ipv="ipv4|ipv6"> args </passthrough> ] 6 </direct>
你們能夠看到這裏的direct一共有三種節點:chain、rule和passthrough,他們都是可選的,並且均可以出現屢次。xml
ipv:這個屬性很是簡單,表示ip的版本blog
table:chain和rule節點中的table屬性就是iptables/ip6tables中的table教程
chain:chain中的chain屬性用於指定一個自定義鏈的名字,注意,不可與已有鏈重名;rule中的chain屬性既能夠是內建的(也就是iptables/ip6tables中的五條鏈),也能夠是在direct中自定義的chainip
priority:優先級,用於設置不一樣rule的優先級,就像iptables中規則的先後順序,數字越小優先級越高utf-8
args:rule和passthrough中的args就是iptables/ip6tables中的一條具體的規則,不過他們可使用咱們自定義的chain。rem
由於direct的使用跟iptables/
很是類似,換句話說,想用好direct須要有ip6tables
iptables/
的基礎,而ip6tables
iptables/
並非咱們這套教程的重點,因此這裏學生就不給你們詳細講解了。文檔ip6tables
direct中自定義chain跟iptables/
中使用-N新建chain相似,建立完以後就能夠在規則中使用-j或者-g來使用了。ip6tables
Firewalld中跟direct相關的命令以下
1 firewall-cmd [--permanent] --direct --get-all-chains 2 firewall-cmd [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table 3 firewall-cmd [--permanent] --direct --add-chain { ipv4 | ipv6 | eb } table chain 4 firewall-cmd [--permanent] --direct --remove-chain { ipv4 | ipv6 | eb } table chain 5 firewall-cmd [--permanent] --direct --query-chain { ipv4 | ipv6 | eb } table chain 6 7 firewall-cmd [--permanent] --direct --get-all-rules 8 firewall-cmd [--permanent] --direct --get-rules { ipv4 | ipv6 | eb } table chain 9 firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } table chain priority args 10 firewall-cmd [--permanent] --direct --remove-rule { ipv4 | ipv6 | eb } table chain priority args 11 firewall-cmd [--permanent] --direct --remove-rules { ipv4 | ipv6 | eb } table chain 12 firewall-cmd [--permanent] --direct --query-rule { ipv4 | ipv6 | eb } table chain priority args 13 14 firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb } args 15 firewall-cmd --permanent --direct --get-all-passthroughs 16 firewall-cmd --permanent --direct --get-passthroughs { ipv4 | ipv6 | eb } 17 firewall-cmd --permanent --direct --add-passthrough { ipv4 | ipv6 | eb } args 18 firewall-cmd --permanent --direct --remove-passthrough { ipv4 | ipv6 | eb } args 19 firewall-cmd --permanent --direct --query-passthrough { ipv4 | ipv6 | eb } args
下面咱們來看個文檔(firewalld.direct(5))中提供的例子
1 <?xml version="1.0" encoding="utf-8"?> 2 <direct> 3 <chain ipv="ipv4" table="raw" chain="blacklist"/> 4 <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="0">-s 192.168.1.0/24 -j blacklist</rule> 5 <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="1">-s 192.168.5.0/24 -j blacklist</rule> 6 <rule ipv="ipv4" table="raw" chain="blacklist" priority="0">-m limit --limit 1/min -j LOG --log-prefix "blacklisted: "</rule> 7 <rule ipv="ipv4" table="raw" chain="blacklist" priority="1">-j DROP</rule> 8 </direct>
在這個例子中首先自定義了一個叫blacklist的鏈,而後將全部來自192.168.1.0/24和192.168.5.0/24的數據包都指向了這個鏈,最後定義了這個鏈的規則:首先進行記錄,而後drop,記錄的方法是使用「blacklisted: 」前綴而且限制1分鐘記錄一次。
固然,使用類似的方法你們也能夠寫出來上一節學生給你們留下的那個問題:對ping請求進行限制。