服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - linux防火墙iptables规则的查看、添加、删除和修改方法总结

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

2022-03-03 17:24whatday Linux

本文介绍了如何对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

延伸 · 阅读

精彩推荐
  • LinuxLinux rm 命令误删文件的恢复方法

    Linux rm 命令误删文件的恢复方法

    新手用 Linux 命令行还是比较危险的,很容易在移动或者重命名文件时将 mv 写错程 rm,那样可就惨了,文件被误删之后怎么恢复呢 ...

    Linux教程网7632019-12-27
  • Linux玩转Linux文件描述符和重定向

    玩转Linux文件描述符和重定向

    本文介绍linux中文件描述符与重定向的相关知识,文件描述符是与文件输入、输出相关联的整数,它们用来跟踪已打开的文件。有需要的朋友参考下 ...

    Linux教程网4742019-11-23
  • LinuxLinux 初始化MySQL 数据库报错解决办法

    Linux 初始化MySQL 数据库报错解决办法

    这篇文章主要介绍了Linux 初始化MySQL 数据库报错解决办法的相关资料,需要的朋友可以参考下...

    Linux教程网7532022-01-17
  • LinuxFedora 和红帽 Linux:你应该使用哪个,为什么?

    Fedora 和红帽 Linux:你应该使用哪个,为什么?

    Fedora 和红帽 Linux。这两个 Linux 发行版都属于同一个组织,都使用 RPM 包管理器,都提供桌面版和服务器版。这两个 Linux 发行版对操作系统世界都有较大的...

    Linux中国6442021-05-10
  • Linux详解Linux系统中的usr目录

    详解Linux系统中的usr目录

    这篇文章主要介绍了Linux系统中的usr目录,文中列举了usr目录下一些重要子目录所包含文件的功能,需要的朋友可以参考下...

    一介布衣10262019-06-18
  • Linuxlinux系统下安装dovecot方法

    linux系统下安装dovecot方法

    Dovecot 是一个开源的 IMAP 和 POP3 邮件服务器,支持 Linux/Unix 系统。本文主要为大家介绍关于linux系统下安装dovecot的方法...

    linux之家5952019-06-12
  • LinuxSlackware Linux init 进程

    Slackware Linux init 进程

    Joe Brockmeier 研究了 Slackware Linux init 进程。他讨论了系统如何初始化服务、各种运行级别是什么,以及如何从缺省安装中添加或除去服务来定制系统。 ...

    Linux之家4752020-04-25
  • LinuxSSH密钥登录Linux系统服务器的设置方法

    SSH密钥登录Linux系统服务器的设置方法

    这篇文章主要介绍了SSH密钥登录Linux系统服务器的设置方法,同时附送关闭SSH密码登录的方法~需要的朋友可以参考下...

    开源中文社区4422019-09-08