iptables命令學習筆記

NAME

iptables - IPV4數據包過濾與NAT管理工具

SYNOPSIS

iptables [-t table] -[AD] chain rule-specificat [options] 
iptables [-t table] -I chain [rulenum] rule-specification [options] 
iptables [-t table] -R chain rulenum rule-specification [options] 
iptables [-t table] -D chain rulenum [options] 
iptables [-t table] -[LFZ] [chain] [options] 
iptables [-t table] -N chain 
iptables [-t table] -X [chain] 
iptables [-t table] -P chain target [options] 
iptables [-t table] -E old-chain-name new-chain-name

DESCRIPTION

iptables命令用來設置、管理、查看Linux內核中的IP包過濾規則表。可以定義幾個不同的包,每個表包含一定數量的內置chain,也可以包含用戶自定義chain。

每條chain包含多條rule,rule中包含與packet的匹配規則,以及如何處理匹配的包。後者稱爲target,可以是jump到同一個table中的另一條用戶自定義的chain。

TARGETS

Target有幾個系統內置的特殊值:ACCEPT、DROP、QUEUE、RETURN。

ACCEPT表示讓packet通過,繼續配置chain中的下一條rule。DROP表示丟棄,QUEUE表示將packet內核空間直接發送到到用戶空間。RETURN表示從當前chain返回,從調用當前chain的下一條rule開始繼續處理。如果包通過了內置chian的所有rule,或者是在內置chain的某條rule中RETURN,則由這條內置chian中設置的默認規則決定如何處理這個packet。

TABLES

系統一般有三個獨立的table,系統呈現那些表可由內核配置確實。

Tag Description
-t, --table table
 

指定命令操作的table名。

Tag Description
filter:
 

默認table,其中的chain主要用於數據包的過濾。包含系統內置的三條chain,INPUT(數據包的目標是本機)、FORWARD(轉發數據包)、OUTPUT(由本機發送出去的數據包)。

nat:
 

主要用於實現NAT功能。當系統發現創建連接、斷開連接的包時,此表將發揮作用。通過監控連接的狀態,根據設置的規則,修改數據包以實現NAT功能。

包含三條內置的chain。PREROUTING修改進入本機的包,OUTPUT修改由本機發送出去的包,POSTROUTING用於修改離開本機的數據包,可能是FORWARD類型的包,也可能是OUTPUT類型的包。

mangle:
  此表用於爲實現特殊目的對包的修改。在最新的內核版本中,包含五種類型的chain:PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING。
raw:
 

優先級最高的表,主要通過使用Target NOTRACK,避免默認設置下對鏈接狀態的跟蹤以提高性。包含PREROUTING、OUTPUT兩條內置鏈,前者處理入包,後者處理出包。

OPTIONS

iptables從支持的選項可以劃分成幾個不同的組。

COMMANDS

下邊的這些選項用於指明iptables命令需要執行的動作。除非特別說明,一條iptables命令中只能包含下述動作中的一個。

Tag Description
-A, --append chain rule-specification
 

向chain追加一條或者多條rule。如果rule中的source或者destination代表多個地址時,一條rule會自動展開,每爲一個可能的地址都追加一條rule。

-D, --delete chain rule-specification 
-D, --delete chain rulenum
 

刪除chain中的一條或者多條rule。後邊的rule可以通過rule在chain中的編號指定,也可以指定rule的特徵值,由系統匹配。

