溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iptables命令的作用是什么

發(fā)布時間:2021-08-12 15:09:32 來源:億速云 閱讀:269 作者:chen 欄目:網(wǎng)絡(luò)安全

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

一.Iptables命令參數(shù)   

  1. 命令結(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共有三個鏈:INPUT  FORWARD   OUTPUT

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

# iptables  - t  filter  - L

iptables命令的作用是什么

結(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不會影響到默認(rèn)策略的狀態(tài),默認(rèn)策略只能通過- P這個參數(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)容的行號 

示例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          

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

Nat表的示例

NAT共有三個鏈: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共有五個鏈: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共有兩個鏈: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語法規(guī)則

基本語法   

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

高級語法   

iptables  -t  filter  -A   INPUT  -m  mac  --mac-source  00:E0:18:00:7C:A4  -j DROP  

注:基本語法就是iptables只調(diào)用ipTable_filter.ko模塊,高級語法就是除了ipTable_filter.ko模塊,還會調(diào)用其他模塊。上例高級語法中還調(diào)用了xt_mac.ko這個模塊。 

示例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         

語法解釋:

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

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

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

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

處理方式:

ACCEPT:允許通過

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

REJECT:將數(shù)據(jù)包丟掉,并回送一個Destination Unreachable的icmp數(shù)據(jù)包給發(fā)送端,發(fā)送端的應(yīng)用程序在收到這個錯誤信息后,會終止連接。

示例2:不允許192.168.0.200主機(jī)通過本機(jī)的DNS服務(wù)來執(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 

語法解釋:

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

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

-s:指定“來源端”IP

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

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

基本語法結(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的錯誤信息給來源端。

示例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
語法解釋:

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

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

-s:指定來源端IP

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

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

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

[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        

語法解釋:

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

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

-s :指定來源端IP

-d:指定目的端IP

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

示例5:只允許客戶端從eth2這個接口訪問本機(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

語法解釋:

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

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

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

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

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

示例6:不允許本機(jī)的應(yīng)用程序從eth0接口發(fā)送數(shù)據(jù)包去訪問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

語法解釋:

-A OUTPUT:限制本機(jī)對方訪問

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

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

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

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

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

示例7:不允許企業(yè)內(nèi)部的主機(jī)訪問企業(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連接因特網(wǎng),eth2連接企業(yè)內(nèi)網(wǎng)。

語法解釋:

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

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

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

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

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

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

三.Iptables參數(shù)整理

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

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

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

               ppp0(ppp的接口名稱,用于語音撥號)   

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

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

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

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

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

補(bǔ)充:可搭配“!”來代表反向,例如“-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.匹配來源/目的的IP地址

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

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

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

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

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

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

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

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

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

4.匹配來源/目的端口

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

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

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

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

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

5.處理方式

參數(shù)名稱:-j

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

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

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

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

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

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

到此,關(guān)于“iptables命令的作用是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI