linux查看firewalld的状态为什么出现很多iptable的报错


[root@bogon ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 日 2023-06-18 11:55:50 CST; 16min ago
 Main PID: 2216 (firewalld)
   Memory: 18.9M
   CGroup: /system.slice/firewalld.service
           └─2216 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t filter -n -L DOCKER-ISOLATION-STAGE-2' failed: iptables: No chain/...y that name.
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t filter -C DOCKER-ISOLATION-STAGE-1 -j RETURN' failed: iptables: Ba...hat chain?).
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t filter -C DOCKER-ISOLATION-STAGE-2 -j RETURN' failed: iptables: Ba...hat chain?).
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t nat -C DOCKER -i docker0 -j RETURN' failed: iptables: Bad rule (do...hat chain?).
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t nat -C POSTROUTING -m addrtype --src-type LOCAL -o docker0 -j MASQ...y that name.
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule ...hat chain?).
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: INVALID_ZONE: docker
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t nat -C POSTROUTING -m addrtype --src-type LOCAL -o docker0 -j MASQ...y that name.
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule ...hat chain?).
6月 18 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: INVALID_ZONE: docker
Hint: Some lines were ellipsized, use -l to show in full.

请问firewalld和iptables是什么关系?为什么查看firewalld的状态会提示很多iptables的报错,难道firewalld的本质也是iptables?

不好说

实际上,firewalld 在内部使用了 iptables 命令和 Netfilter 内核模块来实现具体的功能,但它为管理员提供了一种更为高级和抽象的方式来配置和管理防火墙规则。
在你的报错信息中,可以看到 firewalld 的部分功能需要调用 iptables 命令来实现,但这些命令执行时出现了错误,其中大部分错误提示都与不存在某个特定的防火墙规则或链有关。一个可能的原因是 docker 这样的特定应用程序使用了自定义的防火墙规则,与 firewalldiptables 默认设置存在冲突。你可以手动编辑防火墙规则,来解决这些错误和冲突问题,以确保你的应用程序和系统的网络安全。

  • 这篇博客也许可以解决你的问题👉 :Linux--firewalld防火墙基础(firewalld和iptables的关系,四表五链,netfilter与iptables的关系,iptables语法与参数,firewalld网络区域)
  • 除此之外, 这篇博客: 防火墙设置与配置开放端口中的 如果想换回iptables防火墙,可关闭firewalld并安装iptables。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 1、关闭firewall:

    • 停止firewall:systemctl stop firewalld.service
    • 禁止firewall开机启动:systemctl disable firewalld.service
    • 查看默认防火墙状态:firewall-cmd --state(关闭后显示notrunning,开启后显示running)

    2.安装iptables-services

    yum install iptables-services
    

    3.修改防火墙配置文件

    vi /etc/sysconfig/iptables
    

    添加端口80、8080、3306、3690端口:

    esc :wq! 退出保存修改。

    【注】:添加在端口22上面或者下面,不要放在最后,不然不起作用。
    

    4.重启防火墙使配置生效

    systemctl restart iptables.service
    

    刚刚yum install iptables.service之后系统如果没有重启,iptables.service是找不到的,会报unit not fount。
    设置防火墙开机启动:

    systemctl enable iptables.service
    
  • 以下回答由chatgpt基于相关博客总结生成:

    firewalld和iptables的关系是,firewalld是对iptables的高层封装,它提供了更高级别的规则设置方式,更加易于使用和管理。而iptables是Linux平台下的一种基础防火墙软件,可以通过规则表来控制网络数据包的流入和流出,同时,iptables也作为Linux内核中的一个模块存在,它可以与其他内核模块(如Netfilter)配合工作,提高了数据包过滤的处理效率。

    当我们查看firewalld状态时,可能会看到很多iptables报错信息,这是因为firewalld确实是基于iptables的机制。它会通过iptables服务来设置规则,所以如果iptables服务没有启动或者没有安装,则firewalld就会出现错误信息。但是,这些错误信息并不影响firewalld的正常运行,因为firewalld已经对iptables进行了高层封装,相比于iptables更加易于使用和管理。

    如果想要查看firewalld状态,可以使用以下命令:

    systemctl status firewalld.service
    

    如果iptables没有安装或者没有启动,则可以通过以下命令来安装和启动:

    yum install iptables
    systemctl start iptables.service
    

    除了查看firewalld状态之外,我们还可以通过firewall-cmd来管理firewalld的规则。以下是常用的几个命令:

    1. 查看当前防火墙状态
    firewall-cmd --state
    
    1. 重新加载防火墙规则
    firewall-cmd --reload
    
    1. 添加/删除端口或服务
    firewall-cmd --zone=public --add-port=80/tcp --permanent  # 添加80端口
    firewall-cmd --zone=public --remove-port=80/tcp --permanent  # 删除80端口
    firewall-cmd --zone=public --add-service=http --permanent  # 添加http服务
    firewall-cmd --zone=public --remove-service=http --permanent  # 删除http服务
    
    1. 添加/删除IP地址或IP段
    firewall-cmd --zone=public --add-source=192.168.1.1/24 --permanent  # 添加192.168.1.1/24 IP段
    firewall-cmd --zone=public --remove-source=192.168.1.1/24 --permanent  # 删除192.168.1.1/24 IP段
    
    1. 启用/禁用防火墙
    systemctl start firewalld.service  # 启动防火墙
    systemctl stop firewalld.service  # 停止防火墙
    systemctl enable firewalld.service  # 开机自启动防火墙
    systemctl disable firewalld.service  # 关闭开机自启动防火墙
    

    需要注意的是,这里的--permanent参数表示永久生效,如果不加这个参数,则规则只会在运行时生效。另外,firewall-cmd是与iptables不同的工具,它并不使用iptables的命令和规则表,所以对于已经存在的iptables规则,firewall-cmd并不会自动识别,这需要手动进行转换或者使用iptables-save/iptables-restore命令导出和导入规则表。


618 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -t nat -C POSTROUTING -m addrtype --src-type LOCAL -o docker0 -j MASQUERADE' failed: 
618 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: COMMAND_FAILED: '/sbin/iptables -w2 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule (does a matching
618 12:09:52 bogon firewalld[2216]: 2023-06-18 12:09:52 ERROR: INVALID_ZONE: docker
618 12:09:52 bogon dockerd[4000]: time="2023-06-18T12:09:52.658672553+08:00" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd names
618 12:09:52 bogon dockerd[4000]: failed to start daemon: Error initializing network controller: Error creating default "bridge" network: Failed to program NAT chain: INVALID_ZONE: doc
618 12:09:52 bogon dockerd[4000]: time="2023-06-18T12:09:52.658814193+08:00" level=info msg="stopping event stream following graceful shutdown" error="context canceled" module=libconta
618 12:09:52 bogon systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
618 12:09:52 bogon systemd[1]: Failed to start Docker Application Container Engine.