参考:http://www.zsythink.net/archives/1517
有两台测试机
zk02 192.168.27.152
zk03 192.168.27.153
目录
1、增加规则
2、追加规则
1、增加规则
首先看一条命令,表示如果报文来自192.168.27.153 ,则将报文进行丢弃
iptables -t filter -I INPUT -s 192.168.27.153 -j DROP
解读:
"-t" 指定操作fitler 表,不使用-t 选项指定表时,默认操作filter 表
"-I" 指定将规则插入到哪个链中,-I表示insert ,即插入的意思,所以-I INPUT 表示将规则插入到INUT链中,即添加规则之意.
"-s" 指定匹配条件中的 "源地址",即如果报文的源地址属于-s 对应的地址,那么报文则满足匹配的条件,-s 表示source ,表示源地址.
"-j" 指定当匹配条件被满足时,所对应的动作,上例中指定的动作为DROP.
再次查看
[root@zk02 ~]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 186 packets, 13147 bytes)
pkts bytes target prot opt in out source destination
60 6140 DROP all -- * * 192.168.27.153 0.0.0.0/0
在153 机子ping zk02 发现,ping 发送的报文没有被152 (zk02) 机子接受.
2、追加规则
[root@zk02 ~]# iptables -t filter -A INPUT -s 192.168.27.153 -j ACCEPT
使用-A 选项,表示在对应链中追加规则,-A 即 append 之意,所以-A INPUT 表示在INPUT链中追加规则,而之前示例中使用的-I选项则表示在链中"插入规则" ,它们都是本意都是添加一条规则,-A 表示在链的尾部追加规则,-I 表示在链的首部添加规则.
Chain INPUT (policy ACCEPT 17 packets, 1192 bytes)
pkts bytes target prot opt in out source destination
194 17396 DROP all -- * * 192.168.27.153 0.0.0.0/0
0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
再次在zk03 上ping zk02 发现 ,ping 还是没有反应.说明第二条添加的规则并没有生效.
使用-I 添加一个ACCPET规则
iptables -t filter -I INPUT -s 192.168.27.153 -j ACCEPT
[root@zk02 ~]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 14 packets, 1097 bytes)
pkts bytes target prot opt in out source destination
6 504 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
402 34868 DROP all -- * * 192.168.27.153 0.0.0.0/0
0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
可以发现ping 已经通了.
从上面可以看出,规则的顺序很重要.
如果报文已经被前面的规则匹配到,iptables 则会对报文执行对应的动作,即使后面的动作也能匹配到当前的报文,很有可能也没机会再对报文执行响应的动作了。
使用--line-numbers 列出规则的序号
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 121 packets, 8835 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
2 402 34868 DROP all -- * * 192.168.27.153 0.0.0.0/0
3 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
我们也可以在添加规则时,指定规则的编号,这样我们就能在任意位置插入规则了, 后面的规则序号往后移动,如:
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 12 packets, 792 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
2 0 0 ACCEPT all -- * * 192.168.27.188 0.0.0.0/0
3 402 34868 DROP all -- * * 192.168.27.153 0.0.0.0/0
4 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
3、删除规则
有两种方式
a、根据规则的编号删除规则
b、根据具体的匹配条件与动作删除规则
首先看一下filter 链中的INPUT规则
root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 12 packets, 792 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
2 0 0 ACCEPT all -- * * 192.168.27.188 0.0.0.0/0
3 402 34868 DROP all -- * * 192.168.27.153 0.0.0.0/0
4 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
删除上图中的第2条规则 ,-D 表示删除指令链中的某条规则
[root@zk02 ~]# iptables -t filter -D INPUT 2
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 6 packets, 396 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
2 402 34868 DROP all -- * * 192.168.27.153 0.0.0.0/0
3 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
下面根据匹配的条件与动作删除规则,删除 源地址 192.168.27.153 ,动作为DROP的规则 ,命令如下:
[root@zk02 ~]# iptables -t filter -D INPUT -s 192.168.27.153 -j DROP
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 12 packets, 792 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
2 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
删除对应的规则时,仍然使用-D选项,剩下的规则选项与添加规则时一样.
删除指定表中某条链的所有规则
iptables -t 表名 -F 链名
-F为flush 的意思,即冲刷指定的链,删除指定链中的所有规则,但是请注意,此操作属于删除操作,在没有保存iptables 规则情况下,请慎用.
其实,-F 不仅能清空指令链上的规则,其实它还能清空指定表上所有链的规则,不指定链名,只指定表名即可删除表的所有规则,命令如下:
iptables -t 表名 -F
注意: 在没有保存iptables 规则时,请勿随便清空或者表中的规则 ,除非你明白你在干什么.
4、修改规则
将如下规则中的DROP 改为REJECT
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 12 packets, 792 bytes)
num pkts bytes target prot opt in out source destination
1 288 24192 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
使用-R 选项指定链中的规则,在修改规则时指定链中的编号即可.
[root@zk02 ~]# iptables -t filter -R INPUT 1 -s 192.168.27.153 -j REJECT
[root@zk02 ~]# iptables --line-numbers -nvL INPUT
Chain INPUT (policy ACCEPT 24 packets, 1584 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 192.168.27.153 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 ACCEPT all -- * * 192.168.27.153 0.0.0.0/0
如果不使用-s 指定源地址,你修改规则中的源地址会变为0.0.0.0/0 (此IP表示匹配所有网段的ip地址)
修改默认规则:
每张表的每条链中,都有自己的默认策略,我们也可以理解为默认动作.
当报文没有被链中的任何规则匹配到时,或者,当链中没有任何规则时,防火墙会按照默认动作处理报文,我们可以修改制定链的默认策略,使用如下命令即可.
iptables -t filter -P INPUT DROP
5、保存规则
在默认情况下,我们对防火墙所做的策略都是临时的,换句话说就是,当重启iptables 服务或者重启服务器后,我们平常添加的规则或者对规则所做的修改都将消失,为了防止这种情况的发生,我们需要将规则"保存".
service iptables save
注意:centos7 默认使用firewalld ,如果想使用上述命令保存规则,需要安装iptables-service
或者使用如下方法保存规则
iptables-save > /etc/sysconfig/iptables
也可以使用如下命令从指定的文件载入规则,注意:重载规则时,文件中的规则将会覆盖现有规则
iptables-restore < /etc/sysconfig/iptables