V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
balabalaXMX
V2EX  ›  Linux

Linux 的防火墙, iptables, firewalld 和 netfilter 这些东西到底是什么关系啊?

  •  
  •   balabalaXMX · 2023-01-03 10:08:19 +08:00 · 4872 次点击
    这是一个创建于 450 天前的主题,其中的信息可能已经有所发展或是发生改变。

    首先,我的理解是 iptables 是用户空间的客户端工具,用于对真正的防火墙服务 netfilter 进行规则创建等功能。那么 firewalld 又是啥?

    为啥我关闭防火墙是关闭 systemctl stop firewalld ?

    如果我关闭了防火墙,是不是意味着我 iptables-save 输出的一系列防火墙规则其实是没用的? 但是我部署在上面的 k8s 集群好像又表现得很正常。

    因为安装 linkerd 出现一个容器启动错误,说是没有 modprobe: can't change directory to '/lib/modules': No such file or directory\niptables-save v1.8.8 (legacy): Cannot initialize: iptables who? (do you need to insmod?) 所以就查阅了一些,越查越困惑。这里我理解我的主机是 centos8 ,用 iptables -V 查看时新版本的 iptables ,所以没有加载旧版本的。那么这里我在宿主机上加载新旧两个版本的 iptables 会有什么问题吗?

    20 条回复    2023-01-06 17:57:16 +08:00
    wonderblank
        1
    wonderblank  
       2023-01-03 10:11:03 +08:00
    建议去看书,这里提问问不出来有实质性的答案,而且很多都是错的。
    NoahNye
        2
    NoahNye  
       2023-01-03 10:23:31 +08:00   ❤️ 5
    netfilter:里世界,实际应用规则的人,但不能直接操作,
    iptables:因为不能直接操作里世界,所以在表世界映射成 iptables 供用户使用,
    firewall 、ufw:让用户使用更爽的一层 iptables 封装。
    ysc3839
        3
    ysc3839  
       2023-01-03 10:25:34 +08:00
    个人理解,netfilter 是内核提供的 API ,允许其他模块 hook 网络流量,iptables 通过调用 netfilter API 来实现各种功能。
    firewalld 是用户模式的 iptables 管理工具,通过一些简化的语法来生成对应复杂的 iptables 规则。
    lolizeppelin
        4
    lolizeppelin  
       2023-01-03 10:32:38 +08:00   ❤️ 9
    --------先给你解释 iptable 和 firewalld 的关系

    假设实际的防火墙规则是一条条内核中的记录

    那么 iptable-save 就是用自己格式输出、保存的防火墙规则
    iptabale 的其他命令行工具能对防火墙规则进行增删改查

    iptable 本质就是清空防火墙规则 保存防火墙规则 推送保存的防火墙规则的工具

    由于 iptable 对复杂规则的使用不够友好,所以设计了 firewalld 这个以 xml 存储防火墙规则的程序

    但是无论是 iptable 还是 firewalld,最后操作的都是内核中的记录,所以无论使用 firewalld 还是 iptable
    修改、列出的规则都是内核中的规则

    现在都是尽量避免使用 iptable 来操作防火墙,都是用 firewalld 来配置

    -------这里解释容器相关防火墙

    容器实现靠的是操作系统的 namespace,每个 namespace 有自己的路由和防火墙规则
    默认所有操作都在 default namespace 中, 你直接执行 iptable-save 导出的是 default namespace 的防火墙规则
    default namespace 是系统中所有正常启动的进程所在的 namespace

    你想获取容器中的防火墙规则,需要切换到指定容器对应的 namespace 中执行 iptable-save
    具体的 namespace exce 命令随便查下就知道了
    dzdh
        5
    dzdh  
       2023-01-03 10:36:05 +08:00   ❤️ 7
    防火墙本体是内核的 NetFilter 。

    iptables 是 mysql-cli

    firewalld 是 navicat

    ufw 是 heidisql
    isno
        6
    isno  
       2023-01-03 10:55:41 +08:00
    zhangsanfeng2012
        7
    zhangsanfeng2012  
       2023-01-03 11:01:03 +08:00
    还有 nftables
    mrzx
        8
    mrzx  
       2023-01-03 14:04:04 +08:00
    最底层的是 NetFilter 然后实际操作层是 iptables 而 firewall 我认为是 iptables 命令简化版的 gui

    作为 12 年的运维工程师,还是更偏向于 iptables

    新生代更偏向于用 firewall ,因为命令更简单,通俗易懂.

    现在基于 ubuntu 的程序员更多,所以他们更喜欢用傻瓜相机版的 ufw
    lolizeppelin
        9
    lolizeppelin  
       2023-01-03 14:35:58 +08:00
    ufw 不知道,filewall 本质还是 iptable

    比如 filewall 标准语法不能描述的规则都用 direct 实现
    direct 里面都是 iptable 的语法

    话说前端娱乐圈搞那么多状态管理....
    linux 搞两三个防火墙管理怎么了!!
    才两三个你们就叫,写前端不得疯
    uncat
        10
    uncat  
       2023-01-03 14:53:34 +08:00
    @NoahNye 最新的 firewalld 不使用 iptables 了,使用的是 nftables

    不直接使用 iptables ,使用 firewalld 是因为:

    firewalld 是有状态的,可以实现有状态运维管理(基于 ansible + git 的代码化运维)
    leonshaw
        11
    leonshaw  
       2023-01-03 15:16:17 +08:00
    >> 因为安装 linkerd 出现一个容器启动错误,说是没有 modprobe

    宿主机上把需要的内核模块先装上。用户态不同版本一般没关系。
    FabricPath
        12
    FabricPath  
       2023-01-03 15:25:15 +08:00
    netfilter 是个框架,iptables 是用户态工具(iptables 之类的命令)+内核模块( lsmod|grep -i ipt),在 netfilter 有几个 iptables 的 hook ,firewalld 就是一个 iptables 规则生成器。

    不过这个年代别学 iptables 了,马上就被 nft 替代了,你看到的 iptables-legacy 就是传统 iptables ,在新的发行版本里面 iptables 就是 nftable 的一层皮
    kaneg
        13
    kaneg  
       2023-01-03 15:37:07 +08:00
    你这个容器貌似需要高级权限的操作,可能需要设置 privileged: true
    allgy
        14
    allgy  
       2023-01-03 15:38:40 +08:00
    @dzdh 类比形象,言简意赅
    swsh007
        15
    swsh007  
       2023-01-03 15:42:55 +08:00 via Android
    还是习惯 iptables
    mrzx
        16
    mrzx  
       2023-01-03 15:50:11 +08:00
    @uncat 谢谢,新知识点,学到了,我去查查 nftables 和 iptables 的区别
    stephenyin
        17
    stephenyin  
       2023-01-03 18:48:17 +08:00
    @dzdh #5 本来挺了解的东西,直接被你比喻到我的知识盲区了。。。🤣
    dzdh
        18
    dzdh  
       2023-01-03 22:16:25 +08:00
    @stephenyin #17 :)
    tomychen
        19
    tomychen  
       2023-01-06 16:52:37 +08:00   ❤️ 1
    如果把 netfilter 理解为防火墙的核心的话,iptables/firewalld/ufw 这些就都算是跟核心通话的传话员。

    老板说把 192.168.1.100 那龟孙给我拦下

    iptables call netfilter 老板说了,192.168.1.100 那龟孙给 drop 了,然后那龟孙就被 drop 了。

    192.168.1.200 那小子是我家亲戚放他进来

    iptables call netfilter 老板说了,192.168.1.200 那小子可以进场,然后他 accept 了


    至于 iptables firewalld ufw 的区别呢 其实都是传话的。

    Redhat 系 把 firewalld 这马甲推出来呢,想让大家觉得这玩意更像个防火墙,但还是个传话筒

    Ubuntu 系 不服,你搞个 firewalld ,我也得整活,我来个 ufw ,再配置个牛 X 的名字“Uncomplicated Firewall”,于是 ufw 就这么出来了...

    iptables 笑而不语,换个名字,还真当自己进内核了?还不是传话筒...
    raysonx
        20
    raysonx  
       2023-01-06 17:57:16 +08:00
    另外补充,iptables 已经过时了,现在新的发行版用的都是 nftables ,新版 firewalld 也是用 nftables 作后端。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1590 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:54 · PVG 00:54 · LAX 09:54 · JFK 12:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.