小知识:linux防火墙iptables规则的查看、添加、删除和修改方法总结

1、查看

iptables -nvL –line-number

-L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数

-n 不对ip地址进行反查,加上这个参数显示速度会快很多

-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口

–-line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

2、添加

添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。

当前规则:

?
1
2
3
4
5
6
[root@test ~]# iptables -nL –line-number
Chain INPUT (policy ACCEPT)
num target   prot opt source        destination
1  DROP    all — 192.168.1.1     0.0.0.0/0
2  DROP    all — 192.168.1.2     0.0.0.0/0
3  DROP    all — 192.168.1.4     0.0.0.0/0

添加一条规则到尾部:

?
1
[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一条规则到第三行,将行数直接写到规则链的后面:

?
1
[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

查看:

?
1
2
3
4
5
6
7
8
[root@test ~]# iptables -nL –line-number
Chain INPUT (policy ACCEPT)
num target   prot opt source        destination
1  DROP    all — 192.168.1.1     0.0.0.0/0
2  DROP    all — 192.168.1.2     0.0.0.0/0
3  DROP    all — 192.168.1.3     0.0.0.0/0
4  DROP    all — 192.168.1.4     0.0.0.0/0
5  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):

?
1
[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。

?
1
2
3
4
5
6
7
8
9
[root@test ~]# iptables -nv –line-number
iptables v1.4.7: no command specified
Try `iptables -h or iptables –help for more information.
[root@test ~]# iptables -nL –line-number
Chain INPUT (policy ACCEPT)
num target   prot opt source        destination
1  DROP    all — 192.168.1.1     0.0.0.0/0
2  DROP    all — 192.168.1.2     0.0.0.0/0
3  DROP    all — 192.168.1.3     0.0.0.0/0

删除第二行规则

?
1
[root@test ~]# iptables -D INPUT 2

4、修改

修改使用-R参数

先看下当前规则:

?
1
2
3
4
5
6
[root@test ~]# iptables -nL –line-number
Chain INPUT (policy ACCEPT)
num target   prot opt source        destination
1  DROP    all — 192.168.1.1     0.0.0.0/0
2  DROP    all — 192.168.1.2     0.0.0.0/0
3  DROP    all — 192.168.1.5     0.0.0.0/0

将第三条规则改为ACCEPT:

?
1
[root@test ~]# iptables -R INPUT 3 -j ACCEPT

再查看下:

?
1
2
3
4
5
6
[root@test ~]# iptables -nL –line-number
Chain INPUT (policy ACCEPT)
num target   prot opt source        destination
1  DROP    all — 192.168.1.1     0.0.0.0/0
2  DROP    all — 192.168.1.2     0.0.0.0/0
3  ACCEPT   all — 0.0.0.0/0      0.0.0.0/0

第三条规则的target已改为ACCEPT。

5、永久生效

service iptables save

service iptables restart

以上就是本文介绍的对linux防火墙iptables规则进行查看、添加、删除和修改的操作

原文链接:http://blog.csdn.net/whatday/article/details/50721777

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:nginx 反向代理之 proxy_pass的实现

2023-3-27 2:10:19

建站知识

小知识:详解Nginx proxy_pass的一个/斜杠引发的血案

2023-3-27 2:19:34

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索