Communitygithub.com

家庭实验室 VLAN 隔离

使用 UniFi、pfSense/OPNsense 和 MikroTik 设备,将家庭网络划分为 IoT、访客、受信任和服务器的 VLAN,包括交换机中继配置、防火墙规则和无线 SSID 映射。在需要将家庭网络拆分为 IoT、访客、受信任和服务器 VLAN 时使用。

家庭实验室 VLAN 隔离 是什么?

该技能详细指导如何在家庭网络环境中实施 VLAN 隔离,以将 IoT 设备、访客网络与主要 PC 等受信任设备隔离开来,从而提升安全性。它支持 UniFi、pfSense/OPNsense 和 MikroTik 三大主流平台,并提供了从 VLAN 设计模板、SSID 映射到防火墙规则配置的完整步骤。技能强调通过明确的“阻止”规则实现隔离,并给出了常见的反模式(如无防火墙规则的 VLAN 等同虚设)和最佳实践(如将 Pi-hole 置于服务器 VLAN 并开放 DNS 端口)。此外,它还涵盖了交换机中继端口与接入端口的区别及配置方法,适合第一次设置 VLAN 或需要排查路由问题的用户。该技能内容基于实际场景编写,所有规则添加隔离,不会移除现有保护,建议在维护窗口逐步应用变更并验证连通性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/homelab-vlan-segmentation

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

家庭实验室 VLAN 隔离 是做什么的?

如何将家庭网络拆分为隔离的 VLAN,使 IoT 设备、访客和您的主电脑无法相互通信。这是家庭网络中最具影响力的安全升级。

此处展示的所有防火墙规则均用于增加网段间的隔离——它们不会移除现有安全保护。请在维护窗口内应用变更,并在每个步骤后验证网段间的连通性,之后再继续。

何时使用

  • 首次在家庭网络上设置 VLAN
  • 隔离 IoT 设备(智能灯泡、摄像头、电视)与受信任设备
  • 创建无法访问家庭设备的访客 Wi-Fi 网络
  • 向不熟悉 VLAN 概念的人解释其工作原理
  • 配置中继端口、接入端口和 SSID 到 VLAN 的映射
  • 在 pfSense/OPNsense/UniFi 上排查 VLAN 间路由或防火墙规则问题

工作原理

无 VLAN —— 扁平网络:
  所有设备在 192.168.1.0/24 网段
  智能电视(潜在恶意软件)→ 可访问您的 NAS、电脑等一切设备

有 VLAN:
  VLAN 10 — 受信任    192.168.10.0/24  (PC、手机、笔记本电脑)
  VLAN 20 — IoT        192.168.20.0/24  (智能电视、灯泡、摄像头)
  VLAN 30 — 服务器    192.168.30.0/24  (NAS、树莓派、虚拟机)
  VLAN 40 — 访客      192.168.40.0/24  (访客 Wi-Fi)
  VLAN 99 — 管理      192.168.99.0/24  (交换机/AP 网页管理界面)

  智能电视 → 被阻止访问 192.168.10.0/24 和 192.168.30.0/24
  访客 → 仅能访问互联网,无法看到任何家庭设备

VLAN 设计模板

VLAN  名称         子网              网关              用途
10    受信任       192.168.10.0/24   192.168.10.1     PC、手机、笔记本电脑
20    IoT          192.168.20.0/24   192.168.20.1     智能家居设备
30    服务器       192.168.30.0/24   192.168.30.1     NAS、树莓派、自托管服务
40    访客         192.168.40.0/24   192.168.40.1     访客 Wi-Fi
99    管理         192.168.99.0/24   192.168.99.1     网络设备管理界面

示例

典型家庭实验室,配有 UniFi AP 和托管交换机:

场景:3 卧室住宅,UniFi Dream Machine + UniFi 8 口交换机 + 2 个 AP

VLAN 10 — 受信任    192.168.10.0/24   MacBook、iPhone、iPad
VLAN 20 — IoT        192.168.20.0/24   Nest 恒温器、Philips Hue、Ring 门铃、智能电视
VLAN 30 — 服务器    192.168.30.0/24   Synology NAS (192.168.30.10)、Pi-hole (192.168.30.2)
VLAN 40 — 访客      192.168.40.0/24   访客 Wi-Fi — 仅能访问互联网

