news 2026/4/23 14:13:30

11. Linux 防火墙管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
11. Linux 防火墙管理

Firewalld 配置

Firewalld 配置方法

  1. 直接编辑 /etc/firewalld/ 中的配置文件
  2. firewall-cmd 命令行工具
  3. firewall-config 图形工具

Firewalld 配置文件

Firewalld 配置配置文件存放在:

  • /usr/lib/firewalld/目录,Firewalld软件包自带配置位置。

  • /etc/firewalld/目录,目录结构与/usr/lib/firewalld/一致,管理员自定义配置保存在该位

    置。

==提示:==我们不建议大家通过修改/etc/firewalld/目录配置防火墙。

查看zone配置

[root@centos7 ~]# cd /usr/lib/firewalld/[root@centos7 firewalld]# lshelpers icmptypes ipsets services zones# 查看zone配置[root@centos7 firewalld]# ls zones/block.xml drop.xml home.xml public.xml work.xml dmz.xml external.xml internal.xml trusted.xml[root@centos7 firewalld]# cat zones/trusted.xml<?xmlversion="1.0"encoding="utf-8"?><zonetarget="ACCEPT"><short>Trusted</short><description>All network connections are accepted.</description></zone>[root@centos7 firewalld]# cat zones/block.xml<?xmlversion="1.0"encoding="utf-8"?><zonetarget="%%REJECT%%"><short>Block</short><description>Unsolicited incoming network packets are rejected. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed.</description></zone>[root@centos7 firewalld]# cat zones/drop.xml<?xmlversion="1.0"encoding="utf-8"?><zonetarget="DROP"><short>Drop</short><description>Unsolicited incoming network packets are dropped. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed.</description></zone>

查看服务配置

[root@centos7 firewalld]# ls -1 servicesamanda-client.xml......[root@centos7 firewalld]# cat services/http.xml<?xmlversion="1.0"encoding="utf-8"?><service><short>WWW(HTTP)</short><description>HTTP is the protocol used to serve Web pages. If you plan tomakeyour Web server publicly available,enablethis option. This option is not requiredforviewing pages locally or developing Web pages.</description><portprotocol="tcp"port="80"/></service>[root@centos7 firewalld]# cat services/https.xml<?xmlversion="1.0"encoding="utf-8"?><service><short>Secure WWW(HTTPS)</short><description>HTTPS is a modified HTTP used to serve Web pages when security is important. Examples are sites that require logins like stores or web mail. This option is not requiredforviewing pages locally or developing Web pages. You need the httpd package installedforthis option to be useful.</description><portprotocol="tcp"port="443"/></service>

firewall-cmd 命令行工具

两个基本概念

配置防火墙必须知道两个基本概念:

  • 防火墙的配置有两个状态:
    • runtime(运行时),命令行执行是的默认状态,该状态下的配置立刻生效
    • permanent(永久),通过选项 --permanent 指定,该状态下的配置不会立刻生
      效,而是写入配置文件,需要通过选项 --reload 重新reload才会生效,重新reload将
      导致运行时状态下未保存的配置丢失,但会保留当前连接状态。如果使用选项 –
      complete-reload 重新reload永久配置,将导致当前连接状态也丢失。

​ 建议配置防火墙一次性写两条规则:
​ 第一条使用选项 --permanent 永久生效;
​ 第二条不使用选项 --permanent 立刻生效。

  • 配置防火墙的规则必须一个区域内(直接规则除外),如果不使用选项 --zone 明确指明区

    域,则使用默认区域。默认区域是public。

zone 管理

