跳至内容

启用 iptables 防火墙

先决条件

  • 一种对禁用默认的 *firewalld* 应用程序并启用 *iptables* 的渴望。

此过程已弃用

从 Rocky Linux 9.0 开始,iptables 及其所有关联的实用程序均已弃用。这意味着将来的操作系统版本将删除 iptables。因此,强烈建议您不要使用此过程。如果您熟悉 iptables,我们建议您使用 iptables 指南到 firewalld。如果您不熟悉防火墙概念,我们建议您使用 firewalld 初学者指南.

简介

firewalld 现在是 Rocky Linux 上的默认防火墙。firewalld 曾经只是在 CentOS 7/RHEL 7 中使用 xml 文件动态应用 *iptables*,在不刷新规则的情况下加载更改。在 CentOS 8/RHEL 8/Rocky 8 中,firewalld 现在是 *nftables* 的包装器。但是,如果愿意,您仍然可以安装和使用直接的 *iptables*。要安装和运行不带 *firewalld* 的直接 *iptables*,您可以按照本指南进行操作。本指南不会说明如何为 *iptables* 编写规则。假设如果您想摆脱 *firewalld*,您一定已经知道如何为 *iptables* 编写规则。

禁用 firewalld

您实际上不能将旧的 *iptables* 实用程序与 *firewalld* 同时运行。它们根本不兼容。解决此问题的最佳方法是完全禁用 *firewalld*(除非您想卸载,否则无需卸载它),然后重新安装 *iptables* 实用程序。可以使用以下命令禁用 *firewalld*:

停止 *firewalld*

systemctl stop firewalld

禁用 *firewalld*,使其在启动时不会启动

systemctl disable firewalld

屏蔽该服务,使其无法被找到

systemctl mask firewalld

安装和启用 iptables 服务

接下来,我们需要安装旧的 *iptables* 服务和实用程序。这可以使用以下方法完成:

dnf install iptables-services iptables-utils

这将安装运行直接 *iptables* 规则集所需的一切。

现在我们需要启用 *iptables* 服务以确保它在启动时启动

systemctl enable iptables

结论

如果您更喜欢 *iptables* 而不是 *firewalld*,您可以恢复使用直接的 *iptables*。只需逆转这些更改即可恢复使用默认的 *firewalld*。

作者:史蒂文·斯宾塞

贡献者:埃塞奎尔·布鲁尼,甘娜·吉尔诺娃