场景 链接到标题

办公室有大量 VM(host-a、host-b、host-c 等),它们无法或不适合安装 NetBird agent。家里的笔记本需要能直接通过这些机器的 LAN IP 访问(RustDesk、SSH 等),而不能逐个安装 agent。

方案:路由 peer 链接到标题

NetBird 的 Network Route 功能允许将整个子网通过一个路由 peer 暴露给 VPN 网络。

家里笔记本 ── NetBird 隧道 ── gateway(路由 peer) ── 办公室 LAN 192.168.0.0/24
                                                    ├── host-a      (192.168.0.*)
                                                    ├── host-c      (192.168.0.*)
                                                    └── 其他无 agent 节点

路由 peer(gateway)对流量做 Masquerade(NAT),将来自 NetBird 的包源 IP 替换为自己的 LAN IP,这样目标机器的回包就能正确路由回来。

前置条件 链接到标题

路由 peer 需要开启 IP 转发:

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-netbird-forward.conf
sudo sysctl -p /etc/sysctl.d/99-netbird-forward.conf

第一步:创建 Network Route 链接到标题

source .env

curl -X POST "https://your-management/api/routes" \
  -H "Authorization: Token $NETBIRD_PAT" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Office LAN",
    "network_id": "office-lan",
    "enabled": true,
    "peer": "<gateway_peer_id>",
    "network": "192.168.0.0/24",
    "metric": 100,
    "masquerade": true,
    "groups": ["<all_group_id>"]
  }'

关键参数:

  • peer:路由 peer 的 ID(可通过 GET /api/peers 查询)
  • masquerade: true:开启 NAT,目标机器的回包才能回来
  • groups:哪些节点可以使用这条路由(填 “All” 组则全网可用)

第二步:创建 Access Policy 链接到标题

路由创建后还需要一条 Access Policy 才能激活路由 peer 的转发能力:

curl -X POST "https://your-management/api/policies" \
  -H "Authorization: Token $NETBIRD_PAT" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "route-lan-access",
    "enabled": true,
    "source_posture_checks": [],
    "rules": [{
      "name": "allow-all",
      "enabled": true,
      "action": "accept",
      "bidirectional": true,
      "protocol": "all",
      "sources": ["<all_group_id>"],
      "destinations": ["<all_group_id>"]
    }]
  }'

不创建 Policy 的话,路由 peer 虽然配置了路由规则,但 NetBird 的防火墙会丢弃经过它的流量。

第三步:配置 dnsmasq(可选) 链接到标题

gateway 上运行的 dnsmasq 负责局域网 DNS 解析。将办公室机器的省写名加到 hosts 文件后,客户端就能用 host-a.lanhost-c.lan 这类域名访问:

# /home/ubuntu/dnsmasq/hosts(dnsmasq 容器映射)
192.168.0.*  host-a host-a.lan
192.168.0.*  host-c host-c.lan
192.168.0.*  host-b host-b.lan

macOS 搜索域补全 链接到标题

默认情况下 Mac 上需要输入完整的 host-c.lan 才能解析。要支持短名 host-c,需要在 NetBird Dashboard 中开启搜索域支持:

DNS → Nameservers → 编辑 lan 记录 → 开启 Mark match domains as search domains

开启后:

ping host-c      → 自动补全为 host-c.lan → 192.168.0.* ✅
ping host-a      → 自动补全为 host-a.lan → 192.168.0.* ✅
ssh host-c.lan   → ✅

验证 链接到标题

从家里/外部网络测试:

# 通过路由访问办公室无 agent 节点
ping 192.168.0.*        ✅ 通
ssh user@192.168.0.*   ✅ 通

# 通过 dnsmasq 域名
ping host-c.lan            ✅ 通(如开启搜索域,直接 ping host-c)
ping host-a.lan            ✅ 通

注意事项 链接到标题

  • 路由 peer 本身不能通过 LAN IP 访问,需用 NetBird IP(100.121.*.*
  • 同时运行 Tailscale 的节点上,Tailscale 的 wg0 接口可能有冲突路由,需要停用 Tailscale 或调整路由优先级
  • Masquerade 开启后,目标机器看到的源 IP 是路由 peer 的 LAN IP,不是发起者的 NetBird IP