Welcome 微信登录

首页 / 操作系统 / Linux / 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链中存在规则:
2345678910111213141516171819202122232425# iptables --listChain INPUT (policy ACCEPT)num  target   prot opt source             destination1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0 Chain FORWARD (policy ACCEPT)num  target   prot opt source             destination1    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             destination1    ACCEPT   all  --  0.0.0.0/0            0.0.0.0/02    ACCEPT   icmp --  0.0.0.0/0            0.0.0.0/0         icmp type 2553    ACCEPT   esp  --  0.0.0.0/0            0.0.0.0/04    ACCEPT   ah --  0.0.0.0/0            0.0.0.0/05    ACCEPT   udp  --  0.0.0.0/0            224.0.0.251       udp dpt:53536    ACCEPT   udp  --  0.0.0.0/0            0.0.0.0/0         udp dpt:6317    ACCEPT   tcp  --  0.0.0.0/0            0.0.0.0/0         tcp dpt:6318    ACCEPT   all  --  0.0.0.0/0            0.0.0.0/0         state RELATED,ESTABLISHED9    ACCEPT   tcp  --  0.0.0.0/0            0.0.0.0/0         state NEW tcp dpt:2210 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更多iptables相关教程见以下内容:CentOS 7.0关闭默认防火墙启用iptables防火墙  http://www.linuxidc.com/Linux/2015-05/117473.htmiptables使用范例详解 http://www.linuxidc.com/Linux/2014-03/99159.htmLinux防火墙iptables详细教程 http://www.linuxidc.com/Linux/2013-07/87045.htmiptables的备份、恢复及防火墙脚本的基本使用 http://www.linuxidc.com/Linux/2013-08/88535.htmLinux下防火墙iptables用法规则详解 http://www.linuxidc.com/Linux/2012-08/67952.htmLinux下iptables防火墙设置 http://www.linuxidc.com/Linux/2015-10/123843.htm本文永久更新链接地址