# 查看zone清单[root@centos7 ~]# firewall-cmd --get-zonesblock dmz drop external home internal public trusted work# 查看激活的zone清单,也就是分配了interface和source的zone[root@centos7 ~]# firewall-cmd --get-active-zonespublic interfaces: ens32# 查看默认zone[root@centos7 ~]# firewall-cmd --get-default-zonepublic# 设置默认zone,例如设置为trusted,该操作同时设置永久态默认zone[root@centos7 ~]# firewall-cmd --set-default-zone=trusted[root@centos7 ~]# firewall-cmd --get-default-zonetrusted# 再次设置回来[root@centos7 ~]# firewall-cmd --set-default-zone=public######################## 以下关于 zone的命令-请自学######################### 如果有需要还可以创建新zone,必须配合--permanent[root@centos7 ~]# firewall-cmd --permanent --new-zone=myweb[root@centos7 ~]# firewall-cmd --permanent --get-zonesblock dmz drop external home internal myweb public trusted work[root@centos7 ~]# firewall-cmd --get-zonesblock dmz drop external home internal public trusted work# 使用--reload选项重新加载所有防火墙规则,再次查看zone清单[root@centos7 ~]# firewall-cmd --reload[root@centos7 ~]# firewall-cmd --get-zonesblock dmz drop external home internal myweb public trusted work# 查看zone target[root@centos7 ~]# firewall-cmd --permanent --zone=myweb --get-targetdefault# 设置zone target,可用target值为:# default,默认值,也就是拒绝数据包进入用户空间。# ACCEPT,允许数据包进入用户空间。# DROP,丢弃数据包,不对客户端做出任何响应。# REJECT,拒绝数据包进入用户空间。[root@centos7 ~]# firewall-cmd --permanent --zone=myweb --settarget=REJECT[root@centos7 ~]# firewall-cmd --permanent --zone=myweb --get-targetREJECT# zone不需要的时候,也可以删除,必须配合--permanent[root@centos7 ~]# firewall-cmd --permanent --delete-zone=myweb[root@centos7 ~]# firewall-cmd --permanent --get-zonesblock dmz drop external home internal public trusted work[root@centos7 ~]# firewall-cmd --reload[root@centos7 ~]# firewall-cmd --get-zonesblock dmz drop external home internal public trusted work# 查看所有zone中的规则[root@centos7 ~]# firewall-cmd --list-all-zonesblock target: %%REJECT%% icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: dmz target: default......drop target: DROP......external target: default......home target: default......internal target: default......public(active)target: default......trusted target: ACCEPT......work target: default......# 查看默认zone中的规则[root@centos7 ~]# firewall-cmd --list-allpublic(active)target: default icmp-block-inversion: no interfaces: ens32 sources: services: cockpit dhcpv6-clientsshports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:# 查看特定zone中的规则[root@centos7 ~]# firewall-cmd --list-all --zone=homehome target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client mdns samba-clientsshports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

source 管理

# 来源于特定source的数据包交给特定zone处理[root@centos7 ~]# firewall-cmd --add-source=192.168.1.0/24 --zone=home# 查看source清单[root@centos7 ~]# firewall-cmd --list-sources --zone=home192.168.1.0/24# 查看source属于哪个zone[root@centos7 ~]# firewall-cmd --get-zone-of-source=192.168.1.0/24home# 查看source是否添加[root@centos7 ~]# firewall-cmd --query-source=192.168.1.0/24 --zone=homeyes# 变更source到其他zone[root@centos7 ~]# firewall-cmd --change-source=192.168.1.0/24 --zone=public# 删除zone中source[root@centos7 ~]# firewall-cmd --remove-source=192.168.1.0/24 --zone=public

interface 管理

# 查看zone中interface[root@centos7 ~]# firewall-cmd --list-interfacesens32# 查看interface属于哪个zone[root@centos7 ~]# firewall-cmd --get-zone-of-interface=ens32public# 查看interface是否添加[root@centos7 ~]# firewall-cmd --query-interface=ens32yes# 将interface变更到其他zone[root@centos7 ~]# firewall-cmd --change-interface=ens32 --zone=home# 删除zone中interface[root@centos7 ~]# firewall-cmd --remove-interface=ens32 --zone=home# 如果interface不属于任何zone,使用以下命令将interface绑定到特定zone[root@centos7 ~]# firewall-cmd --add-interface=ens32 --zone=public

service 管理

准备httpd服务

yuminstall-y httpd systemctl start httpd
# 查看系统中预定义了哪些服务[root@centos7 ~]# firewall-cmd --get-services# 添加放行服务[root@centos7 ~]# firewall-cmd --add-service=http# 查看放行服务列表[root@centos7 ~]# firewall-cmd --list-servicescockpit dhcpv6-client httpssh# 查看服务是否放行[root@centos7 ~]# firewall-cmd --query-service=httpyes# 删除服务[root@centos7 ~]# firewall-cmd --remove-service=http

firewall-cmd命令行还可以定义新的服务。
常用选项如下:

--permanent --new-service=service Add a new permanent and empty service. --permanent --delete-service=service Delete an existing permanent service. --permanent --service=service --set-description=description Set new description toservice--permanent --service=service --get-description Print descriptionforservice--permanent --service=service --set-short=description Set short description toservice--permanent --service=service --get-short Print short descriptionforservice--permanent --service=service --add-port=portid[-portid]/protocol Add a new port to the permanent service. --permanent --service=service --remove-port=portid[-portid]/protocol Remove a port from the permanent service. --permanent --service=service --get-ports List ports added to the permanent service. --permanent --service=service --add-protocol=protocol Add a new protocol to the permanent service. --permanent --service=service --remove-protocol=protocol Remove a protocol from the permanent service. --permanent --service=service --get-protocols List protocols added to the permanent service. --permanent --service=service --add-source-port=portid[- portid]/protocol Add a newsourceport to the permanent service. --permanent --service=service --remove-source-port=portid[- portid]/protocol Remove asourceport from the permanent service. --permanent --service=service --get-source-ports Listsourceports added to the permanent service.

