如何在 CentOS 7 上从 FirewallD 迁移到 Iptables
介绍
与大多数其他 Linux 发行版一样,CentOS 7 在 Linux 内核中使用 netfilter
框架来访问流经网络堆栈的数据包。这提供了必要的接口来检查和操作数据包以实现防火墙系统。
大多数发行版使用 iptables
防火墙,它使用 netfilter
挂钩来执行防火墙规则。 CentOS 7 附带了一项名为 firewalld
的替代服务,它可以实现同样的目的。
虽然 firewalld
是一个非常强大的防火墙解决方案,具有强大的功能,但对于一些用户来说,如果他们熟悉 iptables
的语法并且对其行为感到满意,那么他们可能会更容易坚持使用 iptables
表现。 iptables
command 实际上是被 firewalld
自己使用的,但是 iptables
service 是默认情况下未安装在 CentOS 7 上。在本指南中,我们将演示如何在 CentOS 7 上安装 iptables
服务并将防火墙从 firewalld
迁移到 iptables
(查看此如果您想学习如何使用 FirewallD,请指导)。
保存您当前的防火墙规则(可选)
在切换到 iptables
作为服务器的防火墙解决方案之前,最好保存 firewalld
正在执行的当前规则。我们在上面提到 firewalld
守护进程实际上利用 iptables
命令与 netfilter
内核挂钩通信。因此,我们可以使用 iptables
命令转储当前规则。
通过键入以下命令将当前规则集转储到标准输出和主目录中名为 firewalld_iptables_rules
的文件:
- sudo iptables -S | tee ~/firewalld_iptables_rules
对 ip6tables
做同样的事情:
- sudo ip6tables -S | tee ~/firewalld_ip6tables_rules
根据处于活动状态的 firewalld
区域、启用的服务以及从 firewall-cmd
直接传递到 iptables
的规则,转储的规则集可能非常广泛。
firewalld
服务使用普通的 iptables
规则实施其防火墙策略。它通过使用 iptables
链构建管理框架来实现这一点。您可能会看到的大多数规则将用于创建这些管理链并引导进出这些结构的流量。
您最终转移到 iptables
服务的防火墙规则将不需要重新创建 firewalld
所依赖的管理框架。因此,您最终实施的规则集可能会简单得多。我们在这里保存整个集合,以便尽可能多地保持原始数据的完整性。
您可以看到一些更重要的行,以了解您必须通过键入以下内容重新创建的策略:
- grep 'ACCEPT\|DROP\|QUEUE\|RETURN\|REJECT\|LOG' ~/firewalld_iptables_rules
这将主要显示导致最终决定的规则。不会显示仅跳转到用户创建的链的规则。
下载并安装 Iptables 服务
要开始服务器的转换,您需要从 CentOS 存储库下载并安装 iptables-service
包。
通过键入以下内容下载并安装服务文件:
- sudo yum install iptables-services
这将下载并安装用于管理 iptables
服务的 systemd
脚本。它还会将一些默认的 iptables
和 ip6tables
配置文件写入 /etc/sysconfig
目录。
构建您的 Iptables 防火墙规则
接下来,您需要通过修改 /etc/sysconfig/iptables
和 /etc/sysconfig/ip6tables
文件来构建您的 iptables
防火墙规则。这些文件包含我们启动 iptables
服务时将读取和应用的规则。
如何构建防火墙规则取决于是否安装了 system-config-firewall
进程并用于管理这些文件。检查 /etc/sysconfig/iptables
文件的顶部,看看它是否建议不要手动编辑:
- sudo head -2 /etc/sysconfig/iptables
如果输出看起来像这样,请随意手动编辑 /etc/sysconfig/iptables
和 /etc/sysconfig/ip6tables
文件以实施您的 策略iptables
防火墙:
output# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
使用 sudo
权限打开并编辑文件以添加您的规则:
- sudo nano /etc/sysconfig/iptables
- sudo nano /etc/sysconfig/ip6tables
制定规则后,您可以使用以下命令测试 IPv4 和 IPv6 规则:
- sudo sh -c 'iptables-restore -t < /etc/sysconfig/iptables'
- sudo sh -c 'ip6tables-restore -t < /etc/sysconfig/ip6tables'
另一方面,如果检查 /etc/sysconfig/iptables
文件的输出如下所示,则不应手动编辑该文件:
output# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
这意味着 system-config-firewall
管理工具已安装并用于管理此文件。任何手动更改都将被该工具覆盖。如果您看到这种情况,您应该使用相关工具之一对您的防火墙进行更改。对于文本 UI,键入:
- sudo system-config-firewall-tui
如果您安装了图形用户界面,则可以通过键入以下命令启动它:
- sudo system-config-firewall
如果您在学习 iptables
规则和语法方面需要一些帮助,以下指南可能会有所帮助,尽管它们主要针对 Ubuntu 系统:
- 如何在 Ubuntu 14.04 上使用 Iptables 设置防火墙
- Iptables Essentials:常用防火墙规则和命令
- 如何在 Ubuntu 14.04 上使用 Iptables 实施基本防火墙模板
停止FirewallD服务并启动Iptables服务
接下来,我们需要停止当前的 firewalld
防火墙并启动我们的 iptables
服务。一旦 firewalld
服务成功关闭,我们将使用 &&
构造来启动新的防火墙服务:
- sudo systemctl stop firewalld && sudo systemctl start iptables; sudo systemctl start ip6tables
您可以通过键入以下内容来验证 firewalld
是否未运行:
- sudo firewall-cmd --state
您还可以看到您在 /etc/sysconfig
目录中设置的规则已被加载并通过键入以下内容应用:
- sudo iptables -S
- sudo ip6tables -S
此时,iptables
和 ip6tables
服务在当前会话中处于活动状态。但是,目前,firewalld
服务仍然是服务器重启时自动启动的服务。
这是测试防火墙策略以确保您具有所需访问级别的最佳时间,因为如果出现任何问题,您可以重新启动服务器以恢复到旧防火墙。
禁用 FirewallD 服务并启用 Iptables 服务
在测试您的防火墙规则以确保正确执行您的策略之后,您可以继续并通过键入以下内容来禁用 firewalld
服务:
- sudo systemctl disable firewalld
这将防止服务在启动时自动启动。由于 firewalld
服务不应在 iptables
服务运行时手动启动,因此您可以通过屏蔽该服务来采取额外的步骤。这也将阻止 firewalld
服务被手动启动:
- sudo systemctl mask firewalld
现在,您可以启用 iptables
和 ip6tables
服务,以便它们在开机时自动启动:
- sudo systemctl enable iptables
- sudo systemctl enable ip6tables
这应该完成您的防火墙转换。
结论
实施防火墙是确保服务器安全的重要步骤。虽然 firewalld
是一个很好的防火墙解决方案,但有时使用最熟悉的工具或在更多样化的基础设施中使用相同的系统是最有意义的。