歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Iptables防火牆 基礎知識

Iptables防火牆 基礎知識

日期:2017/2/28 13:49:48   编辑:Linux教程

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鏈中存在規則:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # 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

Copyright © Linux教程網 All Rights Reserved