SSID → VLAN 映射:
  "Home"      → VLAN 10 (WPA2,强密码,仅受信任设备)
  "IoT"       → VLAN 20 (WPA2,单独密码,印在路由器上用于设置)
  "Guest"     → VLAN 40 (WPA2,可随意分享的简单密码)

交换机端口行为:
  端口 1  → 中继到路由器(标记 VLAN 10、20、30、40、99)
  端口 2  → 中继到 AP(标记 VLAN 10、20、40;AP 处理按 SSID 标记)
  端口 3  → 接入 VLAN 30(NAS — 未标记,无需 VLAN 感知)
  端口 4  → 接入 VLAN 30(Pi-hole — 未标记)
  端口 5–8 → 接入 VLAN 10(有线工作站)

应用的防火墙规则(所有规则均增加隔离,不移除现有保护):
  IoT → 受信任:阻止
  IoT → 服务器:除 192.168.30.2:53(允许 Pi-hole DNS)外阻止
  IoT → 互联网:允许
  访客 → 本地网络:阻止
  访客 → 互联网:允许
  受信任 → 所有地方:允许

UniFi 配置

在 UniFi Controller 中创建网络

设置 → 网络 → 创建新网络

对于每个 VLAN:
  名称:IoT
  用途:企业型(提供 DHCP + 路由)
  VLAN ID:20
  网络:192.168.20.0/24
  网关 IP:192.168.20.1
  DHCP:启用
  DHCP 范围:192.168.20.100 – 192.168.20.254

将 SSID 映射到 VLAN(UniFi)

设置 → WiFi → 创建新 WiFi

  名称:IoT-Network
  密码:<单独密码>
  网络:IoT  ← 在此选择您的 VLAN
  # 所有连接到此 SSID 的设备都归于 VLAN 20

  名称:Guest
  密码:<访客密码>
  网络:Guest
  访客策略:启用  ← 也将访客之间互相隔离

UniFi 防火墙规则(流量规则)

设置 → 流量与安全 → 流量规则

# 阻止 IoT 访问受信任 VLAN
  操作:阻止
  类别:本地网络
  源:IoT (192.168.20.0/24)
  目标:受信任 (192.168.10.0/24)

# 仅允许 IoT 访问互联网
  操作:允许
  源:IoT
  目标:互联网

# 阻止访客访问所有本地网络
  操作:阻止
  源:Guest
  目标:本地网络

pfSense / OPNsense 配置

创建 VLAN

接口 → 分配 → VLAN → 添加

  父接口:em1(您的 LAN 网卡)
  VLAN 标签:20
  描述:IoT

# 为每个 VLAN 重复操作,然后将每个 VLAN 分配到接口:
接口 → 分配 → 添加
  选择您创建的 VLAN → 点击添加
  启用接口,将 IP 设置为网关地址 (192.168.20.1/24)

为每个 VLAN 配置 DHCP

服务 → DHCP 服务器 → 选择您的 VLAN 接口

  启用 DHCP
  范围:192.168.20.100 至 192.168.20.254
  DNS 服务器:192.168.30.2  ← 如果有 Pi-hole 的话使用其 IP

防火墙规则(pfSense/OPNsense)

# 规则从上到下处理,第一条匹配即生效。

# 在 IoT 接口(VLAN 20)上:
  规则 1:允许 IoT → Pi-hole DNS  ← 必须放在 RFC1918 阻止规则之前
    协议:UDP/TCP
    源:IoT 网络
    目标:192.168.30.2 端口 53
    操作:允许

  规则 2:阻止 IoT → RFC1918(所有私有 IP 范围)
    协议:任意
    源:IoT 网络
    目标:RFC1918 (192.168.0.0/16、10.0.0.0/8、172.16.0.0/12)
    操作:阻止

  规则 3:允许 IoT → 互联网
    协议:任意
    源:IoT 网络
    目标:任意
    操作:允许

# 在受信任接口(VLAN 10)上:
  允许所有(受信任设备可访问一切)
    源:受信任网络
    目标:任意
    操作:允许

# 需要特定本地服务的 IoT 设备的额外例外:
  在规则 2(RFC1918 阻止)之前插入:
    协议:TCP
    源:IoT 网络
    目标:192.168.30.x 端口 8123  ← Home Assistant
    操作:允许

MikroTik 配置

# 步骤 1:创建启用 VLAN 过滤的桥接
/interface bridge
add name=bridge vlan-filtering=yes

# 步骤 2:将物理端口添加到桥接
# 到路由器的中继端口(标记所有 VLAN)
/interface bridge port
add bridge=bridge interface=ether1 frame-types=admit-only-vlan-tagged