-I, --insert chain [rulenumrule-specification
 

在chain的指定位置插入一條或者多條rule。如果rulenum沒有指定,則將新rule插入到chain的最前頭。

-R, --replace chain rulenum rule-specification
 

替換rule。新的rule定義必需明確,它的SOURCE或者destination不可以展開,否則命令會報錯。

-L, --list [chain]
 

列出chain內容,如果沒有指定具體的chain,則列出table中的所有chain。

例如,列出nat表中的所有chain:

 iptables -t nat -n -L

iptables默認會試圖將IP地址解析成主機名之類,-n選項的意思是取消DNS的lookups過程,直接顯示數字。

另一個常用用的顯示命令:

 iptables -L -v

用於顯示詳細內容,包含rule的統計信息。

-F, --flush [chain]
 

刷新chain,相當於逐個刪除chain中的rule。

-Z, --zero [chain]
 

Zero the packet and byte counters in all chains. It is legal to specify the -L, --list (list) option as well, to see the counters immediately before they are cleared. (See above.)

將rule的統計信息歸零,可與-L選項同時使用。

-N, --new-chain chain
  創建用戶自定義chain。
-X, --delete-chain [chain]
 

刪除用戶自定義chain。有前提條,首先沒有其它chain引用當前要刪除的chain,如果有則先解除引用。第二,當前chain中不能包含任何rule,如果有需要先刪除。

-P, --policy chain target
 

爲chain設置默認target,當chain不知道如何處理某個包時,默認target就會生效。但是默認target不可以是內置的chain,也不可以是用戶自定義的chain,只能是系統內置的那幾個動作,如ACCEPT、DROP等。

-E, --rename-chain old-chain new-chain
  重名列chain。
-h 幫助。

以下參數指定chain中rule的具體內容。

Tag Description
-p, --protocol [!] protocol
 

設定包有關protocol的匹配規則。可以是tcp、udp、icmap或者全部。可以是protocol的名稱,也可以是數字。名稱與數字之間的對應關係可以在/etc/protocals文件中查找。"!"表示取反,數字0表示全部協議,是默認值。

-s, --source [!] address[/mask]
 

數據包源地址匹配規則。可以是一個IP地址,一個網段、主機名,也可以是網絡名。注意如果是主機名的話,iptable會通過DNS解析。如果是網段或者網絡名,則source會被展開成多個IP地址。

-d, --destination [!] address[/mask]
  數據包目標地址,與數據包源地址規則相同。
-j, --jump target
 

如果數據包匹配的話,此選項用於指出如何處理。可以是系統內置的動作,也可以是用戶自定義的chain。如果此選項沒有指定,則匹配的rule不對數據包做任何處理,但rule的統計信息會更新,如匹配的數據包的個數,字節數等。

-g, --goto chain
 

此選項表示將匹配的數據包轉移到用戶自定義chain。jump選項中指定的chain如果發生return,則返回到調用者中繼續,此選項則不會。

-i, --in-interface [!] name
  interface名稱,表示數據包進入本機的設備。可以取反,可以只指定設備名稱中的前幾個字符,後邊追加+號,實現通配。
-o, --out-interface [!] name
  數據包離開本機的設備,規則與-i選項相同。
[!] -f, --fragment
 

如果數據包很大將會被分片。此選項指出匹配除第一個分片以外的其它分片。如果前邊加上“!”號,則只匹配第一個分片。

-c, --set-counters PKTS BYTES
 

初始化rule關於數據包、字節數統計結果。

OTHER OPTIONS

其它可以使用的額外選項:

Tag Description
-v, --verbose
 

當調用顯示命令時,加上這個選項可顯示更加詳細的結果。

-n, --numeric
 

iptables默認會試圖將IP地址通過DNS映射成主機名、網絡名、服務名等,加上此選項則直接顯地址,避免解析過程。

-x, --exact
  顯示更加精確的統計信息。比如關於字節的統計,會顯示多少個字節,而不是M什麼的系統自動選報的單位。
--line-numbers
  顯示chain中的rule時添加上行號,這個行號就是rule在chain中的位置。
--modprobe=command
 

當向某個chain添加rule時,可能需要載入特定的模塊來支持,此選項用來指定模塊名稱。

MATCH EXTENSIONS

iptables能夠擴展包的匹配、處理模塊,有兩種載入這些模塊的方法。一個是隱式載入,比如指定了-p選項,後邊加上了協議名稱,則如果相應的匹配模塊還沒有載入會自動載入。另一種顯式的,通過-m、--match選項後邊直接加匹配模塊的名稱。

不同模塊,支持的可用選項不同。可通過-h選項外加模塊名稱,查看其支持的選項,或者查看官方文檔。

以下是基本模塊,應該是系統默認就使用的,可通過包名前加"!"禁止掉。

account

對指定的network/netmask進行流量計數、統計。

addrtype

本模塊根據地址類型匹配數據包,如單播、廣播等地址類型。

ah

此模塊用於匹配IPsec包中認證頭的SPIs。

childlevel

試驗型模塊。匹配一個包是否屬於某個connection或者這個connection的子connection。

comment

給rule添加註釋,註釋長度有限制。

condition

用於匹配特定文件名中的值是 ’0’ 或者 ’1’.

connbytes

與某個connetcion(單向、雙向)已經傳輸的數據包、字節等統計信息匹配,包括總數,平均等。

主要用來發現那些時間長,傳輸數據量大的連接,進而可將這些連接禁止或者調整到低優先級。

connlimit

設置單個IP或者地址塊對某個服務器IP可同時發起的connection的個數。

connmark

匹配connection包中的netfilter mark字段。

connrate

基於connection的傳輸速率匹配包,注意connbytes模塊是基於歷史統計數據,與這個不同。

conntrack

更加詳細的連接狀態跟蹤。

dstlimit

基於目的地址或者目的端口號,限制其傳輸速率。

esp

匹配IPsec包的ESP頭中的SPIs。

fuzzy

hashlimit

iprange

匹配某個範圍內的IP地址。

ipv4options

匹配IP數據包中Header內的各種字段。

length

匹配數據包長度。

mac

mark

mport

匹配源、目的端口號,端口號可以是集合。

multiport

匹配源、目的端口號,端口號可以是範圍。

physdev

匹配網橋設置的出、入端口號。

pkttype

This module matches the link-layer packet type.

匹配鏈路層包類型。

policy

state

連接狀態跟蹤,沒有conntrack詳細。

tcp

當設置-p tcp時,此模塊載入,它是用來匹配tcp數據包頭的。

TARGET EXTENSIONS

以上是對匹配規則的擴展,下邊是對target的擴展。

BALANCE

This allows you to DNAT connections in a round-robin way over a given range of destination addresses.

DNAT中,大多個目的地址之間實現round-robin輪轉。

Tag Description
--to-destination ipaddr-ipaddr
  Address range to round-robin over.


DNAT

實現DNAT的模塊,對出包進行轉換。此擴展target只能在nat表中使用,並且只能出現在PREROUTING與OUTPUT鏈中。或者用戶自定義鏈中,並且這種用戶自定義鏈只能由PREROUTING與OUTPUT鏈引用。

Tag Description
--to-destination ipaddr[-ipaddr][:port-port]
  which can specify a single new destination IP address, an inclusive range of IP addresses, and optionally, a port range (which is only valid if the rule also specifies -p tcp or -p udp). If no port range is specified, then the destination port will never be modified.

In Kernels up to 2.6.10 you can add several --to-destination options. For those kernels, if you specify more than one destination address, either via an address range or multiple --to-destination options, a simple round-robin (one after another in cycle) load balancing takes place between these addresses. Later Kernels (>= 2.6.11-rc1) don’t have the ability to NAT to multiple ranges anymore.

 

MASQUERADE

只能在nat表中的POSTROUTING鏈中使用。如果需要變換的內部地址是靜態不變的,則應用使用SNAT。如果需要轉換的內部地址是動態的,則使用當前模塊。因爲設備上的地址是會變化的,本質上實現設備與地址的轉換。

Tag Description
--to-ports port[-port]
  This specifies a range of source ports to use, overriding the default SNAT source port-selection heuristics (see above). This is only valid if the rule also specifies -p tcp or -p udp.


REJECT

對DROP動作的擴展,除了丟棄匹配的包,還向發送者回送一個包以便通知對方。

Tag Description
--reject-with type
  The type given can be
 icmp-net-unreachable 
 icmp-host-unreachable 
 icmp-port-unreachable 
 icmp-proto-unreachable 
 icmp-net-prohibited 
 icmp-host-prohibited or 
 icmp-admin-prohibited (*) 
which return the appropriate ICMP error message (port-unreachable is the default). The option tcp-reset can be used on rules which only match the TCP protocol: this causes a TCP RST packet to be sent back. This is mainly useful for blocking ident (113/tcp) probes which frequently occur when sending mail to broken mail hosts (which won’t accept your mail otherwise).
(*) Using icmp-admin-prohibited with kernels that do not support it will result in a plain DROP instead of REJECT
   


SNAT

實現SNAT,只能在nat表中的PSTROUTING鏈中使用。

Tag Description
--to-source ipaddr[-ipaddr][:port-port]
  which can specify a single new source IP address, an inclusive range of IP addresses, and optionally, a port range (which is only valid if the rule also specifies -p tcp or -p udp). If no port range is specified, then source ports below 512 will be mapped to other ports below 512: those between 512 and 1023 inclusive will be mapped to ports below 1024, and other ports will be mapped to 1024 or above. Where possible, no port alteration will occur.

In Kernels up to 2.6.10, you can add several --to-source options. For those kernels, if you specify more than one source address, either via an address range or multiple --to-source options, a simple round-robin (one after another in cycle) takes place between these addresses. Later Kernels (>= 2.6.11-rc1) don’t have the ability to NAT to multiple ranges anymore.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章