歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Iptables防火牆,iptables

Iptables防火牆,iptables

日期:2017/3/6 9:22:24   编辑:學習Linux

Iptables防火牆,iptables


Iptables防火牆,iptables


1 位置

使用vim /usr/sysconfig/iptables

2 啟動、關閉、保存

  • service iptables stop
  • service iptables start
  • service iptables restart
  • service iptables save

3 結構

iptables –> tables –> chains –>rules

3.1 iptables的表與鏈

iptables具有Filter,NAT,Mangle,Raw四種內建表

3.1.1 Filter表

filter表示iptables的默認表,它具有三種內建鏈:

  • input chain   - 處理來之外部的數據
  • output chain - 處理向外發送的數據
  • forward chain- 將數據轉發到本機的其它網卡上

3.1.2 NAT表

NAT有三種內建的鏈:

  • prerouting   - 處理剛到達本機並在路由轉發前的數據包,它會轉換數據包中的目標IP地址(destination ip address),通常用於DNAT(destination NAT)。
  • postrouting - 處理即將離開本機數據包,它會轉換數據包中的源目標IP地址(source ip address),通常SNAT(source NAT)
  • output        - 處理本機產生的數據包

3.1.3 Mangle表

Mangle表用於指定如何處理數據包,它能改變TCP頭中的Qos位,Mangle表具有5個內建鏈

  • prerouting
  • output
  • forward
  • input
  • postrouting

3.1.4 Raw表

raw表用戶處理異常,它具有2個內建鏈

  • prerouting chain
  • output chain

3.2 Iptables規則(Rules)

  • rules包括一個條件和一個目標(target)
  • 如果滿足條件就執行目標target中規則或者特定值
  • 如果不滿足條件,就判斷下一條Rules

3.2.1 目標值

  • accept - 允許防火牆接收數據包
  • drop    - 防火牆丟棄數據包
  • queue  - 防火牆將數據包移交到用戶空間
  • return  - 防火牆停止執行當前鏈中的後續rules規則,並返回到調用鏈(the calling chain)

4 命令

#iptables -t filter -L   查看filter表

#iptables -t nat  -L    查看nat表

#iptables -t mangel -L 查看mangel表

#iptables -t raw  -L 查看Raw表

例如 以下例子表明在filter表的input鏈, forward鏈, output鏈中存在規則:

# iptables --list
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0

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

Chain RH-Firewall-1-INPUT (2 references)
num  target     prot opt source               destination
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 255
3    ACCEPT     esp  --  0.0.0.0/0            0.0.0.0/0
4    ACCEPT     ah   --  0.0.0.0/0            0.0.0.0/0
5    ACCEPT     udp  --  0.0.0.0/0            224.0.0.251         udp dpt:5353
6    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:631
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:631
8    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
10   REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

字段說明

num:編號

target:目標

prot:協議

source:數據包的源IP地址

destination:數據包的目標地址

4.1 清空所有的規則 

#iptables –flush

4.2 追加命令

iptables -A命令追加新規則,其中-A表示append,一般而言最後一條規則用於丟棄(drop)所有數據包,並且使用-A參數添加新規則,那麼就是無用的。

4.2.1 語法

iptables –A chain firewall-rule

  • -A chain 指定要追加的規則的鏈
  • firewall-rule 具體規則的參數
4.2.2 基本參數

用於描述數據包的協議,源地址、目的地址、允許經過的網絡接口,以及如何處理這些數據包。

  • 協議 –p (protocol)
    如tcp,udp,icmp等,可以使用all來指定所有協議
    不指定-p參數,默認值是all,
    可以使用協議名(tcp,udp),或者協議值(6代表tcp),映射關系可以查看/etc/protocols
  • 源地址 –s (source)
    指定數據包的源地址,參數可以使用IP地址、網絡地址、主機名,不指定-s參數,就是代表所有地址。
    例如:-s 192.168.1.101 具體的IP地址
    例如:-s 192.168.1.10/24 指定網絡地址
  • 目的地址 –d  (destination)
    指定目的地址,參數和-s相同
  • 執行目標 –j (jump to target)
    -j代表了當與規則(Rule)匹配時如何處理數據包,可能的值是accept、drop、queue、return,還可以指定其他鏈(chain)作為目標
  • 輸入接口 –i (input interface)
    指定了要處理來自哪個接口的數據包,這些數據包進入input、forward、prepoute鏈,不指定將處理進入所有接口的數據包
    例如:-i eth0 指定了要處理eth0進入的數據
    可以取反  !-i eth0,指eth0以外。
    可以匹配  -i eth+ 指以eth開頭的
  • 輸出接口 –o (out interface)
    數據包有那個接口輸出,類似於 –i
  • 源端口 –sport
    例如 –sport 22
    例如 –sport 22:100指定端口范圍
  • 目的端口 –dport
    類似於-sport
  • TCP標志
  • ICMP類型

5 實例分析

例如:接收目標端口為22的數據包

iptables –A INPUT –i etho –p tcp –dprot 22 –j ACCEPT

例如:拒絕所有其他數據包

iptables –A INPUT –j DROP

6 修改默認策略

上例僅對接收的數據包進行過濾,而對於要發出的數據包卻沒有任何限制。

使用iptables –L

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

http://xxxxxx/Linuxjc/1151175.html TechArticle

Copyright © Linux教程網 All Rights Reserved