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