天天看點

iptables基礎知識

iptables由3個表filter,nat,mangle組成,主要實驗了filter表,這個表是用來過濾資料包的,

有三個鍊INPUT,OUTPUT,FORWARD。

配置防火牆政策有固定的格式

Iptables  表名   鍊名    比對條件  動作

-t 表名 (預設為filter)

-A 鍊名(在該鍊末尾append追加政策)

-I 鍊名 數字

-I (insert)插傳入連結,如果不加數字,預設是将寫的政策添加到表中所有政策的前面,

但是我們要指定插入到相應的行,我們可以這樣

Iptables –t filter –I INPUT 2 ……  這裡就是插到第二個

比對條件:

-i   網卡    資料包進入的網卡

-o  網卡   出去的

-s   ip   源ip

-d   ip    目的ip

-p   協定

--dport  端口号   目的端口号

--sport   端口号   源端口号

動作:

ACCEPT:對滿足政策的資料包允許通過

DROP:丢棄資料包,且不傳回任何資訊

REJECT:丢棄資料包,但是會傳回拒絕的資訊

LOG:把通過的資料包寫到日志中(相當于一個門衛對進去的人進行登記)

iptables   -t filter  -A INPUT –s 192.168.0.0/24  -p tcp  --dport 80 –j REJECT

上面這句的意思是:對filter表的INPUT鍊,追加一條政策,政策是,源位址在192.168.0.0/24網段内,使用tcp協定,目标端口為80的所有輸入包都執行REJECT動作(拒絕)

實驗室完成了filter表的INPUT鍊的基本操作和增加删除一條鍊,過程如下:

Iptables指令的管理控制選項

-A(append) 在指定鍊的末尾添加一條新的規則

-I (insert)在指定鍊中插入一條新規則,為指明插入位置

-D(delete)删除指定鍊裡的某條規則

-R (replace)修改、替換指定鍊的某條規則,按序号或内容确定要替換的規則

-L (list)列出指定鍊中所有規則進行檢視

-F (flush)清空指定鍊中的所有規則

-N 建立一條使用者自己定義的規則鍊

-X(delete-chain)删除指定表中使用者自己定義的規則鍊

-P 設定指定鍊的預設政策

-V 檢視iptables指令工具的版本(--version)資訊

-v 檢視規則清單時顯示詳細(--verbose)的資訊

-h 檢視指令幫助資訊(--help)

-n 使用數字形式(--number)顯示輸出結果,如顯示主機的ip位址而不是主機名

--line-number 檢視規則清單時,同時顯示規則在鍊中的順序号

添加規則有兩個參數:-A和-I。其中-A是添加到規則的末尾;-I可以插入到指定位置,沒有指定位置的話預設插入到規則的首部。

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一條規則到第三行,将行數直接寫到規則鍊的後面:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

檢視:

[root@test ~]# iptables -nLv --line-number

Iptables指令的比對條件選項

Ip位址:

來源位址:—s (source)。

目标位址:—d (destination)。

端口(port):

來源端口:——sport。

目标端口:——dport。

協定:—p。

個服務的端口和協定:

這裡隻列出比較常用的參數,詳細的請檢視man iptables

1、檢視

iptables -nvL –line-number

-L 檢視目前表的所有規則,預設檢視的是filter表,如果要檢視NAT表,可以加上-t NAT參數

-n 不對ip位址進行反查,加上這個參數顯示速度會快很多

-v 輸出詳細資訊,包含通過該規則的資料包數量,總位元組數及相應的網絡接口

–line-number 顯示規則的序列号,這個參數在删除或修改規則時會用到

[root@mail ~]# iptables -L  #檢視目前記憶體中iptables政策,預設是filter表  

Chain INPUT (policy ACCEPT)  

target     prot opt source               destination           

Chain FORWARD (policy ACCEPT)  

Chain OUTPUT (policy ACCEPT)  

[root@mail ~]# iptables -t filter -vnL  #加vn參數,有更多選項  

Chain INPUT (policy ACCEPT 31471 packets, 4322K bytes)  

 pkts bytes target     prot opt in     out     source               destination           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)  

Chain OUTPUT (policy ACCEPT 37490 packets, 3056K bytes)  

 pkts bytes target     prot opt in     out     source               destination         

#政策格式:iptables 表名 鍊名 比對條件 動作  

#下面這句話的意思是

對于filter表的INPUT鍊,源位址為192.169.1.0/24網段内的使用tcp  #協定80端口的輸入包,都執行REJECT(拒絕)動作  

[root@mail ~]# iptables -t filter -A INPUT -s 192.169.1.0/24 -p tcp --dport 80 -j REJECT  

