天天看點

iptables指令使用基礎(02)

一.iptables指令

    規則:根據指定的比對條件來嘗試比對每個流經此處的封包,一旦比對成功,則由規則後面指定的處理動作進行處理;

        比對條件:分為基本比對條件和擴充比對條件,擴充比對條件又分為隐式擴充和顯式擴充;

            基本比對條件:源位址,目标位址,傳輸協定;

            擴充比對條件:需要借助于擴充子產品進行指定的比對條件;

                隐式擴充:已經在基本比對條件中指明的協定相關的擴充;

                顯式擴充:隐式擴充之外的其它擴充比對條件;

        處理動作:分為基本動作和擴充動作;

            基本動作:ACCEPT,DROP,REJECT

            擴充動作:需要借助于擴充子產品進行,但無須顯式指定,僅需指明動作;

二.添加規則需要思考的問題

    (1) 封包流經的位置:用于判斷将規則添加至哪個鍊;

    (2) 實作的功能:用于判斷将規則添加至哪個表;

    (3) 封包的方向:用于判斷哪個為“源”,哪個為“目标”;

    (4) 比對條件:用于編寫能夠正确比對目标封包的規則;

三.iptables指令使用格式

        iples [-t table] {-A|-C|-D} chain rule-specification

       ip6tables [-t table] {-A|-C|-D} chain rule-specification

       iptables [-t table] -I chain [rulenum] rule-specification

       iptables [-t table] -R chain rulenum rule-specification

       iptables [-t table] -D chain rulenum

       iptables [-t table] -S [chain [rulenum]]

       iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

       iptables [-t table] -N chain

       iptables [-t table] -X [chain]

       iptables [-t table] -P chain target

       iptables [-t table] -E old-chain-name new-chain-name

       rule-specification = [matches...] [target]

       match = -m matchname [per-match-options]

       target = -j targetname [per-target-options]tab

    規則管理格式:iptables [-t table] COMMAND chain creteria [-m -m matchname[per-match-options]]  [-j targetname[per-target-options]]

    -t table:指明要管理的表;預設為filter;

    COMMANDS:

        鍊管理:

<code>     </code><code>-P:iptables [-t table] -P chain target,定義鍊的預設政策;其target一般可使用ACCEPT或DROP;</code>

<code>     </code> 

<code>     </code><code>-N:iptables [-t table] -N chain,自定義規則鍊;僅在預設鍊通過某規則進行調用方可生效;是以,每個自定義鍊都有其引用記數;</code>

<code>     </code><code>-X:iptables [-t table] -X [chain],删除自定義并且是空的引用計數為0的鍊;</code>

<code>     </code><code>-F:iptables [-t table] -F [chain [rulenum]] [options...],清空指定的鍊,或删除指定鍊上的規則;</code>

<code>     </code><code>-E:iptables [-t table] -E old-chain-name new-chain-name,重命名自定義的引用計數為0的鍊;</code>

<code>     </code><code>-Z:iptables [-t table] -Z  [chain[rulenum]] [options...],将規則計數器置0;</code>

        規則:      

<code>     </code><code>-A:append, iptables [-t table] -A chain rule-specification,追加規則到指定的鍊尾部;</code>

<code>     </code><code>-I:insert, iptables [-t table] -I chain [rulenum] rule-specification,插入規則到指定的鍊中的指定位置,預設為鍊首;</code>

<code>     </code><code>-D:delete,iptables [-t table] -D chainrule-specification或iptables [-t table] -D chain rulenum,删除指定的鍊上的指定規則;</code>

<code>     </code><code>-R:replace,iptables [-t table] -R chain rulenumrule-specification,将指定的鍊上的指定規則替換為新的規則;</code>

        檢視:

<code>        </code><code>-L:list, iptables [-t table] -L [chain [rulenum]] [options...]</code>

<code>                </code><code>-n:數字格式;</code>

<code>                </code><code>-</code><code>v</code><code>:verbose,詳細格式資訊;</code>

<code>                        </code><code>-vv、-vvv,更詳細格式資訊</code>

<code>                   </code><code>--line-numbers:顯示鍊上的規則的編号;</code>

<code>                   </code><code>-x:exactly,顯示計數器的精确值; </code>

<code>    </code><code>顯示出的結果有這麼幾個含義:</code>

<code>     </code><code>pkts bytes target     prot opt </code><code>in</code>     <code>out     </code><code>source</code>               <code>destination</code>

<code>        </code><code>pkts:被本規則所比對到的的封包個數;</code>

<code>        </code><code>bytes:被本規則所比對到的封包的大小之和;</code>

<code>        </code><code>target:處理目标(目标可以為使用者自定義的鍊);</code>

<code>        </code><code>prot:協定{tcp,udp,icmp}</code>

<code>        </code><code>opt:可選項</code>

<code>        </code><code>in</code><code>:資料包流入接口        </code>

<code>        </code><code>out:資料包流出接口</code>

<code>        </code><code>source</code><code>:源位址</code>

<code>        </code><code>destination:目标位址</code>

四.計數器

    每條規則以及鍊的預設政策分别有各自的兩個計數器:

        (1) pkts:被本規則所比對到的的封包個數;

       (2) bytes:被本規則所比對到的封包的大小之和;

五:儲存及重載規則

    centos6:

        儲存:

            (1) service iptables save

                /etc/sysconfig/iptables檔案;

            (2) iptables-save &gt; /PATH/TO/SOMEFILE

        重載:

            (1) service iptables restart

            (2) iptables-restore &lt; /PATH/FROM/SOMEFILE

    centos7:引入了新的iptables前端管理服務工具firewalld

        firewalld通過前端管理工具:firewalld-cmd和firewalld-config,其功能更強大更完善,是以其規則會更複雜,為了保持了centos6上同樣使用iptables來管理規則,先需要在centos7上關掉和禁用firewalld服務,方法如下:

        (1) systemctl stop firewalld.service

        (2) systemctl disable firewalld.service

      本文轉自wzcLINUXER  51CTO部落格,原文連結:http://blog.51cto.com/yacheng0316/1889979,如需轉載請自行聯系原作者

繼續閱讀