iptables    连接netfilter的工具


3张表  

filter    过滤进出数据包   默认3个链input output forward

nat       网络地址转换     默认3个链prerouting postrouting output

mangle    给数据做标记,根据标记继续操作 默认5个链input output forward prerouting postrouting

常用的表和链有filter的input和output


iptables -nvL              查看filter表的规则,不指定-t默认为filter

iptables -t nat -nvL       查看nat表的规则

iptables -t mangle -nvL    查看mangle表的规则


iptables -Z                规则匹配计数器清零

iptables -F                清空filter表规则

service iptables save      保存iptables规则到配置文件/etc/sysconfig/iptables

iptables-save > 1.txt      规则另存到指定的文件

iptables-restore < 1.txt   通过备份文件,恢复规则


iptables -t filter -I INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP

-I插入规则到指定序号,不指定,默认最开始    

                      iptables -t filter -I INPUT 3 -p tcp --dport 80 -s 1.1.1.1 -j DROP

-A插入规则到最末尾    iptables -t filter -A INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP

-D删除指定的规则      iptables -t filter -D INPUT -p tcp --dport 80 -s 1.1.1.1 -j DROP

-j有3种动作:DROP直接丢弃    REJECT检查后拒绝       ACCEPT接受


注意:如果针对端口操作,必须指定协议类型tcp/udp,否则报错。


iptables -I INPUT -p tcp -s 1.1.1.1 -j ACCEPT   不指定端口,默认为所有端口。



iptables -P INPUT DROP     设置filter表INPUT链的默认策略为拒绝(总开关)


如果iptables规则太多,可用规则序号来删除:

iptables -nvL --line-number           显示filter表的规则序号

iptables -D INPUT 3                   删除filter表中序号是3的规则