AI智能摘要
AI 生成的文章内容摘要
在混合使用 linux 与 Windows 的服务器环境中,统一防火墙策略是保障安全基线的关键。下面以常见服务端口 8080(Web 应用)、3306(MySQL)、9200(Elasticsearch)和 6379(Redis)为例,比较 firewalld(基于 zone)和 Windows Defender 防火墙(基于入站/出站规则)的配置逻辑,并给出最小权限原则下的实际命令示例。

1. 防火墙模型的根本差异
- firewalld:通过 zone(如
public、internal、trusted)划分网络安全等级。每个 zone 维护一套允许的服务或端口,流量只有在匹配的 zone 中被明确放行才会通过。 - Windows Defender:采用 入站 与 出站 规则,规则可以指定协议、端口、方向、适用的网络配置文件(Domain、Private、Public)。规则的粒度更细,但缺少类似 zone 的层级概念,需要在每个规则中手动指明适用的配置文件。
最小权限原则要求只在需要的 zone 或配置文件中打开必要端口,其他流量保持阻断。
2. firewalld 中的端口放行
下面示例假设业务服务器位于 internal 区域,仅对该区域放行目标端口。若服务器使用默认的 public 区域,可将 --zone=internal 替换为 --zone=public。
# 统一将目标服务器加入 internal 区域(如果尚未设置)
sudo firewall-cmd --permanent --zone=internal --add-source=10.0.0.0/24
sudo firewall-cmd --reload
# 8080 – HTTP 应用
sudo firewall-cmd --permanent --zone=internal --add-port=8080/tcp
# 3306 – MySQL
sudo firewall-cmd --permanent --zone=internal --add-port=3306/tcp
# 9200 – Elasticsearch(默认使用 TCP)
sudo firewall-cmd --permanent --zone=internal --add-port=9200/tcp
# 6379 – Redis
sudo firewall-cmd --permanent --zone=internal --add-port=6379/tcp
# 重新加载使配置生效
sudo firewall-cmd --reload
要点
--permanent确保规则持久化。- 只在
internal区域放行,外部public区域仍保持默认阻断。 - 若某端口仅用于出站请求(如内部服务调用外部 API),可以使用
--add-rich-rule指定方向为outbound,但对上述四个服务通常需要入站访问。
3. Windows Defender 中的规则对应
在 Windows 中,使用 PowerShell 的 New‑NetFirewallRule 创建入站规则。下面的示例统一将规则限定在 Private 网络配置文件,适用于内部服务器;如果服务器位于域环境,可改为 Domain。
# 8080 – HTTP 应用
New-NetFirewallRule -DisplayName "Allow TCP 8080 - WebApp" `
-Direction Inbound -Protocol TCP -LocalPort 8080 `
-Action Allow -Profile Private
# 3306 – MySQL
New-NetFirewallRule -DisplayName "Allow TCP 3306 - MySQL" `
-Direction Inbound -Protocol TCP -LocalPort 3306 `
-Action Allow -Profile Private
# 9200 – Elasticsearch
New-NetFirewallRule -DisplayName "Allow TCP 9200 - Elasticsearch" `
-Direction Inbound -Protocol TCP -LocalPort 9200 `
-Action Allow -Profile Private
# 6379 – Redis
New-NetFirewallRule -DisplayName "Allow TCP 6379 - Redis" `
-Direction Inbound -Protocol TCP -LocalPort 6379 `
-Action Allow -Profile Private
要点
-Profile Private限制规则仅在内部网络生效,公共网络仍受阻。- 若需要同时放行出站流量,可再添加对应的出站规则(
-Direction Outbound)。 - 通过
Get-NetFirewallRule -DisplayName "Allow TCP 3306 - MySQL"可随时检查规则状态。

4. 对比表:firewalld vs Windows Defender(同一端口)
| 端口 | firewalld 关键命令 | 适用 zone | Windows Defender 关键命令 | 适用网络配置文件 |
|---|---|---|---|---|
| 8080 | firewall-cmd --permanent --zone=internal --add-port=8080/tcp | internal | New-NetFirewallRule -DisplayName "Allow TCP 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile Private | Private |
| 3306 | firewall-cmd --permanent --zone=internal --add-port=3306/tcp | internal | New-NetFirewallRule -DisplayName "Allow TCP 3306" -Direction Inbound -Protocol TCP -LocalPort 3306 -Action Allow -Profile Private | Private |
| 9200 | firewall-cmd --permanent --zone=internal --add-port=9200/tcp | internal | New-NetFirewallRule -DisplayName "Allow TCP 9200" -Direction Inbound -Protocol TCP -LocalPort 9200 -Action Allow -Profile Private | Private |
| 6379 | firewall-cmd --permanent --zone=internal --add-port=6379/tcp | internal | New-NetFirewallRule -DisplayName "Allow TCP 6379" -Direction Inbound -Protocol TCP -LocalPort 6379 -Action Allow -Profile Private | Private |
5. 落地建议
- 统一安全基线:在 linux 端统一使用
internal(或自定义)zone,在 Windows 端统一使用Private配置文件,确保两套系统的最小权限范围相匹配。 - 审计与验证:
- linux:
firewall-cmd --list-all --zone=internal查看已放行端口。 - Windows:
Get-NetFirewallRule -Profile Private | Where-Object {$_.Direction -eq "Inbound"}列出所有入站规则。 - 定期清理:对不再使用的端口及时删除规则,防止权限漂移。
- Linux:
firewall-cmd --permanent --zone=internal --remove-port=8080/tcp && firewall-cmd --reload - Windows:
Remove-NetFirewallRule -DisplayName "Allow TCP 8080"

重庆市 1F
混合环境防火墙配置确实是个痛点,感谢对比
山东省烟台市 2F
容器宿主机上防火墙策略还要叠加 docker 的规则,更麻烦