# 受信任设备的接入端口(未标记 VLAN 10)
/interface bridge port
add bridge=bridge interface=ether2 pvid=10 frame-types=admit-only-untagged-and-priority-tagged

# IoT 设备的接入端口(未标记 VLAN 20)
/interface bridge port
add bridge=bridge interface=ether3 pvid=20 frame-types=admit-only-untagged-and-priority-tagged

# 步骤 3:定义哪些 VLAN 允许通过哪些端口
/interface bridge vlan
add bridge=bridge tagged=ether1 untagged=ether2 vlan-ids=10
add bridge=bridge tagged=ether1 untagged=ether3 vlan-ids=20

# 步骤 4:在桥接上创建 VLAN 接口(网关 IP)
/interface vlan
add interface=bridge name=vlan10 vlan-id=10
add interface=bridge name=vlan20 vlan-id=20

# 步骤 5:分配网关 IP
/ip address
add interface=vlan10 address=192.168.10.1/24
add interface=vlan20 address=192.168.20.1/24

# 步骤 6:DHCP 地址池和服务器
/ip pool
add name=pool-trusted ranges=192.168.10.100-192.168.10.254
add name=pool-iot ranges=192.168.20.100-192.168.20.254

/ip dhcp-server
add interface=vlan10 address-pool=pool-trusted name=dhcp-trusted
add interface=vlan20 address-pool=pool-iot name=dhcp-iot

/ip dhcp-server network
add address=192.168.10.0/24 gateway=192.168.10.1
add address=192.168.20.0/24 gateway=192.168.20.1

# 步骤 7:防火墙 — 阻止 IoT 访问受信任 VLAN
/ip firewall filter
add chain=forward src-address=192.168.20.0/24 dst-address=192.168.10.0/24 \
    action=drop comment="阻止 IoT 到受信任"

交换机中继端口 vs 接入端口

# 中继端口:承载多个 VLAN(标记)—— 连接交换机到交换机、交换机到路由器、交换机到 AP
# 接入端口:承载一个 VLAN(未标记)—— 连接终端设备(PC、摄像头、NAS)

# 连接到路由器的托管交换机端口应为中继端口:
  允许的 VLAN:10、20、30、40、99

# 连接到 PC 的端口应为接入端口:
  VLAN:10(受信任)
  无标记 —— PC 不知道也不关心 VLAN

# 连接到 AP 的端口必须为中继端口:
  AP 将来自每个 SSID 的流量标记为相应的 VLAN ID
  允许的 VLAN:10、20、40(视 AP 服务的 SSID 而定)

反模式

# 错误:创建 VLAN 却没有添加防火墙规则
# 没有防火墙规则的 VLAN 不提供安全性 —— VLAN 间路由默认是开放的
# 正确:创建 VLAN 后立即添加明确的阻止规则

# 错误:将 Pi-hole 放在 IoT VLAN 中
# IoT 设备可以访问它,但受信任设备不能(除非添加额外规则)
# 正确:Pi-hole 放在服务器 VLAN,并添加一条规则允许所有 VLAN 访问 53 端口

# 错误:本地 VLAN 等于管理 VLAN
# 未标记流量落入管理 VLAN 可能导致 VLAN 跳跃攻击
# 正确:使用专用的未使用 VLAN 作为本地 VLAN(例如 VLAN 999),保持管理流量为标记

# 错误:IoT SSID 和受信任 SSID 使用相同的 Wi-Fi 密码
# 任何知道密码的人都可以将 IoT 设备连接到错误的网段

最佳实践

  • 从 4 个 VLAN 开始:受信任、IoT、服务器、访客 —— 根据需要增加
  • 将 Pi-hole 放在服务器 VLAN (192.168.30.x)
  • 添加一条防火墙规则,允许所有 VLAN 访问 Pi-hole IP 的 DNS(端口 53)—— 且放在任何 RFC1918 阻止规则之前
  • 每次规则修改后测试隔离:从 IoT VLAN ping 一个受信任设备 —— 应该失败
  • 使用管理 VLAN 管理交换机/AP 的 Web 界面,并限制仅受信任 VLAN 可访问
  • 用表格记录您的 VLAN 设计(VLAN ID、名称、子网、用途)

相关技能

  • homelab-network-setup
  • homelab-pihole-dns
  • homelab-wireguard-vpn

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相关技能