iptables命令的作用是什么

這篇文章主要介紹“iptables命令的作用是什么”,在日常操作中,相信很多人在iptables命令的作用是什么問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”iptables命令的作用是什么”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

這篇文章主要介紹“iptables命令的作用是什么”,在日常操作中,相信很多人在iptables命令的作用是什么問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”iptables命令的作用是什么”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

為鹽山等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及鹽山網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都做網(wǎng)站、鹽山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

一.Iptables命令參數(shù)   

命令結(jié)構(gòu)      

Iptables   (- t   表名)   操作方式   規(guī)則條件      

注:若取消“- t 表名”,默認(rèn)是filter表。      

表名:filter、nat、mangle、raw      

操作方式:     - L   ##列出表內(nèi)容  

                     - F  ##清除表內(nèi)容,若不指定表名,默認(rèn)清除所有表的內(nèi)容。  

                     - A   ##添加新規(guī)則  

                     - P   ##設(shè)置默認(rèn)策略  

                     - I   ##插入新規(guī)則  

                     - R   ##取代舊規(guī)則  

                     - D   ##刪除規(guī)則      

規(guī)則條件:           

- p  tcp/udp/icmp/all   - j   ACCEPT/DROP/REJECT

Filter的示例Filter共有三個(gè)鏈:INPUT  FORWARD   OUTPUT

示例1:列出filter表的所有內(nèi)容      

# iptables  - t  filter  - L

結(jié)果顯示:1.第一部分為INPUT鏈的所有內(nèi)容,以下依次為FORWARD和OUTPUT.          

                2.三條鏈的默認(rèn)策略均為ACCEPT   示例2:列出filter表中的INOUT鏈的內(nèi)容
