前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Firewalld 1

Firewalld 1

作者头像
franket
发布2021-08-10 16:18:49
3240
发布2021-08-10 16:18:49
举报
文章被收录于专栏:技术杂记技术杂记

前言

Firewalld 是一个本地的网络策略管理软件,与 iptables 启相同的作用

Firewalld provides a dynamically managed firewall with support for network/firewall zones that define the trust level of network connections or interfaces. It has support for IPv4, IPv6 firewall settings, ethernet bridges and IP sets. There is a separation of runtime and permanent configuration options. It also provides an interface for services or applications to add firewall rules directly

RHEL7/Centos7 开始,系统默认的防火墙软件从 iptables 替换成了 firewalld

这的确是一个不小的变化,从而让很多以前熟悉 iptables 的运维小伙伴一下子变得无所适从

相对于 iptables, firewalld 的确有一些更先进的管理理念,不过也在挑战传统的运维体验

这里就 firewalld 的框架和简单的概念进行一个讲解和介绍

参考 A service daemon with D-Bus interfaceUSING FIREWALLS

Documentation

Tip: 当前最新的版本为 firewalld 0.6.0 release


基础

特性

相对于传统的 iptables, firewalld 拥有很多优势,这些优势由其具备的特性而衍生出来的

那这里列举一下它的特性:

  • 完整的 D-Bus API
  • 支持 IPv4, IPv6, bridge 和 ipset
  • 支持 IPv4 和 IPv6 NAT
  • 支持防火墙区域划分
  • 可以预定义 zones, services and icmptypes
  • zones 可以直接处理 Simple service, port, protocol, source port, masquerading, port forwarding, icmp filter, rich rule, interface and source address
  • 通过 ports, protocols, source ports, modules (netfilter helpers) and destination address 可以定义服务
  • zone 里可以通过 Rich Language 来定义灵活和丰富的规则
  • zone 中可以定义基于时间的防火墙规则
  • 拒绝的流量可以通过日志记录
  • 支持直接接口
  • Lockdown 特性可以让加入白名单的应用来调整防火墙规则
  • 可以自动加载 Linux 内核模块
  • 与 Puppet 进行了整合和集成
  • CLI 可以进行在线和离线的配置
  • 可以使用 gtk3 来进行图形化配置
  • 支持 Qt4 的应用程序

系统层次结构

firewalld
firewalld

与 iptables 一比较,就显得比较清晰

system-config-firewallD-Bus/firewall-config/firewall-cmd 属于同一层面

它们都是用于网络规则配置管理的客户端

iptables(service)firewalld(deamon and service) 属于同一层面

它们都是用于网络规则配置管理的服务端

iptables(command) 是与内核通讯的接口

主要实施规则的是 netfilter 的 Linux 内核模块

由此得出 firewalld 与 iptables 服务,两者只能取其一,因为两者是互相替代的关系,不能共同运行

Tip: firewalld 与 iptables 的关系,就好比 postfix 与 sendmail 的关系

Note: firewalld 是 iptables 命令的一个封装,可以让人更容易地管理 iptables 规则, 它并不是 iptables 命令的替代品,虽然 iptables 命令仍可用于 firewalld,但建议使用 firewalld 时仅使用 firewalld 命令

软件架构

firewalld
firewalld

firewall-cmd/firewall-config/firewall-applet/firewall-offline-cmd 属于 firewalld 的生态环境

用于对 firewalld 的网络规则进行配置

其中 firewall-cmd/firewall-config/firewall-applet 是通过 D-bus 来对 firewalld 进行配置

可以同时实现 runtime 和 permanent 的配置

但是 firewall-offline-cmd 并不直接与 firewalld 实例进行通讯,而是直接修改 firewalld 配置文件,然后应用配置文件,来实现间接的控制,这个方法有很多弊端,firewalld 的动态配置特性完全无法发挥作用, 并且还有延时生效的副作用,所以不建议以此方式来配置 firewalld

