netfilter、iptables、firewalld 的关系、区别

本文介绍了Linux内核中的netfilter框架及其提供的数据包过滤和NAT功能,并回顾了其发展历程。此外,还详细解释了firewalld作为现代Linux系统默认防火墙的作用及其与netfilter的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • netfilter 是 Linux 内核的一个网络框架,提供了数据包过滤(包括无状态包、有状态包)、NAT 等功能。

  • netfilter 工作在内核态,提供了以下命令供用户配置防火墙规则:

    • iptables :用于过滤 IPv4 协议的数据包。
    • ip6tables :用于过滤 IPv6 协议的数据包。
    • arptables
    • ebtables
  • 相关历史:

    • 1998 年,Rusty Russell 创建了 netfilter 项目,旨在改进 Linux 防火墙软件。
    • 2000 年,Linux 2.4 内核发布,加入了 netfilter ,后来成为了很多 Linux 发行版的默认防火墙。
    • 2008 年,netfilter 开发团队公布了 nftables 项目,它是一个新的包过滤框架,提供了一个新命令 nft 来取代 iptables、ip6tables 等命令。
    • 2014 年,Linux 3.13 内核发布,加入了 nftables 。
  • firewalld 是 Red Hat 公司开发的防火墙软件。

    • 于 2011 年发布,采用 Python 开发。
    • 默认后端为 nftables ,也兼容 netfilter 。
    • 在 RHEL 7 之前的 RHEL 发行版中,默认使用 iptables 命令来管理防火墙。
    • 在 RHEL 7 发行版中, firewalld 取代 iptables 成为了默认防火墙软件。
      • 用户可以同时使用 iptables 和 firewalld 两个防火墙软件,但容易搞混规则。建议将一个防火墙关闭或默认允许所有流量,只使用另一个防火墙。
iptablesfirewalld 都是 Linux 系统中的防火墙软件,它们的主要区别如下: 1. 配置文件位置不同:iptables 的配置文件在 /etc/sysconfig/iptables 中,而 firewalld 的配置文件在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件中。 2. 配置方式不同:iptables 仅能通过命令行进行配置,而 firewalld 提供了图形接口,类似于 Windows 防火墙的操作方式。 3. 工作原理不同:iptables 是基于内核的防火墙,它通过在内核中设置规则来过滤网络流量;而 firewalld 是基于用户空间的防火墙,它使用 D-Bus 与内核通信,并通过内核中的 netfilter 过滤网络流量。 下面是一个使用 iptables 配置防火墙的例子: ```shell # 清空所有规则 iptables -F # 设置默认策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # 允许回环接口 iptables -A INPUT -i lo -j ACCEPT # 允许已建立的连接 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许 SSH 连接 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许 HTTP 和 HTTPS 连接 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 其他规则 iptables -A INPUT -j DROP ``` 下面是一个使用 firewalld 配置防火墙的例子: ```shell # 启动 firewalld systemctl start firewalld # 查看状态 systemctl status firewalld # 开启 SSH 连接 firewall-cmd --zone=public --add-service=ssh --permanent # 开启 HTTP 和 HTTPS 连接 firewall-cmd --zone=public --add-service=http --permanent firewall-cmd --zone=public --add-service=https --permanent # 其他规则 firewall-cmd --zone=public --remove-port=8080/tcp --permanent # 重新加载配置 firewall-cmd --reload ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值