CentOS 7 系统中,防火墙要怎么使用?

CentOS 7 系统中,默认有两个防火墙管理工具:

  1. firewalld (默认的动态防火墙,基于 D-Bus 管理)

  2. iptables (传统的静态防火墙,底层由 netfilter 提供支持)

是否需要关闭这两个防火墙,取决于你的具体需求。以下是详细分析和操作指南:

1. 两个防火墙的关系

  • firewalld 是 CentOS 7 的默认前端管理工具,底层仍然依赖 iptables/nftables 规则。

  • iptables 是直接操作内核级防火墙规则的传统工具。

  • 两者同时启用可能导致规则冲突 (例如端口开放不一致),但通常 firewalld 会管理 iptables 规则。

2. 是否需要关闭?

场景 建议操作
使用 firewalld 保持 firewalld 开启,无需关闭 iptablesfirewalld 会自动管理规则)。
使用 iptables 关闭 firewalld,直接通过 iptables 配置规则(避免冲突)。
完全禁用防火墙 关闭 firewalld 并清空 iptables 规则(仅限测试环境,生产环境不推荐!)。

3. 操作步骤

(1)查看防火墙状态
复制代码
systemctl status firewalld    # 检查 firewalld 状态
systemctl status iptables     # 检查 iptables 服务(通常未启用)
iptables -L -n                # 查看 iptables 规则
(2)关闭 firewalld
复制代码
sudo systemctl stop firewalld      # 临时停止
sudo systemctl disable firewalld   # 永久禁用
(3)关闭 iptables(如需)
复制代码
sudo systemctl stop iptables       # 临时停止
sudo systemctl disable iptables    # 永久禁用
sudo iptables -F                  # 清空所有规则(谨慎操作!)
(4)启用 iptables(替代 firewalld
复制代码
sudo yum install iptables-services  # 确保 iptables 服务已安装
sudo systemctl enable iptables      # 启用服务
sudo systemctl start iptables       # 启动服务

4. 注意事项

  1. 生产环境建议保留防火墙

    • 使用 firewalld(推荐)或 iptables 至少开启一个,避免暴露所有端口。

    • 通过 firewall-cmdiptables 命令配置放行必要端口(如 SSH、HTTP)。

  2. firewalldiptables 冲突

    • 如果同时修改两者,可能导致规则混乱。建议统一使用一种工具。
  3. 临时测试关闭防火墙

    复制代码
    sudo systemctl stop firewalld
    sudo iptables -F

5. 推荐方案

  • 大多数情况 :保持 firewalld 开启,通过以下命令管理:

    复制代码
    sudo firewall-cmd --zone=public --add-port=80/tcp --permanent  # 放行端口
    sudo firewall-cmd --reload                                    # 重载配置
  • 需要传统 iptables :禁用 firewalld,手动编写 /etc/sysconfig/iptables 规则。


总结

需求 操作
默认安全防护 保持 firewalld 开启,无需操作 iptables
使用自定义规则 关闭 firewalld,启用 iptables-services 并手动配置。
彻底关闭防火墙 停止并禁用 firewalld + 清空 iptables 规则(仅限内网测试环境)。
相关推荐
松涛和鸣13 分钟前
72、IMX6ULL驱动实战:设备树(DTS/DTB)+ GPIO子系统+Platform总线
linux·服务器·arm开发·数据库·单片机
野指针YZZ19 分钟前
一键配置RK3588网络与SSH远程连接
网络·ssh·rk3588
简单中的复杂39 分钟前
【避坑指南】RK3576 Linux SDK 编译:解决 Buildroot 卡死在 host-gcc-final 的终极方案
linux·嵌入式硬件
迎仔44 分钟前
10-网络安全监控与事件响应:数字世界的智能监控与应急系统
网络·安全·web安全
wVelpro1 小时前
如何在Pycharm 2025.3 版本实现虚拟环境“Make available to all projects”
linux·ide·pycharm
上海合宙LuatOS1 小时前
LuatOS核心库API——【audio 】
java·网络·单片机·嵌入式硬件·物联网·音视频·硬件工程
程序员老舅2 小时前
C++高并发精髓:无锁队列深度解析
linux·c++·内存管理·c/c++·原子操作·无锁队列
雨中风华2 小时前
Linux, macOS系统实现远程目录访问(等同于windows平台xFsRedir软件的目录重定向)
linux·windows·macos
Yeats_Liao2 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
深圳市恒星物联科技有限公司2 小时前
水质流量监测仪:复合指标监测的管网智能感知设备
大数据·网络·人工智能