> For the complete documentation index, see [llms.txt](https://docs.verge.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.verge.io/knowledge-base/zh/networking/wireguard--setup-remote-access-vpn.md).

# WireGuard - 设置远程访问 VPN

这里是如何使用 VergeOS 内置的 WireGuard 功能来设置远程访问 VPN 的说明。更多信息可在 VergeOS 用户界面的帮助部分找到。

## 在你的内部网络上创建 WireGuard 配置

{% hint style="info" %}
**你可以使用现有的内部网络，或创建一个新的内部网络。**
{% endhint %}

1. 在 VergeOS 界面中，导航到 **网络 -> 内部** 并查看或 **双击** 你想使用的内部网络。
2. 在左侧菜单中，点击 **WireGuard（VPN）**.
3. 点击 **添加新接口**. ![wireguardvpn-img1.png](/files/ac9766434058821bc4b8125bcfd68ce2c8fe0c4b)
4. 输入以下信息：
   * 输入一个唯一的 **名称** 用于此接口。
   * 输入一个 **说明** （可选）。
   * 勾选 **已启用**.
   * 输入 **IP 地址** 作为此 WireGuard 内部网络使用。此 **必须为** 与你现有的内部网络 IP 方案分开。例如，如果你的内部网络使用 192.168.0.1/24，你必须选择另一个不同且唯一的 IP 方案，例如 192.168.255.1/24。
   * 输入 **监听端口** 用于连接 VPN（**默认值：51820**）。这是你将在外部网络上用于将 VPN 流量发送到内部网络的端口。
   * 输入一个 **私钥** 或留空以自动生成密钥。
   * 输入一个 **端点 IP** 或留空，系统将尝试自动检测 IP。我们 **强烈建议** 你手动输入该 IP，以确保配置正确。此 IP 是你的环境的外部 IP，通常与你的 UI IP 相同。你可以通过进入以下位置找到外部 IP： **网络 -> 外部** 并查看你的外部网络。在网络路由器部分，它应是如下所示的 IP 地址： ![wireguardvpn-img3-fixed.png](/files/5b0a4ad86e9b39057e28460e9114dcf405c75061)
5. 点击 **提交** 以添加新接口。
6. 添加接口后，系统会带你进入仪表板，在那里你会看到你的新接口。\
   ![wireguardvpn-img2.png](/files/8f61d7da00f7dd8c7f19efefa440873d8e971b19)
7. 点击 **应用规则** 在左侧菜单栏中以应用防火墙规则。自动创建的规则将接受端口 51820 上发往路由器 IP 和该 **内部网络**. ![wireguardvpn-img-intrules.png](/files/ee03b9a25e54e4ef80ead841a15c5fceaf716096)

## 外部网络 PAT 规则

为了使内部网络能够连接，我们需要一个外部 **PAT** （端口地址转换）规则来转换端口（**默认 51820**）到 **内部网络**.

![2023-09-06\_11\_56\_18-training\_\_\_rules.png](/files/c5e85dc6e735e769ba801b93102c438ee093e2cc)

### 添加外部 PAT 规则

1. 从 **外部网络** 仪表板，点击 **规则** 左侧菜单中的。
2. 点击 **新建** 左侧菜单中的。
3. 输入一个 ***名称*** 这将有助于未来的管理。
4. **可选地**，一个 ***说明*** 可输入，以记录额外的管理信息。
5. 在 ***操作*** 下拉菜单，选择 **转换**.
6. 在 ***协议*** 下拉菜单，选择 **UDP**.
7. 在 ***方向*** 下拉菜单，选择 **入站**.

**源：**

8. 在 ***类型*** 下拉菜单，选择 **任意/无**. **可选地**，如有需要，你可以在此对 VPN 流量进行源地址锁定。

**目标：**

9. 在 ***类型*** 下拉菜单，选择 **我的路由器 IP**。如果你位于 **租户**，将其更改为 **我的 IP 地址** 并选择该 **租户 UI**。这应与 **端点 IP** 上面使用的相同。如果使用与 UI IP 不同的 IP，请创建一个 **SNAT** 规则在 **外部** 网络。
10. 在 ***目标端口/范围*** 字段中，输入 **端口** （默认端口：51820）。

**目标：**

11. 在 ***类型*** 下拉菜单，选择 **其他网络 DMZ IP**.
12. 在 ***目标网络*** 下拉菜单中，选择 **目标网络**.
13. 将 ***目标端口/范围*** 字段留空。
14. 点击 **提交** 以及 **应用规则** 在左侧菜单中以使新规则生效。

## SNAT 规则（如果不使用 UI IP）

如果你正在添加 **WireGuard** 且未使用 **UI 的 IP 地址，**，我们建议创建一个 **SNAT** 规则在 **外部** 网络。

1. 从 **外部网络** 仪表板，点击 **规则** 左侧菜单中的。
2. 点击 **新建** 左侧菜单中的。
3. 输入一个 ***名称*** 这将有助于未来的管理。
4. **可选地**，输入一个 ***说明*** 以获取更多信息。
5. 在 ***操作*** 下拉菜单，选择 **转换**.
6. 在 ***协议*** 下拉菜单，选择 **UDP**.
7. 在 ***方向*** 下拉菜单，选择 **出站**.

**源：**

8. 在 ***类型*** 下拉菜单，选择 **其他网络 DMZ IP**.
9. 在 ***网络*** 在下拉菜单中，选择 WireGuard 所在的内部网络。
10. 将 ***源端口/范围*** 字段留空。

**目标：**

11. 在 ***类型*** 下拉菜单，选择 **任意 / 无**.
12. 将 ***目标端口/范围*** 字段留空。

**目标：**

13. 在 ***类型*** 下拉菜单，选择 **我的 IP 地址**.
14. 在 ***IP 地址*** 下拉菜单中，选择 **IP 地址** 你想使用的。
15. 点击 **提交** 以及 **应用规则** 以启用 SNAT 规则。

{% hint style="info" %}
**此 SNAT 规则强制内部网络的 DMZ IP 发出的任何出站流量使用正确的 IP。默认情况下，它会通过 UI IP 发出，导致抖动问题。**
{% endhint %}

## 添加远程用户对等端

{% hint style="info" %}
**你将为每个连接到 VPN 的用户设置一个对等端。**
{% endhint %}

1. 在 WireGuard 接口页面中，点击 **添加新对等端**. ![wireguardvpn-img4.png](/files/b3d337ab38adf3196b25c5d90b826b428062cf7e)
2. 分配一个 **名称** 给该对等端，例如远程用户的姓名。
3. 可选地，输入一个 **说明**.
4. 检查 **自动生成对等端配置** 复选框。
5. 输入 **端点** 用于该对等端（面向外部的 IP 地址、主机名或 URL）。
6. 对于 **允许的 IP**，输入此对等端的 /32 IP。
7. 在 **配置防火墙** 下拉菜单，选择 **远程用户**.
8. 点击 **提交** 以保存对等端条目。 ![wireguardvpn-img6.png](/files/9ced3ff595c002dc31258a00a5b2fdf33198b942)

### 下载配置文件：

9. 点击 **下载配置** 按钮，然后下载文件。

   ![download-link.png](/files/ae928d609ebecf70a11b1ad8aea4aee00e19029b) ![configuration-file.png](/files/05ece789c31c127e443d2a0d6f54d1aa00fc92ea)

### 在客户端上安装 WireGuard 软件：

WireGuard 客户端软件可从以下地址下载： <https://wireguard.com/install>.

1. 在客户端机器上安装 WireGuard。
2. 点击 **添加隧道**.
3. 导航到并选择生成的配置文件。
4. 点击 **激活** 按钮以打开隧道。 ![tunnel-active.png](/files/5d1d394d3fee97c24e537d3554e27d378e496acd)

***

{% hint style="info" %}
**文档信息**

* 最后更新：2024-08-29
* vergeOS 版本：4.11
  {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.verge.io/knowledge-base/zh/networking/wireguard--setup-remote-access-vpn.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
