<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                iptables防火墻可以用于創建過濾(filter)與NAT規則。所有Linux發行版都能使用iptables,因此理解如何配置 iptables將會幫助你更有效地管理Linux防火墻。如果你是第一次接觸iptables,你會覺得它很復雜,但是一旦你理解iptables的工 作原理,你會發現其實它很簡單。? 首先介紹iptables的結構:iptables -> Tables -> Chains -> Rules. 簡單地講,tables由chains組成,而chains又由rules組成。如下圖所示。?![iptables基礎知識詳解 ](http://static.open-open.com/lib/uploadImg/20140917/20140917104640_878.png) 圖: IPTables Table, Chain, and Rule Structure 一、iptables的表與鏈 iptables具有Filter, NAT, Mangle, Raw四種內建表: 1\. Filter表 Filter表示iptables的默認表,因此如果你沒有自定義表,那么就默認使用filter表,它具有以下三種內建鏈: * INPUT鏈?– 處理來自外部的數據。 * OUTPUT鏈?– 處理向外發送的數據。 * FORWARD鏈?– 將數據轉發到本機的其他網卡設備上。 2\. NAT表 NAT表有三種內建鏈: * PREROUTING鏈?– 處理剛到達本機并在路由轉發前的數據包。它會轉換數據包中的目標IP地址(destination ip address),通常用于DNAT(destination NAT)。 * POSTROUTING鏈?– 處理即將離開本機的數據包。它會轉換數據包中的源IP地址(source ip address),通常用于SNAT(source NAT)。 * OUTPUT鏈?– 處理本機產生的數據包。 3\. Mangle表 Mangle表用于指定如何處理數據包。它能改變TCP頭中的QoS位。Mangle表具有5個內建鏈: * PREROUTING * OUTPUT * FORWARD * INPUT * POSTROUTING 4\. Raw表 Raw表用于處理異常,它具有2個內建鏈: * PREROUTING chain * OUTPUT chain 5.小結 下圖展示了iptables的三個內建表: ![iptables基礎知識詳解 ](http://static.open-open.com/lib/uploadImg/20140917/20140917104641_651.png) 圖: IPTables 內建表 二、IPTABLES 規則(Rules) 牢記以下三點式理解iptables規則的關鍵: * Rules包括一個條件和一個目標(target) * 如果滿足條件,就執行目標(target)中的規則或者特定值。 * 如果不滿足條件,就判斷下一條Rules。 目標值(Target Values) 下面是你可以在target里指定的特殊值: * ACCEPT?– 允許防火墻接收數據包 * DROP?– 防火墻丟棄包 * QUEUE?– 防火墻將數據包移交到用戶空間 * RETURN?– 防火墻停止執行當前鏈中的后續Rules,并返回到調用鏈(the calling chain)中。 如果你執行iptables –list你將看到防火墻上的可用規則。下例說明當前系統沒有定義防火墻,你可以看到,它顯示了默認的filter表,以及表內默認的input鏈, forward鏈, output鏈。 # iptables -t filter –list? 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 查看mangle表: # iptables -t mangle –list 查看NAT表: # iptables -t nat –list 查看RAW表: # iptables -t raw –list !注意:如果不指定?-t選項,就只會顯示默認的?filter表。因此,以下兩種命令形式是一個意思: # iptables -t filter –list? (or)? # iptables –list 以下例子表明在filter表的input鏈, forward鏈, output鏈中存在規則: # iptables –list? Chain INPUT (policy ACCEPT)? num ? ? ? ?target ? ? ? ? ? ? ? ? prot ? ?opt ? ? ? ?source ? ? ? ? ? ?destination? 1? ? RH-Firewall-1-INPUT ? ? all ? ? ? — ? ? ? ? 0.0.0.0/0 ? ? ? ? 0.0.0.0/0 Chain FORWARD (policy ACCEPT) num ? ? ? ?target ? ? ? ? ? ? ? ? prot ? ?opt ? ? ? ?source ? ? ? ? ? ?destination 1 ? ? ?RH-Firewall-1-INPUT ? all ? ? ? ?– ? ? ?0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 Chain OUTPUT (policy ACCEPT) num? target? ? prot opt source? ? ? ? ? ? ? destination Chain RH-Firewall-1-INPUT (2 references) num? target? ? prot opt source? ? ? ? ? ? ? destination 1? ? ACCEPT? ? all? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0 2? ? ACCEPT? ? icmp –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? icmp type 255 3? ? ACCEPT? ? esp? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0 4? ? ACCEPT? ? ah? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0 5? ? ACCEPT? ? udp? –? 0.0.0.0/0? ? ? ? ? ? 224.0.0.251? ? ? ? udp dpt:5353 6? ? ACCEPT? ? udp? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? udp dpt:631 7? ? ACCEPT? ? tcp? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? tcp dpt:631 8? ? ACCEPT? ? all? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? state RELATED,ESTABLISHED 9? ? ACCEPT? ? tcp? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? state NEW tcp dpt:22 10? REJECT? ? all? –? 0.0.0.0/0? ? ? ? ? ? 0.0.0.0/0? ? ? ? ? reject-with icmp-host-prohibited 以上輸出包含下列字段: * num – 指定鏈中的規則編號 target – 前面提到的target的特殊值 prot – 協議:tcp, udp, icmp等 source – 數據包的源IP地址 destination – 數據包的目標IP地址 三、清空所有iptables規則 在配置iptables之前,你通常需要用iptables –list命令或者iptables-save命令查看有無現存規則,因為有時需要刪除現有的iptables規則: iptables –flush? 或者? iptables -F 這兩條命令是等效的。但是并非執行后就萬事大吉了。你仍然需要檢查規則是不是真的清空了,因為有的linux發行版上這個命令不會清除NAT表中的規則,此時只能手動清除: iptables -t NAT -F 四、永久生效 當你刪除、添加規則后,這些更改并不能永久生效,這些規則很有可能在系統重啟后恢復原樣。為了讓配置永久生效,根據平臺的不同,具體操作也不同。下面進行簡單介紹: 1.Ubuntu 首先,保存現有的規則: iptables-save > /etc/iptables.rules 然后新建一個bash腳本,并保存到?/etc/network/if-pre-up.d/目錄下: #!/bin/bash? iptables-restore 這樣,每次系統重啟后iptables規則都會被自動加載。 !注意:不要嘗試在.bashrc或者.profile中執行以上命令,因為用戶通常不是root,而且這只能在登錄時加載iptables規則。 2.CentOS, RedHat # 保存iptables規則? service iptables save # 重啟iptables服務 service iptables stop service iptables start 查看當前規則: cat? /etc/sysconfig/iptables 五、追加iptables規則 可以使用iptables -A命令追加新規則,其中?-A表示?Append。因此,?新的規則將追加到鏈尾。 一般而言,最后一條規則用于丟棄(DROP)所有數據包。如果你已經有這樣的規則了,并且使用?-A參數添加新規則,那么就是無用功。 1.語法 iptables -A chain firewall-rule * -A chain – 指定要追加規則的鏈 * firewall-rule – 具體的規則參數 2.描述規則的基本參數 以下這些規則參數用于描述數據包的協議、源地址、目的地址、允許經過的網絡接口,以及如何處理這些數據包。這些描述是對規則的基本描述。 -p 協議(protocol) * 指定規則的協議,如tcp, udp, icmp等,可以使用all來指定所有協議。 * 如果不指定-p參數,則默認是all值。這并不明智,請總是明確指定協議名稱。 * 可以使用協議名(如tcp),或者是協議值(比如6代表tcp)來指定協議。映射關系請查看/etc/protocols * 還可以使用–protocol參數代替-p參數 -s 源地址(source) * 指定數據包的源地址 * 參數可以使IP地址、網絡地址、主機名 * 例如:-s 192.168.1.101指定IP地址 * 例如:-s 192.168.1.10/24指定網絡地址 * 如果不指定-s參數,就代表所有地址 * 還可以使用–src或者–source -d 目的地址(destination) * 指定目的地址 * 參數和-s相同 * 還可以使用–dst或者–destination -j 執行目標(jump to target) * -j代表”jump to target” * -j指定了當與規則(Rule)匹配時如何處理數據包 * 可能的值是ACCEPT, DROP, QUEUE, RETURN * 還可以指定其他鏈(Chain)作為目標 -i 輸入接口(input interface) * -i代表輸入接口(input interface) * -i指定了要處理來自哪個接口的數據包 * 這些數據包即將進入INPUT, FORWARD, PREROUTE鏈 * 例如:-i eth0指定了要處理經由eth0進入的數據包 * 如果不指定-i參數,那么將處理進入所有接口的數據包 * 如果出現!?-i eth0,那么將處理所有經由eth0以外的接口進入的數據包 * 如果出現-i eth+,那么將處理所有經由eth開頭的接口進入的數據包 * 還可以使用–in-interface參數 -o 輸出(out interface) * -o代表”output interface” * -o指定了數據包由哪個接口輸出 * 這些數據包即將進入FORWARD, OUTPUT, POSTROUTING鏈 * 如果不指定-o選項,那么系統上的所有接口都可以作為輸出接口 * 如果出現!?-o eth0,那么將從eth0以外的接口輸出 * 如果出現-i eth+,那么將僅從eth開頭的接口輸出 * 還可以使用–out-interface參數 3.描述規則的擴展參數 對規則有了一個基本描述之后,有時候我們還希望指定端口、TCP標志、ICMP類型等內容。 –sport 源端口(source port)針對 -p tcp 或者 -p udp * 缺省情況下,將匹配所有端口 * 可以指定端口號或者端口名稱,例如”–sport 22″與”–sport ssh”。 * /etc/services文件描述了上述映射關系。 * 從性能上講,使用端口號更好 * 使用冒號可以匹配端口范圍,如”–sport 22:100″ * 還可以使用”–source-port” –-dport 目的端口(destination port)針對-p tcp 或者 -p udp * 參數和–sport類似 * 還可以使用”–destination-port” -–tcp-flags TCP標志 針對-p tcp * 可以指定由逗號分隔的多個參數 * 有效值可以是:SYN, ACK, FIN, RST, URG, PSH * 可以使用ALL或者NONE -–icmp-type ICMP類型 針對-p icmp * –icmp-type 0 表示Echo Reply * –icmp-type 8 表示Echo 4.追加規則的完整實例:僅允許SSH服務 本例實現的規則將僅允許SSH數據包通過本地計算機,其他一切連接(包括ping)都將被拒絕。 # 1.清空所有iptables規則? iptables -F # 2.接收目標端口為22的數據包 iptables -A INPUT -i eth0 -p tcp –dport 22 -j ACCEPT # 3.拒絕所有其他數據包 iptables -A INPUT -j DROP 六、更改默認策略 上例的例子僅對接收的數據包過濾,而對于要發送出去的數據包卻沒有任何限制。本節主要介紹如何更改鏈策略,以改變鏈的行為。 1\. 默認鏈策略 /!\警告:請勿在遠程連接的服務器、虛擬機上測試! 當我們使用-L選項驗證當前規則是發現,所有的鏈旁邊都有?policy ACCEPT標注,這表明當前鏈的默認策略為ACCEPT: # iptables -L? Chain INPUT (policy ACCEPT)? target? ? prot opt source? ? ? ? ? ? ? destination? ACCEPT? ? tcp? –? anywhere? ? ? ? ? ? anywhere? ? ? ? ? ? tcp dpt:ssh? DROP? ? ? all? –? anywhere? ? ? ? ? ? anywhere Chain FORWARD (policy ACCEPT) target? ? prot opt source? ? ? ? ? ? ? destination Chain OUTPUT (policy ACCEPT) target? ? prot opt source? ? ? ? ? ? ? destination 這種情況下,如果沒有明確添加DROP規則,那么默認情況下將采用ACCEPT策略進行過濾。除非: a)為以上三個鏈單獨添加DROP規則: iptables -A INPUT -j DROP? iptables -A OUTPUT -j DROP? iptables -A FORWARD -j DROP b)更改默認策略: iptables -P INPUT DROP? iptables -P OUTPUT DROP? iptables -P FORWARD DROP 糟糕!!如果你嚴格按照上一節的例子配置了iptables,并且現在使用的是SSH進行連接的,那么會話恐怕已經被迫終止了! 為什么呢?因為我們已經把OUTPUT鏈策略更改為DROP了。此時雖然服務器能接收數據,但是無法發送數據: # iptables -L? Chain INPUT??(policy DROP)? target? ? prot opt source? ? ? ? ? ? ? destination? ACCEPT? ? tcp? –? anywhere? ? ? ? ? ? anywhere? ? ? ? ? ? tcp dpt:ssh? DROP? ? ? all? –? anywhere? ? ? ? ? ? anywhere Chain FORWARD?(policy DROP) target? ? prot opt source? ? ? ? ? ? ? destination Chain OUTPUT?(policy DROP) target? ? prot opt source? ? ? ? ? ? ? destination 七、配置應用程序規則 盡管5.4節已經介紹了如何初步限制除SSH以外的其他連接,但是那是在鏈默認策略為ACCEPT的情況下實現的,并且沒有對輸出數據包進行限 制。本節在上一節基礎上,以SSH和HTTP所使用的端口為例,教大家如何在默認鏈策略為DROP的情況下,進行防火墻設置。在這里,我們將引進一種新的 參數-m state,并檢查數據包的狀態字段。 1.SSH # 1.允許接收遠程主機的SSH請求? iptables -A INPUT -i eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT # 2.允許發送本地主機的SSH響應 iptables -A OUTPUT -o eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT * -m state:?啟用狀態匹配模塊(state matching module) * –-state:?狀態匹配模塊的參數。當SSH客戶端第一個數據包到達服務器時,狀態字段為NEW;建立連接后數據包的狀態字段都是ESTABLISHED * –sport 22:?sshd監聽22端口,同時也通過該端口和客戶端建立連接、傳送數據。因此對于SSH服務器而言,源端口就是22 * –dport 22:?ssh客戶端程序可以從本機的隨機端口與SSH服務器的22端口建立連接。因此對于SSH客戶端而言,目的端口就是22 如果服務器也需要使用SSH連接其他遠程主機,則還需要增加以下配置: # 1.送出的數據包目的端口為22? iptables -A OUTPUT -o eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT # 2.接收的數據包源端口為22 iptables -A INPUT -i eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT 2.HTTP HTTP的配置與SSH類似: # 1.允許接收遠程主機的HTTP請求? iptables -A INPUT -i eth0 -p tcp –dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT # 1.允許發送本地主機的HTTP響應 iptables -A OUTPUT -o eth0 -p tcp –sport 80 -m state –state ESTABLISHED -j ACCEPT 3.完整的配置 # 1.刪除現有規則? iptables -F # 2.配置默認鏈策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT DROP # 3.允許遠程主機進行SSH連接 iptables -A INPUT -i eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT # 4.允許本地主機進行SSH連接 iptables -A OUTPUT -o eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT iptables -A INPUT -i eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT # 5.允許HTTP請求 iptables -A INPUT -i eth0 -p tcp –dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT iptables -A OUTPUT -o eth0 -p tcp –sport 80 -m state –state ESTABLISHED -j ACCEPT References [1]??[Linux Firewall Tutorial: IPTables Tables, Chains, Rules Fundamentals](http://www.thegeekstuff.com/2011/01/iptables-fundamentals/) [2]??[IPTables Flush: Delete / Remove All Rules On RedHat and CentOS Linux](http://www.thegeekstuff.com/2011/01/redhat-iptables-flush/) [3]??[Linux IPTables: How to Add Firewall Rules (With Allow SSH Example)](http://www.thegeekstuff.com/2011/02/iptables-add-rule/) [4]??[Linux IPTables: Incoming and Outgoing Rule Examples (SSH and HTTP)](http://www.thegeekstuff.com/2011/03/iptables-inbound-and-outbound-rules/) [5]??[25 Most Frequently Used Linux IPTables Rules Examples](http://www.thegeekstuff.com/2011/06/iptables-rules-examples/) [6] man 8 iptables? 原文地址:?[http://www.tsingpost.com/articles/201406/878.html](http://www.tsingpost.com/articles/201406/878.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看