---
title: 在 OPNsense 中搭建 WireGuard VPN 并从 Windows 客户端连接的步骤
description: "网络操作指南 [![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opnsense-wg.png)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opnsense-wg.png) 本文将实际讲解如何在家庭、分支机构或公司内部把 OPNsense 配置为 WireGuard 服务器，并从 Windows 电脑安全连接的方法。 本文以“访问内部局域网和家庭网络”的使用场景为前提，采用**分离隧道（split tunnel）**方式进行配置。整体流程参考了 OPNsense 官方的 *WireGuard Road Warrior Setup*、通用的 WireGuard 文档，以及 Windows 官方安装指南。^[[1]](#ref-1)^[[2]](#ref-2)^[[4]](#ref-4) 预计所需时间：20–30 分钟 更新日期：2026-03-13 本文构建的配置 Windows 客户端 ⇄ WireGuard 隧道 ⇄ OPNsense - 协议：WireGuard / UDP - 用途：远程访问 - 方式：分离隧道…"
url: "https://bestnetllc.co.jp/zh-hans/docs/%e5%9c%a8-opnsense-%e4%b8%ad%e6%90%ad%e5%bb%ba-wireguard-vpn-%e5%b9%b6%e4%bb%8e-windows-%e5%ae%a2%e6%88%b7%e7%ab%af%e8%bf%9e%e6%8e%a5%e7%9a%84%e6%ad%a5%e9%aa%a4/"
updated: "2026-06-08"
category: "操作指南-BESTNET CLOUD"
---

# 在 OPNsense 中搭建 WireGuard VPN 并从 Windows 客户端连接的步骤

网络操作指南

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opnsense-wg.png)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opnsense-wg.png)

