1、鏈的基本操作
(1) 清除所有規(guī)則
1) 清除預(yù)設(shè)表filter中所有規(guī)則鏈中的規(guī)則
# iptables –F
2) 清除預(yù)設(shè)表filter中使用者自定鏈中的規(guī)則
# iptables –X
3)將指定鏈中所有規(guī)則的包字節(jié)計(jì)數(shù)器清零
# iptables –Z
(2)設(shè)置鏈的默認(rèn)策略
1)先允許,再禁止
用下面的命令初始化
# iptables –P INPUT ACCEPT
# iptables –P OUTPUT ACCEPT
# iptables –P FORWARD ACCEPT
2) 先禁止,再允許
用下面的命令初始化
# iptables –P INPUT DROP
# iptables –P OUTPUT DROP
# iptables –P FORWARD DROP
(3)列出表/鏈中的所有規(guī)則
# iptables –L –n
(4)向鏈中添加規(guī)則。下面的語(yǔ)句用于開(kāi)放網(wǎng)絡(luò)接口
# iptables –A INPUT –i lo –j ACCEPT
# iptables –A OUTPUT –o lo –j ACCEPT
# iptables –A INPUT –i eth0 –j ACCEPT
# iptables –A OUTPUT –o eth0 –j ACCEPT
# iptables –A FORWARD –i eth0 –j ACCEPT
# iptables –A FORWARD –o eth0 –j ACCEPT
(5)使用用戶(hù)自定義鏈
# iptables –N custom
# iptables –A custom –s 0/0 –d 0/0 –p icmp –j DROP
# iptables –A INPUT –s 0/0 –d 0/0 –j custom
2、設(shè)置基本的規(guī)則匹配(忽略目標(biāo)動(dòng)作)
(1) 指定協(xié)議匹配
1) 匹配指定的協(xié)議
# iptables –A INPUT –p tcp
2) 匹配指定協(xié)議之外的所有協(xié)議
# iptables –A INPUT –p ! tcp
(2) 指定地址匹配
1) 指定匹配的主機(jī)
# iptables –A INPUT –s 192.168.0.1
2) 指定匹配的網(wǎng)絡(luò)
# iptables –A INPUT –s 192.168.0.0/24
3) 匹配指定主機(jī)之外的地址
# iptables –A INPUT –s ! 192.168.0.1
4) 匹配指定網(wǎng)絡(luò)之外的網(wǎng)絡(luò)
# iptables –A INPUT –s ! 192.168.0.1/24
(3) 指定網(wǎng)絡(luò)接口匹配
1) 指定單一的網(wǎng)絡(luò)接口匹配
# iptables –A INPUT –i eth0
# iptables –A FORWARD –o eth0
2) 指定同類(lèi)型的網(wǎng)絡(luò)接口匹配
# iptables –A FORWARD –o ppp+
(4) 指定端口匹配
1) 指定單一的端口匹配
# iptables –A INPUT –p tcp –sport wwww
# iptables –A INPUT –p tcp –sport 80
# iptables –A INPUT –p udp –sport 53
# iptables –A INPUT –p udp –dport 53
2) 匹配指定端口之外的端口
# iptables –A INPUT –p tcp –dport !22
3) 匹配指定的端口范圍
# ipbables –A INPUT –p tcp –sport 22:80
4) 匹配ICMP端口和ICMP 類(lèi)型
# iptables –A INPUT –p icmp-type 8
(5) 指定IP碎片
# iptables –A FORWARD –p tcp –s 192.168.0.0/24 –d 192.168.2.100 –dport 80 –f ACCEPT
# iptables –A FORWARD –f –s 192.168.0.0/24 –d 192.168.2.100 –j ACCEPT
3、設(shè)置擴(kuò)展的規(guī)則匹配(忽略目標(biāo)動(dòng)作)
(1)多端口匹配擴(kuò)展
1)匹配多個(gè)源端口
# iptables –A INPUT –p tcp –m multiport –source-port 22,53,80,110
2)匹配多個(gè)目的端口
# iptables –A INPUT –p tcp –m multiport –destination-port 22,53,80,110
3)匹配多個(gè)端口
# iptables –A INPUT –p tcp –m multiport –prot 22,53,80,110
(2)指定TCP匹配擴(kuò)展
通過(guò)使用--tcp-flags 選項(xiàng)可以根據(jù)TCP包的標(biāo)志位進(jìn)行過(guò)濾,第一個(gè)參數(shù)為要檢查的標(biāo)志位;第二個(gè)參數(shù)是標(biāo)志位為1的標(biāo)志
# iptables –A INPUT –p tcp --tcp-flags SYN,FIN,ACK SYN
# iptables –p tcp --syn
表示SYN、ACK、FIN的標(biāo)志都要檢查,但是只有設(shè)置了SYN的才匹配
# iptables –A INPUT –p tcp --tcp-flags ALL SYN,ACK
表示ALL(SYN,ACK,F(xiàn)IN,RST,USG,PSH)的標(biāo)志都要檢查,但是只有設(shè)置了SYN和ACK的才匹配
(3)limit速率匹配擴(kuò)展
1)指定單位時(shí)間內(nèi)允許通過(guò)的數(shù)據(jù)包個(gè)數(shù)
# iptables –A INPUT –m limit --limit 300/hour
表示限制每小時(shí)允許通過(guò)300個(gè)數(shù)據(jù)包
2)指定觸發(fā)事件的閥值(默認(rèn)值是5)
# iptables –A INPUT –m limit --limit-burst 10
表示一次涌入的封包超過(guò)10個(gè)將被直接丟棄
3)同時(shí)指定速率限制和觸發(fā)閥值
# iptables –A INPUT –p icmp –m limit –limit 3/m –limit-burst 3