示例2:列出filter表中的INOUT鏈的內(nèi)容
[root@vm1 ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh

示例3:清除filter表中的所有內(nèi)容
[root@vm1 ~]# iptables -t filter -F
[root@vm1 ~]# iptables -t filter -L
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         

示例4:將規(guī)則添加到filter表中的INPUT鏈中
[root@vm1 ~]# iptables -t filter -A INPUT -p icmp -j ACCEPT
[root@vm1 ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

ACCEPT     icmp --  anywhere             anywhere            

示例5:將forward鏈的默認(rèn)策略設(shè)置為DROP
[root@vm1 ~]# iptables -t filter -P FORWARD  DROP
[root@vm1 ~]# iptables -t filter -L
Chain FORWARD (policy DROP)
target     prot opt source               destination        

注:iptables  - F不會(huì)影響到默認(rèn)策略的狀態(tài),默認(rèn)策略只能通過(guò)- P這個(gè)參數(shù)設(shè)置

示例6:在INPUT鏈中插入新規(guī)則。
[root@vm1 ~]# iptables -t filter -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     icmp --  anywhere             anywhere            

2    ACCEPT     tcp  --  anywhere             anywhere            
Chain FORWARD (policy DROP)

num  target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@vm1 ~]# iptables -t filter -I INPUT 2 -p udp  -j ACCEPT
[root@vm1 ~]# iptables -t filter -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     icmp --  anywhere             anywhere            2    ACCEPT     udp  --  anywhere             anywhere    
        

3    ACCEPT     tcp  --  anywhere             anywhere            
Chain FORWARD (policy DROP)

num  target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

注:“iptables - t filter - L - - line- number”用于顯示規(guī)則內(nèi)容的行號(hào) 

示例7:取代INPUT鏈中的已經(jīng)存在的規(guī)則

[root@vm1 ~]# iptables -t filter -R INPUT 2 -p tcp -j ACCEPT
[root@vm1 ~]# iptables -t  filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            

ACCEPT     icmp --  anywhere             anywhere            
Chain FORWARD (policy DROP)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

示例8:刪除INPUT鏈中已經(jīng)存在的規(guī)則
[root@vm1 ~]# iptables -t filter -L INPUT --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere            
2    ACCEPT     tcp  --  anywhere             anywhere            
3    ACCEPT     icmp --  anywhere             anywhere            
[root@vm1 ~]# iptables -t filter -D INPUT 2
[root@vm1 ~]# iptables -t filter -L INPUT --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere            

2    ACCEPT     icmp --  anywhere             anywhere          

注:以上七個(gè)參數(shù)也同樣適用于其他三個(gè)表。

Nat表的示例

NAT共有三個(gè)鏈:PREROUTING  POSTROUTING  OUTPUT。 

示例1.列出nat表中的所有規(guī)則
[root@vm1 ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain POSTROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

示例2:給nat表中的POSTROUTING添加規(guī)則。

[root@vm1 ~]# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j SNAT --to 192.168.5.178
[root@vm1 ~]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

SNAT       all  --  192.168.0.0/24       anywhere            to:192.168.5.178
Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

Mangle表示例

Mangle共有五個(gè)鏈:PREROUTING  INPUT  FORWARD  OUTPUT  POSTROUTING
[root@vm1 ~]# iptables -t mangle -A INPUT -p icmp -j ACCEPT
[root@vm1 ~]# iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

ACCEPT     icmp --  anywhere             anywhere            
Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         
Chain POSTROUTING (policy ACCEPT)

target     prot opt source               destination        

Raw示例

Raw共有兩個(gè)鏈:PREROUTING  OUTPUT   
[root@vm1 ~]# iptables -t raw -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        
[root@vm1 ~]# iptables -t raw -A OUTPUT -p tcp -j ACCEPT
[root@vm1 ~]# iptables -t raw -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

ACCEPT     tcp  --  anywhere             anywhere            
[root@vm1 ~]# iptables -t raw -A OUTPUT -p tcp -j NOTRACK
[root@vm1 ~]# iptables -t raw -L
Chain PREROUTING (policy ACCEPT)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            

NOTRACK    tcp  --  anywhere             anywhere            

二. Iptables語(yǔ)法規(guī)則

基本語(yǔ)法  

iptables  -t  filter  -A  INPUT  -p  icmp   -j  DROP

高級(jí)語(yǔ)法  

iptables  -t  filter  -A   INPUT  -m  mac  --mac-source  00:E0:18:00:7C:A4  -j DROP   注:基本語(yǔ)法就是iptables只調(diào)用ipTable_filter.ko模塊,高級(jí)語(yǔ)法就是除了ipTable_filter.ko模塊,還會(huì)調(diào)用其他模塊。上例高級(jí)語(yǔ)法中還調(diào)用了xt_mac.ko這個(gè)模塊。

 

示例1:將192.168.0.200進(jìn)入本機(jī)的icmp協(xié)議包丟掉
[root@vm1 ~]# iptables -A INPUT -p icmp -s 192.168.0.200 -j DROP
[root@vm1 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            DROP       icmp --  192.168.0.200        anywhere 

           
Chain FORWARD (policy DROP)

target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

語(yǔ)法解釋:

- A INPUT :INPUT鏈保護(hù)的對(duì)象是本機(jī),主要說(shuō)明保護(hù)的對(duì)象

-p icmp:-p參數(shù)用于指定匹配特定協(xié)議的數(shù)據(jù)包,此處為icmp協(xié)議。

-s 192.268.0.200:-s用于指定匹配數(shù)據(jù)包中的“來(lái)源“端IP,-d用于指定匹配數(shù)據(jù)包中的“目的”端IP

-j:意為將符合以上條件的數(shù)據(jù)包做特定處理。

處理方式:

ACCEPT:允許通過(guò)

DROP:將數(shù)據(jù)包丟掉,這種處理方式將導(dǎo)致來(lái)源端誤認(rèn)為數(shù)據(jù)包丟失而不斷發(fā)送數(shù)據(jù)包,這個(gè)動(dòng)作將延續(xù)到連接超時(shí)。

REJECT:將數(shù)據(jù)包丟掉,并回送一個(gè)Destination Unreachable的icmp數(shù)據(jù)包給發(fā)送端,發(fā)送端的應(yīng)用程序在收到這個(gè)錯(cuò)誤信息后,會(huì)終止連接。
示例2:不允許192.168.0.200主機(jī)通過(guò)本機(jī)的服務(wù)來(lái)執(zhí)行名稱解析
[root@vm1 ~]# iptables -A INPUT -p udp -s 192.168.0.200 --dport 53 -j REJECT

[root@vm1 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  192.168.0.200        anywhere            

REJECT     udp  --  192.168.0.200        anywhere            udp dpt:domain reject-with icmp-port-unreachable 

語(yǔ)法解釋:

-A INPUT:保護(hù)主機(jī)

-p udp:匹配udp協(xié)議的數(shù)據(jù)包,DNS服務(wù)使用的是udp協(xié)議。

-s:指定“來(lái)源端”IP

--dport   53:--dport指定服務(wù)的目的端口,DNS服務(wù)使用的端口是udp協(xié)議的53端口。--sport意為指定服務(wù)使用源端口。

注:當(dāng)使用--dport或者--sport參數(shù)時(shí),一定要指明是tcp或者udp協(xié)議。若不指明。則默認(rèn)tcp,udp,icmp協(xié)議的端口都符合。

基本語(yǔ)法結(jié)構(gòu):iptables -A INPUT -    p udp/tcp/icmp  -s/-d 192.168.0.200 --dport/--sport 53 -j REJECT。

-j REJECT:符合以上條件的數(shù)據(jù)包都丟失,并回送udp的錯(cuò)誤信息給來(lái)源端。

示例3:允許192.168.0.200主機(jī)連接到本機(jī)的TELNET
[root@vm1 ~]# iptables -A INPUT -p tcp -s 192.168.0.200 --dport 23 -j ACCEPT
[root@vm1 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  192.168.0.200        anywhere            
REJECT     udp  --  192.168.0.200        anywhere            udp dpt:domain reject-with icmp-port-unreachable
ACCEPT     tcp  --  192.168.0.200        anywhere            tcp dpt:telnet

語(yǔ)法解釋:

-A INPUT:保護(hù)本機(jī)

-p tcp:匹配tcp協(xié)議,telnet服務(wù)使用的是tcp協(xié)議

-s:指定來(lái)源端IP

--dport 23:匹配tcp協(xié)議的23端口,此端口為telnet服務(wù)的默認(rèn)端口。

-j ACCEPT:允許符合以上條件的數(shù)據(jù)包通過(guò)。

示例4:允許192.168.1.0/24網(wǎng)段的主機(jī)向本機(jī)192.168.0.1提出任何服務(wù)請(qǐng)求

[root@vm1 ~]# iptables -A INPUT -p all -s 192.168.1.0/24 -d 192.168.1.0 -j ACCEPT
[root@vm1 ~]# iptables  -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  192.168.0.200        anywhere            
REJECT     udp  --  192.168.0.200        anywhere            udp dpt:domain reject-with icmp-port-unreachable
ACCEPT     tcp  --  192.168.0.200        anywhere            tcp dpt:telnet

ACCEPT     all  --  192.168.1.0/24       192.168.1.0        

語(yǔ)法解釋:

-A INPUT:保護(hù)本機(jī)

-p all:匹配u任何協(xié)議的數(shù)據(jù)包

-s :指定來(lái)源端IP

-d:指定目的端IP

-j ACCETP:允許符合以上條件的數(shù)據(jù)包通過(guò)。

示例5:只允許客戶端從eth2這個(gè)接口訪問(wèn)本機(jī)的ssh服務(wù)
[root@vm1 ~]# iptables -A INPUT -p tcp -i eth2 --dport 22 -j ACCEPT

[root@vm1 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            
DROP       icmp --  192.168.0.200        anywhere            
REJECT     udp  --  192.168.0.200        anywhere            udp dpt:domain reject-with icmp-port-unreachable
ACCEPT     tcp  --  192.168.0.200        anywhere            tcp dpt:telnet
ACCEPT     all  --  192.168.1.0/24       192.168.1.0         

ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh

語(yǔ)法解釋:

-A INPUT:保護(hù)主機(jī)

-p tcp:匹配tcp協(xié)議

-i eth2:匹配從本機(jī)eth2端口進(jìn)入的數(shù)據(jù)包。-i意為數(shù)據(jù)包從本機(jī)的某個(gè)端口進(jìn)入,-o意為數(shù)據(jù)包從本機(jī)的某個(gè)端口出去。

--dport 22:指定ssh服務(wù)所使用的tcp協(xié)議的22端口

-j ACCEPT:意為符合以上條件的數(shù)據(jù)包均允許通過(guò)

示例6:不允許本機(jī)的應(yīng)用程序從eth0接口發(fā)送數(shù)據(jù)包去訪問(wèn)edu.uuu.com.tw網(wǎng)站
[root@vm1 ~]# iptables -A OUTPUT -p tcp -o eth0 -d edu.uuu.com.tw --dport 80 -j REJECT

[root@vm1 ~]# iptables -L
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

REJECT     tcp  --  anywhere             vm5.example.com     tcp dpt:http reject-with icmp-port-unreachable

語(yǔ)法解釋:

-A OUTPUT:限制本機(jī)對(duì)方訪問(wèn)

-p tcp :匹配tcp協(xié)議

-o eth0:匹配數(shù)據(jù)是否時(shí)從eth0接口送出的

-d edu.uuu.com.tw:指定目的網(wǎng)站

--dport 80:匹配的服務(wù)端口為http服務(wù)的80端口

-j REJECT:符合以上 條件的數(shù)據(jù)包全部丟失,并將錯(cuò)誤信息返回到來(lái)源端

示例7:不允許企業(yè)內(nèi)部的主機(jī)訪問(wèn)企業(yè)意外的網(wǎng)站
[root@vm1 ~]# iptables -A FORWARD -i eth2 -o eth0 -p tcp --dport 80 -j DROP

[root@vm1 ~]# iptables -L
Chain FORWARD (policy DROP)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere            tcp dpt:http

注:本例使用的網(wǎng)關(guān)式防火墻,因此所用的鏈?zhǔn)荈ORWARD,假設(shè)此防火墻主機(jī)上的eth0連接因創(chuàng)新互聯(lián),eth2連接企業(yè)內(nèi)網(wǎng)。

語(yǔ)法解釋:

-A FORWARD:保護(hù)防火墻后面的主機(jī)。

-i eth2:匹配數(shù)據(jù)包進(jìn)入的接口進(jìn)入

-o eth0:匹配數(shù)據(jù)包離開(kāi)的接口

-p tcp:匹配tcp協(xié)議的數(shù)據(jù)包

--dport 80:匹配目的服務(wù)渡口為80的數(shù)據(jù)包

-j REJECT:將符合以上條件的數(shù)據(jù)包都丟棄,并將錯(cuò)誤信息返回給來(lái)源端。

三.Iptables參數(shù)整理

1.接口的匹配參數(shù)

參數(shù)名稱:-i(進(jìn)入)    -o(送出)

參數(shù)值:  eth0(以太網(wǎng)的接口名稱)   

               ppp0(ppp的接口名稱,用于語(yǔ)音撥號(hào))  

               lo(本地回環(huán)接口)   

               fddi0(光纖網(wǎng)絡(luò)接口)

使用示例:-i eth0(匹配從eth0接口進(jìn)入的數(shù)據(jù)包)   

                -o eth0(匹配從eth0接口送出的數(shù)據(jù)包)

意義:匹配數(shù)據(jù)包進(jìn)出的接口

補(bǔ)充:可搭配“!”來(lái)代表反向,例如“-i ! eth0”代表匹配不是從eth0接口進(jìn)入的數(shù)據(jù)包

2.上層協(xié)議的匹配參數(shù)

參數(shù)名稱:-p

參數(shù)值:  tcp(匹配的上層協(xié)議是tcp協(xié)議)   

               udp(匹配的上層協(xié)議是udp協(xié)議)   

               icmp(匹配的上層協(xié)議是icmp協(xié)議)  

               all(匹配所有的上層協(xié)議)

意義:匹配上層通信協(xié)議

補(bǔ)充:可搭配“!”表示反向,“-p ! icmp”意為匹配不是icmp的協(xié)議

3.匹配來(lái)源/目的的IP地址

參數(shù)名稱:-s(源)   -d(目的)

參數(shù)值:192.168.0.1(匹配單一IP)

             172.10.0.0/16(匹配一個(gè)B類網(wǎng)段)

             192.168.0.0/24(匹配一個(gè)C類網(wǎng)段)

             www.playboy.com(匹配一個(gè)FQDN,但存放的值還是一個(gè)IP)

使用示例:-s 192.168.0.0/24(匹配從192.168.0.0網(wǎng)段法來(lái)的數(shù)據(jù)包)

                -s 192.168.0.1(匹配從192.168.0.1主機(jī)法來(lái)的數(shù)據(jù)包)

                -d 192.168.0.10(匹配發(fā)送到192.168.0.10主機(jī)的數(shù)據(jù)包)

意義:匹配數(shù)據(jù)包的來(lái)源或者目的IP

4.匹配來(lái)源/目的端口

參數(shù)名稱:--sport(源)  --dport(目的)

參數(shù)值:匹配端口的目的是為了匹配所需訪問(wèn)的服務(wù)

使用示例:--dport  80(匹配要訪問(wèn)web的數(shù)據(jù)包)

                --sport  110(匹配由pop3服務(wù)應(yīng)答給客戶端的數(shù)據(jù)包)

意義:匹配數(shù)據(jù)的來(lái)源或者目的端口

5.處理方式

參數(shù)名稱:-j

參數(shù)值:ACCEPT(允許)

             DROP(將數(shù)據(jù)包丟棄)

             REJECT(將數(shù)據(jù)包丟棄,并回送發(fā)送端一個(gè)icmp數(shù)據(jù)包)

使用示例:-j ACCEPT(允許)

                -j DROP(將數(shù)據(jù)包丟棄)

    意義:采用特定方式來(lái)處理符合條件的數(shù)據(jù)包。

標(biāo)題名稱:iptables命令的作用是什么
當(dāng)前地址:http://bm7419.com/article10/sesjgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、定制網(wǎng)站、自適應(yīng)網(wǎng)站、做網(wǎng)站、外貿(mào)建站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)