二十二、【Linux系统防火墙管理】firewalld命令详解
firewalld命令详解
- 本章学习目标
- 内容简介
-
- 防火墙核心功能
- 防火墙工作流程
- 操作演示
-
-
- 启停防火墙服务
- 区域管理
- 开放http服务
- 自定义端口开放
- 临时开放端口
- IP白名单
- 端口转发
- 拒绝ICMP响应(禁ping)
- 故障排查流程
-
本章学习目标
了解学习Linux系统防火墙信息与firewalld命令使用
内容简介
防火墙核心功能
-
动态规则管理
- 无需重启生效:规则修改后通过
--reload
重载即时生效,不中断现有连接 - 双状态配置:
运行时规则
:临时生效,重启后失效(不加--permanent
)。持久化规则
:需--permanent
+--reload
,重启后保留
- 无需重启生效:规则修改后通过
-
区域隔离策略
- 分层信任模型
public
(默认):不信任公共网络,仅放行显式允许的服务。trusted
:完全信任指定 IP(如--add-source=192.168.1.0/24
)。
- 接口绑定:将网卡分配到不同区域(如
--zone=home --change-interface=eth0
),实现网络环境差异化策略
- 分层信任模型
-
高级流量控制
- 富规则 (Rich Rules)
- 精准过滤:支持 IP/端口/协议组合(如仅允许某 IP 访问特定端口)。
- 限速与审计:
limit value=\"2/s\"
限制每秒连接数,log prefix=\"Blocked: \"
记录拒绝事件 。
- NAT 与端口转发:
- 依赖
--add-masquerade
启用 IP 伪装,实现端口转发或隐藏内网结构
- 依赖
- 富规则 (Rich Rules)
防火墙工作流程
#mermaid-svg-VsZhR3KoHIyjhGPE {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .error-icon{fill:#552222;}#mermaid-svg-VsZhR3KoHIyjhGPE .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VsZhR3KoHIyjhGPE .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-VsZhR3KoHIyjhGPE .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VsZhR3KoHIyjhGPE .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VsZhR3KoHIyjhGPE .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VsZhR3KoHIyjhGPE .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VsZhR3KoHIyjhGPE .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VsZhR3KoHIyjhGPE .marker.cross{stroke:#333333;}#mermaid-svg-VsZhR3KoHIyjhGPE svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VsZhR3KoHIyjhGPE .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .cluster-label text{fill:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .cluster-label span{color:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .label text,#mermaid-svg-VsZhR3KoHIyjhGPE span{fill:#333;color:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .node rect,#mermaid-svg-VsZhR3KoHIyjhGPE .node circle,#mermaid-svg-VsZhR3KoHIyjhGPE .node ellipse,#mermaid-svg-VsZhR3KoHIyjhGPE .node polygon,#mermaid-svg-VsZhR3KoHIyjhGPE .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VsZhR3KoHIyjhGPE .node .label{text-align:center;}#mermaid-svg-VsZhR3KoHIyjhGPE .node.clickable{cursor:pointer;}#mermaid-svg-VsZhR3KoHIyjhGPE .arrowheadPath{fill:#333333;}#mermaid-svg-VsZhR3KoHIyjhGPE .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VsZhR3KoHIyjhGPE .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VsZhR3KoHIyjhGPE .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-VsZhR3KoHIyjhGPE .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-VsZhR3KoHIyjhGPE .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VsZhR3KoHIyjhGPE .cluster text{fill:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE .cluster span{color:#333;}#mermaid-svg-VsZhR3KoHIyjhGPE div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VsZhR3KoHIyjhGPE :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;} 匹配 未匹配 首包 允许 拒绝 后续包 合法 非法 网络流量进入 基础包处理 解析帧头部/IP头 白名单/黑名单检查? 放行或阻断 会话表查询 状态检测 安全策略匹配 创建会话表项 丢弃并记录 基于会话表转发 应用层安全检测 内容安全检测 地址转换NAT 发送报文
操作演示
systemctl start firewalld
start
立即运行firewall-cmd --get-default-zone
firewall-cmd --zone=public --add-service=http --permanent
--add-service
服务名firewall-cmd --zone=public --add-port=8080/tcp --permanent
--add-port
端口/协议firewall-cmd --list-all
--list-all
完整配置firewall-cmd --add-rich-rule=\'rule...\' --permanent
--add-rich-rule
复杂规则firewall-cmd --add-forward-port=port=80:...
--add-forward-port
转发目标启停防火墙服务
# 启动并设置开机自启[root@localhost ~]# systemctl start firewalld[root@localhost ~]# systemctl enable firewalldCreated symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.# 查看运行状态[root@localhost ~]# firewall-cmd --staterunning # 防火墙已运行
区域管理
# 查看默认区域[root@localhost ~]# firewall-cmd --get-default-zonepublic # 默认区域为 public# 切换默认区域为 home[root@localhost ~]# firewall-cmd --set-default-zone=homesuccess # 生产环境慎用(home 区域规则较宽松)
开放http服务
# 永久允许 HTTP(80/TCP)[root@localhost ~]# firewall-cmd --zone=public --add-service=http --permanentsuccess[root@localhost ~]# firewall-cmd --reload # 重载生效
自定义端口开放
# 永久开放 TCP 端口 8080[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanentsuccess[root@localhost ~]# firewall-cmd --reload# 查看当前规则[root@localhost ~]# firewall-cmd --list-allpublic (active) services: dhcpv6-client http ssh # 已放行服务 ports: 8080/tcp # 已开放端口
临时开放端口
# 临时开放 9999/TCP(重启失效)[root@localhost ~]# firewall-cmd --add-port=9999/tcpsuccess # 无需重载,立即生效
IP白名单
# 仅允许 192.168.1.0/24 访问 SSH[root@localhost ~]# firewall-cmd --zone=public --add-rich-rule=\'rule family=\"ipv4\" source address=\"192.168.1.0/24\" service name=\"ssh\" accept\' --permanentsuccess[root@localhost ~]# firewall-cmd --reload # 阻断非白名单 IP 的 SSH 访问
端口转发
# 将本机 80 转发至 192.168.0.100:8080[root@localhost ~]# firewall-cmd --add-masquerade --permanent # 开启 IP 伪装(NAT)success[root@localhost ~]# firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.0.100 --permanentsuccess[root@localhost ~]# firewall-cmd --reload # 实现内网服务隐藏
拒绝ICMP响应(禁ping)
# 禁止公网 Ping 探测[root@localhost ~]# firewall-cmd --zone=public --add-icmp-block=echo-reply --permanentsuccess[root@localhost ~]# firewall-cmd --reload # 提升隐蔽性
故障排查流程
#mermaid-svg-ENKtpO128ZXUO0PB {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .error-icon{fill:#552222;}#mermaid-svg-ENKtpO128ZXUO0PB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ENKtpO128ZXUO0PB .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ENKtpO128ZXUO0PB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ENKtpO128ZXUO0PB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ENKtpO128ZXUO0PB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ENKtpO128ZXUO0PB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ENKtpO128ZXUO0PB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ENKtpO128ZXUO0PB .marker.cross{stroke:#333333;}#mermaid-svg-ENKtpO128ZXUO0PB svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ENKtpO128ZXUO0PB .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .cluster-label text{fill:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .cluster-label span{color:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .label text,#mermaid-svg-ENKtpO128ZXUO0PB span{fill:#333;color:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .node rect,#mermaid-svg-ENKtpO128ZXUO0PB .node circle,#mermaid-svg-ENKtpO128ZXUO0PB .node ellipse,#mermaid-svg-ENKtpO128ZXUO0PB .node polygon,#mermaid-svg-ENKtpO128ZXUO0PB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ENKtpO128ZXUO0PB .node .label{text-align:center;}#mermaid-svg-ENKtpO128ZXUO0PB .node.clickable{cursor:pointer;}#mermaid-svg-ENKtpO128ZXUO0PB .arrowheadPath{fill:#333333;}#mermaid-svg-ENKtpO128ZXUO0PB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ENKtpO128ZXUO0PB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ENKtpO128ZXUO0PB .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-ENKtpO128ZXUO0PB .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-ENKtpO128ZXUO0PB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ENKtpO128ZXUO0PB .cluster text{fill:#333;}#mermaid-svg-ENKtpO128ZXUO0PB .cluster span{color:#333;}#mermaid-svg-ENKtpO128ZXUO0PB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ENKtpO128ZXUO0PB :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;} 不通流量 检查接口安全域 启用Debug 分析策略匹配 补全规则
养生知识,日常养生,食疗中医,养生常识,知识大全