混合环境防火墙配置指南:Linux firewalld 与 Windows Defender 关键端口策略对比

枫少@KillBoy
枫少@KillBoy
枫少@KillBoy
管理员
266
文章
0
粉丝
安全运维251字数 916阅读3分3秒阅读模式
AI智能摘要
AI 生成的文章内容摘要

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

firewalld 区域示意图

1. 防火墙模型的根本差异

  • firewalld:通过 zone(如 publicinternaltrusted)划分网络安全等级。每个 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

要点

  1. --permanent 确保规则持久化。
  2. 只在 internal 区域放行,外部 public 区域仍保持默认阻断。
  3. 若某端口仅用于出站请求(如内部服务调用外部 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

要点

  1. -Profile Private 限制规则仅在内部网络生效,公共网络仍受阻。
  2. 若需要同时放行出站流量,可再添加对应的出站规则(-Direction Outbound)。
  3. 通过 Get-NetFirewallRule -DisplayName "Allow TCP 3306 - MySQL" 可随时检查规则状态。
Windows Defender PowerShell 示例

4. 对比表:firewalld vs Windows Defender(同一端口)

端口firewalld 关键命令适用 zoneWindows Defender 关键命令适用网络配置文件
8080firewall-cmd --permanent --zone=internal --add-port=8080/tcpinternalNew-NetFirewallRule -DisplayName "Allow TCP 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile PrivatePrivate
3306firewall-cmd --permanent --zone=internal --add-port=3306/tcpinternalNew-NetFirewallRule -DisplayName "Allow TCP 3306" -Direction Inbound -Protocol TCP -LocalPort 3306 -Action Allow -Profile PrivatePrivate
9200firewall-cmd --permanent --zone=internal --add-port=9200/tcpinternalNew-NetFirewallRule -DisplayName "Allow TCP 9200" -Direction Inbound -Protocol TCP -LocalPort 9200 -Action Allow -Profile PrivatePrivate
6379firewall-cmd --permanent --zone=internal --add-port=6379/tcpinternalNew-NetFirewallRule -DisplayName "Allow TCP 6379" -Direction Inbound -Protocol TCP -LocalPort 6379 -Action Allow -Profile PrivatePrivate

5. 落地建议

  1. 统一安全基线:在 linux 端统一使用 internal(或自定义)zone,在 Windows 端统一使用 Private 配置文件,确保两套系统的最小权限范围相匹配。
  2. 审计与验证
  3. linuxfirewall-cmd --list-all --zone=internal 查看已放行端口。
  4. Windows:Get-NetFirewallRule -Profile Private | Where-Object {$_.Direction -eq "Inbound"} 列出所有入站规则。
  5. 定期清理:对不再使用的端口及时删除规则,防止权限漂移。
  6. Linux:firewall-cmd --permanent --zone=internal --remove-port=8080/tcp && firewall-cmd --reload
  7. Windows:Remove-NetFirewallRule -DisplayName "Allow TCP 8080"

 
枫少@KillBoy
评论  2  访客  2
    • 银甲寒光
      银甲寒光 1

      混合环境防火墙配置确实是个痛点,感谢对比

      • 翡翠生凉
        翡翠生凉 1

        容器宿主机上防火墙策略还要叠加 docker 的规则,更麻烦

      匿名

      发表评论

      匿名网友

      拖动滑块以完成验证