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
更多iptables相关教程见以下内容: