Communitygithub.com

家庭實驗室 VLAN 分割

使用 UniFi、pfSense/OPNsense 和 MikroTik 將家庭網路分割成 IoT、訪客、信任和伺服器 VLAN,包含交換器主幹設定、防火牆規則和無線 SSID 對應。在需要將家庭網路分割成 IoT、訪客、信任和伺服器 VLAN 時使用,支援 UniFi、pfSense/OPNsense 或 MikroTik 平台。

家庭實驗室 VLAN 分割 是什麼?

這個「家庭實驗室 VLAN 分割」技能提供了一個完整的方案,用於將家庭網路分割成多個隔離的 VLAN,以增強安全性。它詳細說明了如何在 UniFi、pfSense/OPNsense 和 MikroTik 這三種常見的網路平台上進行設定,包括交換器主幹和存取埠的配置、SSID 到 VLAN 的映射,以及防火牆規則的建立。透過將 IoT 設備、訪客、信任設備和伺服器分置於不同的 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、PC、所有設備

有了 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  (交換器/無線基地台網頁管理介面)

  智慧電視 → 被封鎖,無法存取 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 無線基地台和受管交換器的典型家庭實驗室:

情境:3 房住宅,UniFi Dream Machine + UniFi 8 埠交換器 + 2 個無線基地台

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,可隨意分享的簡單密碼)

交換器埠行為:
  Port 1  → 主幹至路由器(標記 VLAN 10、20、30、40、99)
  Port 2  → 主幹至無線基地台(標記 VLAN 10、20、40;無線基地台處理每 SSID 的標記)
  Port 3  → 存取埠 VLAN 30(NAS — 未標記,不需 VLAN 感知)
  Port 4  → 存取埠 VLAN 30(Pi-hole — 未標記)
  Port 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)

設定 → Wi-Fi → 建立新 Wi-Fi

  名稱: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 的話

防火牆規則(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(已標記)— 連接交換器到交換器、交換器到路由器、交換器到無線基地台
# 存取埠:承載一個 VLAN(未標記)— 連接終端裝置(PC、攝影機、NAS)

# 連接到路由器的受管交換器埠應設為主幹埠:
  允許的 VLAN:10、20、30、40、99

# 連接到 PC 的連接埠應設為存取埠:
  VLAN:10(信任)
  無需標記 — PC 不知道也不關心 VLAN

# 連接到無線基地台的連接埠必須設為主幹埠:
  無線基地台會將來自每個 SSID 的流量標記上正確的 VLAN ID
  允許的 VLAN:10、20、40  (無線基地台服務的任意 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 來存取交換器和無線基地台的網頁管理介面,並僅允許信任 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.

相關技能