[root@mail ~]# iptables -L  #需要注意的是,該政策目前隻在記憶體中,/etc/sysconfig/iptables配置檔案中是沒有的  

REJECT     tcp  --  192.169.1.0/24       anywhere            tcp dpt:http reject-with icmp-port-unreachable   

[root@mail ~]# iptables -vnL    #看的更詳細點  

Chain INPUT (policy ACCEPT 88 packets, 8188 bytes)  

    4   240 REJECT     tcp  --  *      *       192.169.1.0/24       0.0.0.0/0           tcp dpt:80 reject-with icmp-port-unreachable   

Chain OUTPUT (policy ACCEPT 131 packets, 11625 bytes)  

[root@mail ~]# vim /etc/sysconfig/iptables  #iptables中沒有  

# Firewall configuration written by system-config-firewall  

# Manual customization of this file is not recommended.  

*filter  

:INPUT ACCEPT [0:0]  

:FORWARD ACCEPT [0:0]  

:OUTPUT ACCEPT [0:0]  

COMMIT  

#執行save後會将這條政策寫入/etc/sysconfig/iptables,在儲存的時候,是執行覆寫式的儲存

#記憶體中有的儲存下來,記憶體中沒有的,這個檔案中有的将會被删除。 

[root@mail ~]# service iptables save      

iptables:将防火牆規則儲存到 /etc/sysconfig/iptables:     [确定]  

[root@mail ~]# vim /etc/sysconfig/iptables  

# Generated by iptables-save v1.4.7 on Wed Aug 15 17:28:53 2012  

:INPUT ACCEPT [4:352]  

:OUTPUT ACCEPT [4:298]  

-A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable   

# Completed on Wed Aug 15 17:28:53 2012  

#表的每條鍊後面都有一個預設動作,Chain INPUT (policy ACCEPT),預設動作意思是  

#沒有比對是以政策的比對條件時(按序比對),就執行的動作,可以修改鍊的預設動作  

[root@mail ~]# iptables -t filter -P INPUT DROP

             #修改filter表的INPUT鍊的預設動作  

[root@mail ~]# iptables -L  

Chain INPUT (policy DROP) 

target     prot opt source               destination    

[root@mail ~]# iptables -t filter -P INPUT ACCEPT   #暫時改回來  

#可以删除一條政策,政策是有序的,從1開始,要删除一條政策,需要知道它的序号  

[root@mail ~]# iptables -L --line-numbers   #檢視政策的序号  

num  target     prot opt source               destination           

1    REJECT     tcp  --  192.169.1.0/24       anywhere            tcp dpt:http reject-with icmp-port-unreachable   

num  target     prot opt source               destination    

[root@mail ~]# iptables -D INPUT 1  #删除INPUT鍊的序号為1的政策  

[root@mail ~]# vim /etc/sysconfig/iptables  #和前面一樣,這隻是删除記憶體中

的,/etc/sysconfig/iptables中仍然存在  

-A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable  

[root@mail ~]# service iptables save    #執行儲存,配置檔案中也被删除了  

# Generated by iptables-save v1.4.7 on Wed Aug 15 17:45:03 2012  

# Completed on Wed Aug 15 17:45:03 2012  

#除了INPUT,FORWARD,OUTPUT鍊,可以定義自己的鍊 

[root@mail ~]# iptables -N chen     #定義一個chen鍊,相當于多了一扇門  

#拒絕通過chen鍊,位址為192.169.1.99,協定為tcp端口為80的資料包  

[root@mail ~]# iptables -t filter -A chen -s 192.169.1.99 -p tcp --dport 80 -j REJECT     

[root@mail ~]# iptables -t filter -A INPUT -j chen  #把經過INPUT鍊的資料引入到chen這個鍊上  

[root@mail ~]# iptables -L --line-numbers    

1    chen       all  --  anywhere             anywhere    #INPUT鍊的target變為chen了          

Chain chen (1 references)   #可以看到多了一個鍊,下面的政策也存在  

1    REJECT     tcp  --  192.169.1.99         anywhere            tcp dpt:http reject-with icmp-port-unreachable   

[root@mail ~]#   

[root@mail ~]# service iptables save  

# Generated by iptables-save v1.4.7 on Wed Aug 15 18:11:28 2012  

:INPUT ACCEPT [1:125]  

:OUTPUT ACCEPT [1:71]  

:chen - [0:0]  

-A INPUT -j chen   

-A chen -s 192.169.1.99/32 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable   

# Completed on Wed Aug 15 18:11:28 2012  

[root@mail ~]# iptables -X chen     #删除chen這條鍊  

iptables: Too many links.  

[root@mail ~]# iptables -F      #删除前需要清空政策,否則删除不掉  

[root@mail ~]# iptables -X chen  

[root@mail ~]#   

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

繼續閱讀