netfilter---->iptables(工具)


查看指定filter表的规则 -nvL(默认是filter表)

[root@wy ~]# iptables -t filter -nvL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination


另外两个表nat、mangle,但最常用的是filter。

filter主要用来过滤包(进包、出包),作一些规则或限制。


添加一条规则

[root@wy ~]# iptables -t filter -I INPUT -p tcp  --dport 80 -s 12.12.12.12 -j REJECT

[root@wy ~]# iptables -nvL

Chain INPUT (policy ACCEPT 36 packets, 2592 bytes)

pkts bytes target     prot opt in     out     source               destination

0     0 REJECT     tcp  --  *      *       12.12.12.12          0.0.0.0/0           tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 19 packets, 1992 bytes)

pkts bytes target     prot opt in     out     source               destination

说明:添加规则也可以用-A ,但它们的区别是-A是添加在所有规则的后面,-I是添加在最前面。

         -I插入的规则比-A增加的规则优先生效


删除刚添加的规则

[root@wy ~]# iptables -t filter -D INPUT -p tcp  --dport 80 -s 12.12.12.12 -j REJECT


还有另外一种删除规则方法:

iptables -nvL --line-numbers先显示规则序号,然后iptables -D INPUT/OUTPUT 序号就可以了


清空计数器-Z

[root@wy ~]# iptables -Z

说明:也就是包和字节都变成了0


清空全部规则-F

[root@wy ~]# iptables -F


注:若指定port的时候要加上协议,否则会报错。


保存规则

[root@wy ~]# service iptables save


查看保存的规则

[root@wy ~]# cat /etc/sysconfig/iptables


备份与恢复

[root@wy ~]# iptables-save > 1.ipt

[root@wy ~]# iptables -F         ###恢复前先清空

[root@wy ~]# iptables-restore < 1.ipt


filter                主要用来限制进入本机的包和出去的包

nat                  主要网络地址转换

mangle                主要给数据包打标记


修改策略

[root@wy ~]# iptables -P INPUT ACCEPT      #针对INPUT链


INPUT链实例:要求把预设策略设成DROP,其他两个是ACCEPT,针对192.168.0.0/24,22端口开放,对所有的Ip的80端口放行,所有的网段21端口放行。

[root@wy ~]  vim 1.ipt.sh

#! /bin/bash

ipt="/sbin/iptables"

$ipt -F

$ipt -P INPUT DROP

$ipt -P OUTPUT ACCEPT

$ipt -P FORWARD ACCEPT

$ipt -A INPUT -s 192.168.0.0./24 -p tcp --dport 22 -j ACCEPT

$ipt -A INPUT -p tcp --dport 80 -j ACCEPT

$ipt -A INPUT -p tcp --dport 21 -j ACCEPT

iptables_iptables

执行脚本

[root@wy ~]  sh  1.ipt.sh