port 管理

# 添加放行端口[root@centos7 ~]# firewall-cmd --add-port=5900/tcp# 查看端口放行列表[root@centos7 ~]# firewall-cmd --list-ports5900/tcp# 查看端口是否放行[root@centos7 ~]# firewall-cmd --query-port=5900/tcpyes# 删除端口[root@centos7 ~]# firewall-cmd --remove-port=5900/tcp

masquerade 管理

# 查看masquerade是否启动[root@centos7 ~]# firewall-cmd --query-masqueradeno# 启动 masquerade[root@centos7 ~]# firewall-cmd --add-masquerade[root@centos7 ~]# firewall-cmd --query-masqueradeyes# 禁用masquerade[root@centos7 ~]# firewall-cmd --remove-masquerade

网络模型:

  • client:10.1.1.11 网关 10.1.1.10
  • server: 10.1.1.10、10.1.8.10(可以访问公网)

forward-port 管理

使用 port forward前,确保对应zone启用masquerade功能。

# 启动 masquerade[root@centos7 ~]# firewall-cmd --add-masquerade# 添加端口转发,语法:--add-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[- portid]][:toaddr=address[/mask]]

# 访问本机端口8000转发到本机80[root@centos7 ~]# firewall-cmd --add-forwardport=port=8000:proto=tcp:toport=80# 查看是否具有特定端口转发规则[root@centos7 ~]# firewall-cmd --query-forwardport=port=8000:proto=tcp:toport=80yes# 访问本机端口1022转发到10.1.1.11:22[root@centos7 ~]# firewall-cmd --add-forwardport=port=1022:proto=tcp:toport=22:toaddr=10.1.1.11# 查询端口转发清单[root@centos7 ~]# firewall-cmd --list-forward-portsport=8000:proto=tcp:toport=80:toaddr=port=1022:proto=tcp:toport=22:toaddr=10.1.1.11

# 删除端口转发[root@centos7 ~]# firewall-cmd --remove-forwardport=port=8000:proto=tcp:toport=80:toaddr=[root@centos7 ~]# firewall-cmd --remove-forwardport=port=1022:proto=tcp:toport=22:toaddr=10.1.8.80

icmp-block-inversion 管理

  • icmp-block-inversion默认为no,放行所有的icmp type。此时可以通过富规则禁用特定主机ping本机。

  • icmp-block-inversion设置为yes,禁止所有的icmp type。此时可以通过富规则放行特定主

    机ping本机。

# 查看icmp-block-inversion是否启用[root@centos7 ~]# firewall-cmd --query-icmp-block-inversionno# 设置icmp-block-inversion为yes[root@centos7 ~]# firewall-cmd --add-icmp-block-inversion[root@centos7 ~]# firewall-cmd --add-icmp-block-inversion --permanent[root@centos7 ~]# firewall-cmd --query-icmp-block-inversionyes# 客户端ping测试[c:\~]$ping-n110.1.8.88 正在 Ping10.1.8.88 具有32字节的数据: 来自10.1.8.88 的回复: 无法访问目标主机。10.1.8.88 的 Ping 统计信息: 数据包: 已发送=1,已接收=1,丢失=0(0% 丢失)# 由于firewalld会保留之前的连接状态,如果此时仍能ping通,# 则需要重启firewalld服务或使用选项 --complete-reload彻底重新加载防火墙规则。[root@centos7 ~]# firewall-cmd --complete-reload# 改回默认值[root@centos7 ~]# firewall-cmd --remove-icmp-block-inversion[root@centos7 ~]# firewall-cmd --remove-icmp-block-inversion --permanent

icmp-blocks 管理

