V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
SJ2050cn
V2EX  ›  问与答

firewalld 防火墙是如何工作的?

  •  
  •   SJ2050cn · 2021-07-20 10:01:13 +08:00 · 1427 次点击
    这是一个创建于 983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    新版本的 docker 在安装的时候会自动在 firewalld 中添加一个名为 docker 的 zone,并且将默认 docker 的网卡 docker0 添加到其中。这个 docker zone 的默认 target 为 ACCEPT,按照我查阅相关资料后的理解,target 为默认行为,即不匹配任何规则后的行为,在这种理解下,如果不配置规则,进入 docker zone 的默认都是放行的,但我需要默认阻拦,仅放行部分指定端口,于是我将 docker zone 的 target 改为 DROP,并且未添加任何规则。但当我运行 nginx 容器时,我将 nginx 容器的 80 端口映射到宿主机的 8080,不幸的是,从外网依然能访问 8080 端口,docker zone 的默认 DROP 规则并未生效,我尝试使用中英文搜索了 firewalld 的相关工作原理,大多都只停留在 firewalld 的基本命令操作,未找到描述其工作原理的解释,于是特此来 v2 上问问各位老哥,firewalld 的不同 zones 到底是咋工作的。

    以下附上我的 firewalld 的不同 zone 的配置:

    block
      target: %%REJECT%%
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: 
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    dmz
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    docker (active)
      target: DROP
      icmp-block-inversion: no
      interfaces: docker0
      sources: 
      services: 
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    drop
      target: DROP
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: 
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    external
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: ssh
      ports: 
      protocols: 
      masquerade: yes
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    home
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: dhcpv6-client mdns samba-client ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    internal
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: dhcpv6-client mdns samba-client ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    public
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: dhcpv6-client ssh
      ports: 2222/tcp
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    trusted
      target: ACCEPT
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: 
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    work
      target: default
      icmp-block-inversion: no
      interfaces: 
      sources: 
      services: dhcpv6-client ssh
      ports: 
      protocols: 
      masquerade: no
      forward-ports: 
      source-ports: 
      icmp-blocks: 
      rich rules: 
    	
    
    
    
    9 条回复    2021-07-21 21:22:34 +08:00
    AoEiuV020
        1
    AoEiuV020  
       2021-07-20 10:27:15 +08:00   ❤️ 1
    不大熟,跑 nginx 容器时会不会自动添加了规则,
    另外建议用"iptables -vnL"这种检查实时的规则而不是 firewall 配置,
    而且我直觉判断 docker0 网卡的配置并不影响外网,外网还是走的外网网卡,
    EPr2hh6LADQWqRVH
        2
    EPr2hh6LADQWqRVH  
       2021-07-20 10:30:00 +08:00   ❤️ 1
    他自己有自己的模型,他把这个模型动态地给你转换成 iptables 记录,就这么简单。
    openwrt 上的 firewall3,更简单,但原理上是一致的,就是 iptables 配置生成器。
    SJ2050cn
        3
    SJ2050cn  
    OP
       2021-07-20 10:39:17 +08:00
    @AoEiuV020 主要不懂 docker 到底动了防火墙啥,我现在将 docker0 网卡加入到 public zone,在 public 中如果不给定端口是默认阻拦的,算是曲线完成我的目标
    SJ2050cn
        4
    SJ2050cn  
    OP
       2021-07-20 10:41:49 +08:00
    @avastms 我知道最后都是在改 iptables,但就没法用 firewalld 防火墙管理了吗,因为 firewalld 管理比较方便简明,iptables 又是一大串规则。
    Jirajine
        5
    Jirajine  
       2021-07-20 11:26:48 +08:00 via Android
    其实 nft 最方便,简单直接。firewalld 除了笔记本这种移动设备以外都不需要那些所谓的“动态”功能。
    zhangsanfeng2012
        6
    zhangsanfeng2012  
       2021-07-20 14:03:04 +08:00
    你把 pulic 域 active 起来就可以了,不需要改 docker 域;
    public 域没有加源,--add-interface 或者--add-source 就可以 active 了
    SJ2050cn
        7
    SJ2050cn  
    OP
       2021-07-20 14:46:32 +08:00
    @zhangsanfeng2012 我目前就是这样干的,把 docker0 的网卡加入到 public 里
    zhangsanfeng2012
        8
    zhangsanfeng2012  
       2021-07-20 15:02:18 +08:00
    @SJ2050cn 你应该把物理出接口放到 public 里面
    arthurire
        9
    arthurire  
       2021-07-21 21:22:34 +08:00
    我记得好象是 docker 和 firewalld 冲突 需要手动改一些东西 网上有一大把文章
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4616 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:54 · PVG 17:54 · LAX 02:54 · JFK 05:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.