本文将实际讲解如何在家庭、分支机构或公司内部把 OPNsense 配置为 WireGuard 服务器，并从 Windows 电脑安全连接的方法。
本文以“访问内部局域网和家庭网络”的使用场景为前提，采用**分离隧道（split tunnel）**方式进行配置。整体流程参考了 OPNsense 官方的 *WireGuard Road Warrior Setup*、通用的 WireGuard 文档，以及 Windows 官方安装指南。^[[1]](#ref-1)^[[2]](#ref-2)^[[4]](#ref-4)

预计所需时间：20–30 分钟
更新日期：2026-03-13

本文构建的配置

Windows 客户端

⇄

WireGuard 隧道

⇄

OPNsense

- 协议：WireGuard / UDP
- 用途：远程访问
- 方式：分离隧道
- 客户端：官方 WireGuard for Windows

概述

## 概述与前提条件

OPNsense 上的 WireGuard 配置由**实例（Instance，服务端虚拟接口设置）**与**对端（Peers，被授权的连接方）**组合而成。OPNsense 官方文档建议将隧道地址设计为一个独立网络，就如同存在一个单独的物理接口一样。^[[1]](#ref-1)^[[2]](#ref-2)

### 本文目标

让 Windows 电脑能够通过 WireGuard 连接到 OPNsense，并安全访问局域网内的 NAS、RDP、SSH 及业务系统。

### 所采用的连接方式

不会将客户端对整个互联网的流量都发送到 VPN 中，而是采用分离隧道，**仅将指向局域网的流量**发送到隧道中。

### 客户端应用

在 Windows 一侧使用**官方 WireGuard for Windows**。安装程序可从 WireGuard 官方网站获取。^[[4]](#ref-4)

**注意：**OPNsense 的界面布局和项目名称可能因版本而异。
但 Instance / Peer / General / Firewall 这一概念结构在各版本中保持一致。

- 可以登录 OPNsense 管理界面
- WAN 侧可用 UDP 51820 端口
- 如果 OPNsense 位于上游路由器之后，可以将 UDP 51820 转发到 OPNsense 的 WAN 口
- 可以规划一个不与目标局域网网段重叠的 WireGuard 网络
- 可以在 Windows 客户端上安装官方 WireGuard for Windows

示例设计

## 示例配置

为便于说明，本文统一使用以下示例。这些数值仅为示例，正式环境中请替换为贵单位自身的地址规划。

| 项目 | 数值 | 含义 |
| --- | --- | --- |
| 局域网（LAN） | `192.168.10.0/24` | 公司内部或家庭网络 |
| WireGuard 网络 | `10.10.10.0/24` | VPN 隧道专用网段 |
| OPNsense 隧道 IP | `10.10.10.1/24` | 在 Instance 上设置的地址 |
| Windows 客户端 IP | `10.10.10.2/32` | 分配给该 Peer 的专用地址 |
| 公网主机名 | `vpn.example.jp` | WAN 的公网 IP 或 DDNS 名称 |
| 监听端口 | `51820/UDP` | WireGuard 监听端口 |

**Windows 客户端**
10.10.10.2/32⇄**WireGuard 隧道**
10.10.10.0/24⇄**OPNsense**
10.10.10.1/24→**局域网**
192.168.10.0/24**要点：**OPNsense 官方指南建议在 Instance 侧的隧道地址上设置带网络位的 CIDR，例如 `10.10.10.1/24`，服务端 Instance 应避免使用 `/32` 或 `/128`。^[[1]](#ref-1)

步骤 1

## 在 OPNsense 一侧创建 WireGuard 实例

1

### 菜单

打开 `VPN > WireGuard > Instances`，点击 **+** 按钮新增一条记录。^[[1]](#ref-1)

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg01.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg01.webp)

| 项目 | 设置示例 | 备注 |
| --- | --- | --- |
| Enabled | 勾选 | 启用 |
| Name | `HomeWireGuard` | 任意可识别的名称 |
| Public Key / Private Key | 自动生成 | 点击齿轮图标生成新密钥 |
| Listen Port | `51820` | 与其他服务不冲突的 UDP 端口 |
| MTU | `1420` | 可考虑改为 `1412`（针对 PPPoE 环境）^[[1]](#ref-1) |
| Tunnel Address | `10.10.10.1/24` | WireGuard 专用网段 |
| Peers | 留空 | 此阶段先留空，之后再创建 Peer |
| DNS Server | 留空 | 即便在高级模式下，通常也建议留空。若在此处设置，会覆盖 OPNsense 自身的 DNS 配置。^[[1]](#ref-1) |

保存后重新打开该 Instance，记录下**公钥（Public Key）**。
这个公钥就是稍后要填入 Windows 客户端设置中的服务端公钥。^[[1]](#ref-1)

步骤 2

## 为客户端创建 Peer

OPNsense 现行文档推荐在向多个客户端分发配置时使用 **Peer generator**（对端生成器）。它可以选择 Instance、保存 Endpoint、自动分配地址并生成 Config 文本，而 OPNsense 上只保存公钥。由于私钥不会保留在 OPNsense 上，请务必妥善保管生成的配置文件。^[[2]](#ref-2)^[[3]](#ref-3)

### 推荐流程：使用 Peer generator

1. 打开 `VPN > WireGuard > Peer generator`。
2. 选择目标 Instance，输入客户端名称、连接目标的 FQDN/端口、所需网络及 DNS。
3. 将 `10.10.10.2/32` 之类的地址分配为该客户端专用地址。当前实现也支持自动分配地址。^[[3]](#ref-3)
4. 建议将 Keepalive 间隔设置为 25 秒（从在 NAT 与防火墙环境下维持连接的角度出发）
5. DNS 通常留空；但若需要通过 VPN 目标端的 DNS 服务器进行主机名解析，请在此处指定。
6. 勾选 Enable WireGuard
7. 点击**存储并生成下一步（Store and generate next）**以保存公开信息，复制生成的 **Config**，并妥善保存为 `client-windows.conf`。随后执行 **Apply**（在 `Peers` 界面）。^[[2]](#ref-2)

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg02.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg02.webp)

**注意：**Peer generator 生成的私钥不会保存在 OPNsense 上。
如果重新生成，也需要在 Windows 一侧重新导入配置文件。^[[2]](#ref-2)

### 手动创建 Peer

如果不使用 Peer generator，可在 `VPN > WireGuard > Peers` 处新建 Peer，并设置客户端公钥及供客户端使用的 `/32` 地址。^[[1]](#ref-1)

| 项目 | 设置示例 | 备注 |
| --- | --- | --- |
| Enabled WireGuard | 勾选 | 启用 |
| Name | `Windows-Laptop-01` | 用于识别设备的名称 |
| Public Key | 客户端公钥 | 在 Windows 端生成的密钥 |
| Allowed IPs | `10.10.10.2/32` | 分配给该客户端的专用地址 |
| Pre-shared Key | 可选 | 可作为附加安全措施使用^[[2]](#ref-2) |

手动创建 Peer 后，返回原来的 Instance，关联该 Peer，保存并执行 **Apply**。^[[1]](#ref-1)

手动创建用的 `client-windows.conf` 模板

```
[Interface]
PrivateKey = <Windows client private key>
Address = 10.10.10.2/32
DNS = 10.10.10.1

[Peer]
PublicKey = <OPNsense Instance public key>
Endpoint = vpn.example.jp:51820
AllowedIPs = 192.168.10.0/24, 10.10.10.0/24
```

如果使用隧道内 DNS，请确保 DNS 目标地址已包含在 `AllowedIPs` 中。^[[1]](#ref-1)

步骤 3

## 分配并启用接口

1

### 接口分配

在 `Interfaces > Assignments` 中，添加生成的 `wgX` 设备并赋予一个易于识别的名称。^[[1]](#ref-1)

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg03.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg03.webp)

2

### 启用接口

在 `Interfaces > OPTX` 中勾选“Enable Interface”，保存后点击“Apply Changes”

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg04.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg04.webp)

| 项目 | 设置示例 | 备注 |
| --- | --- | --- |
| Enable | 勾选 | 启用该接口 |
| Description | `HomeWireGuard` | 便于在创建规则时加以区分 |
| IPv4 Configuration Type | `None` | 不手动设置 IP |
| IPv6 Configuration Type | `None` | 仅在需要时才进行配置 |

OPNsense 官方指南指出，无需在已分配的接口上直接设置 IP；WireGuard 重启后，Instance 中输入的隧道地址会被自动反映到接口上。^[[1]](#ref-1)

**补充说明：**本文采用分离隧道配置，因此无需为将客户端流量路由到 VPN 而配置 Gateway / Outbound NAT。只有在希望将客户端对外部互联网的全部流量都发送到 VPN（全隧道）时，才需要在 OPNsense 一侧额外进行 NAT / Gateway 配置。^[[1]](#ref-1)

步骤 4

## 创建防火墙规则

官方流程采用两层结构：先**在 WAN 侧放行用于建立隧道的 UDP**，再**在 WireGuard 接口侧放行经过批准的目标地址**。^[[1]](#ref-1)^[[2]](#ref-2)

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg05.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg05.webp)

### WAN 侧规则

| 项目 | 设置示例 | 备注 |
| --- | --- | --- |
| Action | `Pass` | 允许接收 |
| Interface | `WAN` | WAN 接收侧 |
| Protocol | `UDP` | WireGuard 使用 UDP |
| Destination | `WAN address` | 自身的 WAN |
| Destination Port | `51820` | 需与 Instance 的 Listen Port 保持一致 |
| Description | `Allow WireGuard` | 可选 |

**运维提示：**如果操作地点固定或仅从跳板机接入，还可以通过将 WAN 规则的**来源（Source）**限制为该 IP 或网段，进一步缩小对外暴露面。

### WireGuard 接口侧规则

[![](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg06.webp)](https://bestnetllc.co.jp/wp-content/uploads/2026/03/opns-wg06.webp)

| 项目 | 设置示例 | 备注 |
| --- | --- | --- |
| Action | `Pass` | 允许隧道流量 |
| Interface | `HomeWireGuard` | 已分配的 WireGuard 接口 |
| Protocol | `any` | 用于初步的连通性测试可以接受 |
| Source | `HomeWireGuard net` | 隧道网络 |
| Destination | `192.168.10.0/24` | 先仅允许局域网 |

OPNsense 官方指南允许将 WireGuard 规则的目标地址设置为 `any`，但在实际运维中，建议**仅限定为所需的子网和服务器**。^[[1]](#ref-1)

PPPoE 环境或部分 TCP 通信不稳定时的 MTU / MSS 调整

官方指南中给出了在 PPPoE 环境下将 Instance MTU 降至 `1412` 的示例，并进一步提供了按需通过 `Firewall > Settings > Normalization` 添加 MSS clamping 的说明。当出现仅 TCP 通信不稳定或仅 Web 访问失败等症状时，这一方法较为有效。^[[1]](#ref-1)

| 项目 | 设置示例 |
| --- | --- |
| Interface | `WireGuard (Group)` |
| Description | `WireGuard MSS Clamping IPv4` |
| Max MSS | `1380`（PPPoE 环境下为 `1372`） |

步骤 5

## 将配置部署到 Windows 客户端

在 Windows 一侧，使用**官方 WireGuard for Windows** 是最简便的方法。安装程序可从 WireGuard 官方网站获取。^[[4]](#ref-4)

1. 安装 WireGuard for Windows。
2. 准备从 OPNsense Peer generator 保存下来的 `client-windows.conf`。若为手动创建，请使用前文提供的模板。
3. 将该配置文件导入 WireGuard for Windows。
4. 启用目标隧道并开始连接。

**补充说明：**在 NAT 后方或移动网络等会话可能不稳定的环境下，可以考虑使用 Keepalive 间隔。OPNsense 官方文档也指出，Keepalive 间隔对维持 NAT / 防火墙穿透是有效的。^[[2]](#ref-2)分离隧道客户端配置示例
client-windows.conf

```
[Interface]
PrivateKey = <Client private key>
Address = 10.10.10.2/32
DNS = 10.10.10.1

[Peer]
PublicKey = <OPNsense public key>
Endpoint = vpn.example.jp:51820
AllowedIPs = 192.168.10.0/24, 10.10.10.0/24
```

通过将 `192.168.10.0/24` 之类的局域网网段包含在 `AllowedIPs` 中，只有该目标流量会经由隧道发送。如果希望将客户端的全部流量都通过 VPN 路由，可按官方指南使用 `0.0.0.0/0` 或 `::/0`，但同时也需要在 OPNsense 一侧额外进行 NAT / 路由设计。^[[1]](#ref-1)

步骤 6

## 连接验证

- 在 Windows 客户端上启用隧道
- 测试与 `10.10.10.1`（OPNsense 隧道 IP）的连通性
- 对局域网内的代表性主机尝试 `ping`、RDP、SSH 等连接（例如：`192.168.10.10`）
- 按需测试名称解析，确认 DNS 是否正常工作
- 在 `VPN > WireGuard > Status` 中检查握手情况及收发统计信息^[[2]](#ref-2)

运维

## 正式对外开放前应实施的加固措施

### 每个 Peer 分配一个 /32

为每台客户端分配唯一的 `/32` 地址，可以更方便地按设备追踪规则与日志。^[[1]](#ref-1)

### 仅允许必要的目标地址

相比将 WireGuard 接口规则的目标地址保持为 `any`，将其限定为局域网或特定服务器组会更加安全。

### 针对固定地点的来源限制

如果能将 WAN 规则的来源限制为固定 IP，就能进一步收窄可达范围。

### 按需添加 PSK

OPNsense 的 Peer 可以附加预共享密钥（PSK），官方指南也建议将其作为附加的安全措施。^[[2]](#ref-2)

### Status 与 Log File 是基本功

利用 `Status` 与 `Log File` 检查握手情况、数据量以及配置错误，是最基本的运维手段。^[[2]](#ref-2)

### DNS 也应遵循最小权限原则

如果使用隧道内的 DNS，请一并确认目标是否可达、是否已包含在 AllowedIPs 中，以及是否已在 Unbound 接口中注册。^[[1]](#ref-1)

故障排查

## 连接失败时的检查清单

没有出现握手

首先检查 WAN 侧是否放行 UDP 51820、上游路由器的端口转发是否正确、客户端一侧 Endpoint 的 FQDN/端口是否正确，以及客户端时间是否存在明显偏差。

出现了握手，但无法访问局域网

请检查 Peer 的 `Allowed IPs`、客户端一侧的 `AllowedIPs`，以及 WireGuard 接口规则的目标地址。OPNsense 官方文档也指出，Allowed IPs 设置错误会导致数据包被静默丢弃。^[[2]](#ref-2)

ICMP 正常，但部分 Web 或 TCP 流量不稳定

请怀疑是 MTU / MSS 的影响。若为 PPPoE 环境，可尝试将 Instance MTU 设为 `1412`，并配置 Normalization 的 MSS clamping。^[[1]](#ref-1)

仅 DNS 失败

请确认客户端上指定的 DNS 目标地址是否已包含在 `AllowedIPs` 中。如果 Unbound DNS 采用全接口注册方式运行，在新增 WireGuard 接口后需要重新加载 Unbound。^[[1]](#ref-1)

找不到 WireGuard 菜单 / 界面看起来不一样

请检查版本差异。Peer generator 相关区域在 24.1 系列中经历了多次增量改进，界面外观和项目排布会因版本而异。^[[3]](#ref-3)

信息来源

## 参考信息

1. OPNsense Documentation, *WireGuard Road Warrior Setup*
   [https://docs.opnsense.org/manual/how-tos/wireguard-client.html](https://docs.opnsense.org/manual/how-tos/wireguard-client.html)
2. OPNsense Documentation, *Virtual Private Networking > WireGuard*
   [https://docs.opnsense.org/manual/vpnet.html](https://docs.opnsense.org/manual/vpnet.html)
3. OPNsense Documentation, *24.1 “Savvy Shark” Series*（包含 Peer generator 相关更新）
   [https://docs.opnsense.org/releases/CE_24.1.html](https://docs.opnsense.org/releases/CE_24.1.html)
4. WireGuard Official, *Installation*
   [https://www.wireguard.com/install/](https://www.wireguard.com/install/)
