增
#注意点:添加规则时,规则的顺序非常重要
#在指定表的指定链的尾部添加一条规则,
#-A选项表示在对应链的末尾添加规则,
#省略-t选项时,表示默认操作filter表中的规则:
#命令语法:iptables -t 表名 -A 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -A INPUT -s 192.168.1.146 -j DROP
#命令语法:iptables -t 表名 -A 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -A INPUT -s 192.168.1.146 -j DROP
#在指定表的指定链的首部添加一条规则,-I选型表示在对应链的开头添加规则:
#命令语法:iptables -t 表名 -I 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -I INPUT -s 192.168.1.146 -j ACCEPT
#命令语法:iptables -t 表名 -I 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -I INPUT -s 192.168.1.146 -j ACCEPT
#在指定表的指定链的指定位置添加一条规则:
#命令语法:iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作
root@devENV:~#iptables -t filter -I INPUT 5 -s 192.168.1.146 -j REJECT
#命令语法:iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作
root@devENV:~#iptables -t filter -I INPUT 5 -s 192.168.1.146 -j REJECT
#设置指定表的指定链的默认策略(默认动作),并非添加规则:
#命令语法:iptables -t 表名 -P 链名 动作
root@devENV:~#iptables -t filter -P FORWARD ACCEPT
#命令语法:iptables -t 表名 -P 链名 动作
root@devENV:~#iptables -t filter -P FORWARD ACCEPT
#上例表示将filter表中FORWARD链的默认策略设置为ACCEPT
删
批量全删
#命令语法:iptables -t 表名 -F
root@devENV:~#iptables -t filter -F
单独删
#注意点:如果没有保存规则,删除规则时请慎重
#按照规则序号删除规则,删除指定表的指定链的指定规则,-D选项表示删除对应链中的规则:
#命令语法:iptables -t 表名 -D 链名 规则序号
root@devENV:~#iptables -t filter -D INPUT 3
#命令语法:iptables -t 表名 -D 链名 规则序号
root@devENV:~#iptables -t filter -D INPUT 3
#上述示例表示删除filter表中INPUT链中序号为3的规则。
#按照具体的匹配条件与动作删除规则,删除指定表的指定链的指定规则:
#命令语法:iptables -t 表名 -D 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -D INPUT -s 192.168.1.146 -j DROP
#命令语法:iptables -t 表名 -D 链名 匹配条件 -j 动作
root@devENV:~#iptables -t filter -D INPUT -s 192.168.1.146 -j DROP
#上述示例表示删除filter表中INPUT链中源地址为192.168.1.146并且动作为DROP的规则。
改
#注意点:如果使用-R选项修改规则中的动作,那么必须指明原规则中的原匹配条件,
#例如源IP,目标IP等。
#命令语法:iptables -t 表名 -R 链名 规则序号 规则原本的匹配条件 -j 动作
root@devENV:~#iptables -t filter -R INPUT 3 -s 192.168.1.146 -j ACCEPT
#命令语法:iptables -t 表名 -R 链名 规则序号 规则原本的匹配条件 -j 动作
root@devENV:~#iptables -t filter -R INPUT 3 -s 192.168.1.146 -j ACCEPT
#上述示例表示修改filter表中INPUT链的第3条规则,将这条规则的动作修改为ACCEPT,
#-s 192.168.1.146为这条规则中原本的匹配条件,如果省略此匹配条件,
#修改后的规则中的源地址可能会变为0.0.0.0/0。
查
root@devENV:~#iptables --line -nvxL OUTPUT
保存到文件
root@devENV:~#iptables-save > /etc/sysconfig/iptables
从文件恢复
root@devENV:~#iptables-restore < /etc/sysconfig/iptables