The firewalld D-Bus interface is the primary way to alter and create the firewall configuration. The interface is used by all firewalld provided online tools, like for example firewall-cmd, firewall-config and firewall-applet. firewall-offline-cmd is not talking to firewalld, but altering and creating firewalld configuration files directly using the firewalld core with the IO handlers. firewall-offline-cmd can be used while firewalld is running, but it is not recommended as it is only able to alter the permanent configuration that are visible in firewall after about five seconds

两层结构

firewalld 分成两层结构

  • D-Bus
  • Core

D-Bus 提供一个抽象的接口层,用于与外层的客户端软件进行通讯,并且修改 firewalld 行为

Core 又可以分为两层

  • 模型的实例层
  • 后端支持层

模型的实例层用于管理配置文件,和根据配置文件来初始化模型实例,用于将 DSL 与 网络规则进行互化

后端支持层的职能是调用基础工具来实现模型所表达的网络规则,也可以理解为调用基础工具或者基础网络库来与内核中的模块通讯,以支持和实现 DSL 所表达的网络策略

这是应用层到内核层的边界

Note: firewalld 并不是强依赖 NetworkManager,但是还是建议使用 NetworkManager,因为任何网络变更都会被 NetworkManger 及时通知到 firewalld,这样 firewalld 就可实时了解到真实的网络环境,如果不使用 NetworkManager 也不影响 firewalld 的已有配置与规则,但是如果网络发生了临时性的变更,那么 firewalld 并不会自动有感知的,于是之前的配置很可能就会不符合当前的新的网络环境,于是产生一些意想不到的网络表现(由于规则与实情不匹配导致的)

Firewalld 与 Iptables 的配置区别

iptables 在 /etc/sysconfig/iptables 和 /etc/sysconfig/ip6tables 中以文本的形式存放配置文件

firewalld 在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中以 XML 的形式存放配置文件

iptables 中的服务,每条记录的调整都意味着所有的老记录的刷新和对配置文件 /etc/sysconfig/iptables 的重读

firewalld 却不必重建所有的规则,只有变更的部分进行了应用,因此 firewalld 可以在运行时调整配置而不会让已经存在的链接丢失或者断开

firewalld 使用区域和服务而不是链式规则

但是两者都是使用的 iptables 工具与内核进行通讯的

Note: firewalld 是 iptables 命令的一个封装,可以让人更容易地管理 iptables 规则, 它并不是 iptables 命令的替代品,虽然 iptables 命令仍可用于 firewalld,但建议使用 firewalld 时仅使用 firewalld 命令

替换 firewalld 成 iptables

如果上面的一顿解释成功的把看官搞蒙逼了,那么这边有一个杀手锏

就是放弃挣扎,退回到温暖而熟悉的 iptables 吧

下面的所有命令都要使用 root 的权限执行

禁用 firewalld

代码语言:javascript
复制
systemctl disable firewalld
systemctl stop firewalld

安装 iptables-services

代码语言:javascript
复制
yum install iptables-services

启动 iptables

代码语言:javascript
复制
systemctl start iptables
systemctl start ip6tables

自动开机启动

代码语言:javascript
复制
systemctl enable iptables
systemctl enable ip6tables

总结

firewalld 毕竟是一个更为先进的 firewall 配置软件

对于防火墙的管理有一套更为强大和灵活的管理方法 

但是有一定的学习成本,影响简单直接的 iptables 运维体验

但是走出舒适区,进行技能的更新和迭代也是一个合格运维的基础素质

后面再讲讲 firewalld 的一些基础概念

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 基础
    • 特性
      • 系统层次结构
        • 软件架构
          • 两层结构
        • Firewalld 与 Iptables 的配置区别
          • 替换 firewalld 成 iptables
            • 禁用 firewalld
            • 安装 iptables-services
            • 启动 iptables
            • 自动开机启动
        • 总结
        相关产品与服务
        命令行工具
        腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档