找回密码
 立即注册
搜索
热搜: 生活 中国

iptables规则的查看、添加、删除和修改

[复制链接]
admin 发表于 2016-9-8 12:58:38 | 显示全部楼层 |阅读模式
这里只列出比较常用的参数,详细的man iptables
1、查看
iptables -nvL --line-number
  • -L     查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
  • -n     不对ip地址进行查,加上这个参数显示速度会快很多
  • -v     输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
  • –line-number     显示规则的序列号,这个参数在删除或修改规则时会用到

2、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部
例如:
当前规则:
[root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.4          0.0.0.0/0添加一条规则到尾部:
[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP再插入一条规则到第三行:
[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP查看:
[root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.3          0.0.0.0/04    DROP       all  --  192.168.1.4          0.0.0.0/05    DROP       all  --  192.168.1.5          0.0.0.0/0可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。
3、删除
删除用-D参数
删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):
[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP有时候有些规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number查看出该条规则的行号,再通过行号删除
[root@test ~]# iptables -nv --line-numberiptables v1.4.7: no command specifiedTry `iptables -h' or 'iptables --help' for more information.[root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.3          0.0.0.0/0删除第二行规则
[root@test ~]# iptables -D INPUT 24、修改
修改使用-R参数
将第三行规则改为ACCEPT
先看下当前规则:
[root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    DROP       all  --  192.168.1.5          0.0.0.0/0修改:
[root@test ~]# iptables -R INPUT 3 -j ACCEPT再查看下:
[root@test ~]# iptables -nL --line-numberChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    DROP       all  --  192.168.1.1          0.0.0.0/02    DROP       all  --  192.168.1.2          0.0.0.0/03    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生活导航|生活导航 ( 新ICP备12003026-1号 )

GMT+8, 2024-4-26 07:31 , Processed in 0.082725 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表