# 查看系统中预定义了哪些icmptypes[root@centos7 ~]# firewall-cmd --get-icmptypes# 常用的ICMP类型有# echo-request:icmp请求报文# echo-reply:icmp响应回复报文# 添加禁止通过的icmp类型,例如通过添加echo-request实现禁止ping[root@centos7 ~]# firewall-cmd --add-icmp-block echo-request[root@centos7 ~]# firewall-cmd --add-icmp-block echo-request --permanent# 查看 icmp-block 禁止清单[root@centos7 ~]# firewall-cmd --list-icmp-blocksecho-request# 查看echo-request是否添加[root@centos7 ~]# firewall-cmd --query-icmp-block echo-requestyes# 客户端ping测试[c:\~]$ping-n110.1.8.88 正在 Ping10.1.8.88 具有32字节的数据: 来自10.1.8.88 的回复: 无法访问目标主机。10.1.8.88 的 Ping 统计信息: 数据包: 已发送=1,已接收=1,丢失=0(0% 丢失)# 由于firewalld会保留之前的连接状态,如果此时仍能ping通,# 则需要重启firewalld服务或使用选项 --complete-reload彻底重新加载防火墙规则。[root@centos7 ~]# firewall-cmd --complete-reload# 改回默认值[root@centos7 ~]# firewall-cmd --remove-icmp-block echo-request[root@centos7 ~]# firewall-cmd --remove-icmp-block echo-request --permanent

protocol 管理

# 添加protocol[root@centos7 ~]# firewall-cmd --add-protocol=icmp# 查看放行protocol清单[root@centos7 ~]# firewall-cmd --list-protocolicmp# 查看protocol是否放行[root@centos7 ~]# firewall-cmd --query-protocol=icmpyes# 删除放行protocol[root@centos7 ~]# firewall-cmd --remove-protocol=icmp

source-ports 管理

限制客户端 source-ports,很少用,设置方法同 source管理 部分。

rich rule 管理

富规则可灵活自定义各种规则,例如:

  • 允许单个IP地址或者网段连接到某个服务
  • 配置记录和审计
  • 限制速率

quest –
permanent

### protocol 管理 ```bash # 添加protocol [root@centos7 ~]# firewall-cmd --add-protocol=icmp # 查看放行protocol清单 [root@centos7 ~]# firewall-cmd --list-protocol icmp # 查看protocol是否放行 [root@centos7 ~]# firewall-cmd --query-protocol=icmp yes # 删除放行protocol [root@centos7 ~]# firewall-cmd --remove-protocol=icmp

source-ports 管理

限制客户端 source-ports,很少用,设置方法同 source管理 部分。

rich rule 管理

富规则可灵活自定义各种规则,例如:

  • 允许单个IP地址或者网段连接到某个服务
  • 配置记录和审计
  • 限制速率

富规则参考 firewalld.richlanguage(5)。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 21:03:54

学长亲荐2026TOP10AI论文网站:本科生毕业论文写作全测评

学长亲荐2026TOP10AI论文网站&#xff1a;本科生毕业论文写作全测评 2026年AI论文写作工具测评&#xff1a;为什么你需要这份榜单&#xff1f; 随着人工智能技术的快速发展&#xff0c;AI在学术写作领域的应用日益广泛。然而&#xff0c;面对市场上琳琅满目的AI论文写作平台&am…

作者头像 李华
网站建设 2026/4/6 2:08:45

【C++入门】05、复合类型-数组

1、数组数组是一种数据格式&#xff0c;能够存储多个同类型的值。数组声明时需要确定的三个要点&#xff1a;存储在每个元素中的值的类型数组名数组中的元素个数&#xff0c;必须是整数常量或const值short months[12];2、数组元素访问数组的特性之一是可以单独访问数组的元素&a…

作者头像 李华
网站建设 2026/4/19 23:09:02

基于Java+SpringBoot+SSM克州旅游网站(源码+LW+调试文档+讲解等)/克州旅游平台/克州旅游信息网/克州旅游门户网站/克州旅游官网/克州旅游服务网站

博主介绍 &#x1f497;博主介绍&#xff1a;✌全栈领域优质创作者&#xff0c;专注于Java、小程序、Python技术领域和计算机毕业项目实战✌&#x1f497; &#x1f447;&#x1f3fb; 精彩专栏 推荐订阅&#x1f447;&#x1f3fb; 2025-2026年最新1000个热门Java毕业设计选题…

作者头像 李华
网站建设 2026/4/18 11:08:05

微服务分布式SpringBoot+Vue+Springcloud中药材进存销管理系统

文章目录系统概述技术架构核心功能创新点应用价值主要技术与实现手段系统设计与实现的思路系统设计方法java类核心代码部分展示结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;系统概述 中药材进存销管理系统基于微服务分布式架构&am…

作者头像 李华
网站建设 2026/4/23 12:25:39

微服务分布式SpringBoot+Vue+Springcloud在线投票系统

文章目录微服务分布式在线投票系统摘要主要技术与实现手段系统设计与实现的思路系统设计方法java类核心代码部分展示结论源码lw获取/同行可拿货,招校园代理 &#xff1a;文章底部获取博主联系方式&#xff01;微服务分布式在线投票系统摘要 基于SpringBoot、Vue和SpringCloud的…

作者头像 李华