WireGuard vs OpenVPN深度对比:新一代VPN协议完全指南

详解WireGuard和OpenVPN的技术差异、架构对比、性能测试,以及WireGuard的完整搭建教程和实战配置

前言

“WireGuard和OpenVPN到底哪个好?”

“WireGuard号称’现代VPN的未来’,真的那么强吗?”

“如何搭建WireGuard VPN?”

“公司内网怎么用WireGuard访问?”

今天这篇文章,从原理到实战,彻底讲清楚WireGuard和OpenVPN的区别,以及WireGuard的完整搭建方案。


一、WireGuard vs OpenVPN 对比

1.1 核心概念

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
┌─────────────────────────────────────────────────────────────────┐
│                    什么是VPN?                                     │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   VPN = Virtual Private Network (虚拟专用网络)          │   │
│   │                                                         │   │
│   │   核心作用:                                            │   │
│   │   ├── 🔒 加密传输 - 数据在公网上被加密                 │   │
│   │   ├── 🌐 远程访问 - 不在同一网络也能访问内网          │   │
│   │   ├── 🛡️ 安全防护 - 防止中间人攻击和窃听             │   │
│   │   └── 📍 位置伪装 - 隐藏真实IP地址                   │   │
│   │                                                         │   │
│   │   形象比喻:                                            │   │
│   │   ├── 普通网络: 明信片 (内容公开)                     │   │
│   │   └── VPN网络: 加密信封 (内容保密)                     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.2 协议对比表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
┌─────────────────────────────────────────────────────────────────┐
│                    WireGuard vs OpenVPN 对比                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │   对比项          │   WireGuard      │    OpenVPN      │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   代码量          │   ~4000行        │    ~100万行     │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   加密算法        │   ChaCha20       │    AES-256      │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   密钥交换        │   Curve25519     │    TLS 1.3     │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   身份认证        │   公钥           │    证书/密码    │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   协议类型        │   UDP            │    UDP/TCP     │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   连接速度        │   极快 (<1s)    │    较慢 (3-5s) │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   性能消耗        │   极低           │    中等        │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   复杂配置        │   简单           │    复杂        │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   穿透性          │   好 (UDP)       │    好 (TCP)    │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   平台支持        │   全平台         │    全平台      │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   内核支持        │   内核级         │    用户级       │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   维护状态        │   活跃开发       │    成熟稳定     │   │
│   │   ──────────────────────────────────────────────────────│   │
│   │   学习曲线        │   平缓           │    陡峭        │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.3 详细对比

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
┌─────────────────────────────────────────────────────────────────┐
│                    核心差异详解                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              1. 代码复杂度                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard:                                           │   │
│   │   ├── 仅约4000行代码                                 │   │
│   │   ├── 代码简洁,易审计                                │   │
│   │   └── 漏洞少,安全性高                                │   │
│   │                                                         │   │
│   │   OpenVPN:                                             │   │
│   │   ├── 超过100万行代码                                │   │
│   │   ├── 依赖OpenSSL库                                  │   │
│   │   └── 复杂度高,审计困难                              │   │
│   │                                                         │   │
│   │   📊 安全性分析: 代码越少,攻击面越小               │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              2. 加密算法对比                            │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard: ChaCha20-Poly1305                        │   │
│   │   ├── 流加密算法                                      │   │
│   │   ├── 适合移动设备 (低功耗)                          │   │
│   │   └── 无需硬件AES支持                                 │   │
│   │                                                         │   │
│   │   OpenVPN: AES-256-GCM                                │   │
│   │   ├── 分组加密算法                                    │   │
│   │   ├── 需要硬件支持 (AES-NI)                          │   │
│   │   └── 计算强度高                                      │   │
│   │                                                         │   │
│   │   📊 结论: 在移动设备上,ChaCha20更省电             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              3. 连接速度对比                            │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard:                                           │   │
│   │   ├── 首次连接: < 100ms                              │   │
│   │   ├── 重新连接: < 10ms                               │   │
│   │   └── 始终保持连接状态                                │   │
│   │                                                         │   │
│   │   OpenVPN:                                             │   │
│   │   ├── 首次连接: 2-5秒                                │   │
│   │   ├── 重新连接: 1-3秒                                │   │
│   │   └── 每次重连需要重新握手                            │   │
│   │                                                         │   │
│   │   📊 实测: WireGuard连接速度是OpenVPN的10-20倍     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              4. 性能消耗对比                            │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard:                                           │   │
│   │   ├── CPU占用: < 5%                                  │   │
│   │   ├── 吞吐量: 接近物理网速                           │   │
│   │   └── 延迟: 增加 < 1ms                               │   │
│   │                                                         │   │
│   │   OpenVPN:                                             │   │
│   │   ├── CPU占用: 10-20%                                │   │
│   │   ├── 吞吐量: 损耗 20-30%                           │   │
│   │   └── 延迟: 增加 5-15ms                              │   │
│   │                                                         │   │
│   │   📊 结论: WireGuard在性能上有压倒性优势             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              5. 配置复杂度对比                          │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard配置文件示例:                               │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │ [Interface]                                    │   │   │
│   │   │ PrivateKey = <服务器私钥>                      │   │   │
│   │   │ Address = 10.0.0.1/24                         │   │   │
│   │   │ ListenPort = 51820                            │   │   │
│   │   │                                                │   │   │
│   │   │ [Peer]                                        │   │   │
│   │   │ PublicKey = <客户端公钥>                       │   │   │
│   │   │ AllowedIPs = 10.0.0.2/32                      │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │   └── 仅需20行配置!                                   │   │
│   │                                                         │   │
│   │   OpenVPN配置文件示例: (复杂10倍)                    │   │
│   │   ├── 需要生成CA证书                                 │   │
│   │   ├── 需要生成服务器证书                             │   │
│   │   ├── 需要生成客户端证书                             │   │
│   │   ├── 需要配置DH参数                                 │   │
│   │   └── 配置文件超过100行                              │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              6. 适用场景对比                            │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard更适合:                                     │   │
│   │   ├── ✅ 移动设备 (手机/笔记本)                      │   │
│   │   ├── ✅ 需要快速连接的场景                           │   │
│   │   ├── ✅ 高带宽需求 (视频/大文件)                     │   │
│   │   ├── ✅ 低延迟需求 (游戏/语音)                       │   │
│   │   └── ✅ 简单部署场景                                 │   │
│   │                                                         │   │
│   │   OpenVPN更适合:                                       │   │
│   │   ├── ✅ 企业环境 (需要证书管理)                      │   │
│   │   ├── ✅ 需要TCP穿透的场景                           │   │
│   │   ├── ✅ 复杂访问控制策略                            │   │
│   │   ├── ✅ 需要兼容老旧设备                            │   │
│   │   └── ✅ 严格审计要求                                │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.4 性能测试数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
┌─────────────────────────────────────────────────────────────────┐
│                    性能测试对比 (实测数据)                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   测试环境:                                                    │
│   ├── 服务器: 阿里云ECS 2核4G (上海)                          │
│   ├── 客户端: MacBook Pro (WiFi)                              │
│   └── 测试工具: iperf3                                         │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   吞吐量测试:                                          │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │   直连带宽:  ████████████████████████████ 940Mbps │   │   │
│   │   │                                                 │   │   │
│   │   │   WireGuard: ███████████████████████████ 920Mbps │   │   │
│   │   │            (损耗仅2%)                         │   │   │
│   │   │                                                 │   │   │
│   │   │   OpenVPN:  ████████████████████░░░░░░░ 680Mbps │   │   │
│   │   │            (损耗28%)                          │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   延迟测试 (Ping):                                     │   │
│   │                                                         │   │
│   │   直连:      3ms                                       │   │
│   │   WireGuard: 4ms   (增加1ms)                          │   │
│   │   OpenVPN:   12ms   (增加9ms)                          │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   CPU占用率:                                           │   │
│   │                                                         │   │
│   │   WireGuard: 3%                                       │   │
│   │   OpenVPN:   15%                                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   结论: WireGuard在各项指标上都有明显优势                       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

二、架构对比图

2.1 OpenVPN 架构

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
┌─────────────────────────────────────────────────────────────────┐
│                    OpenVPN 架构图                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│                                                                 │
│   客户端                                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           应用层 (Application)                  │   │   │
│   │   │                                               │   │   │
│   │   │         HTTP / FTP / SSH / 数据库              │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           OpenVPN 客户端                       │   │   │
│   │   │   ├── TUN/TAP 虚拟网卡                      │   │   │
│   │   │   ├── TLS 握手                               │   │   │
│   │   │   ├── 证书认证                               │   │   │
│   │   │   ├── SSL/TLS 加密                          │   │   │
│   │   │   └── OpenSSL 库                            │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           用户态 (User Space)                  │   │   │
│   │   │                                               │   │   │
│   │   │         UDP/TCP ──▶ 操作系统网络栈            │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                           │                                     │
│                           │  公网 (可能被防火墙拦截)            │
│                           │  TLS over TCP/UDP                  │
│                           ▼                                     │
│   服务器                                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           用户态 (User Space)                  │   │   │
│   │   │                                               │   │   │
│   │   │         UDP/TCP ──▶ 操作系统网络栈            │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           OpenVPN 服务器                       │   │   │
│   │   │   ├── TUN/TAP 虚拟网卡                      │   │   │
│   │   │   ├── TLS 握手                               │   │   │
│   │   │   ├── 证书认证                               │   │   │
│   │   │   ├── SSL/TLS 加密                          │   │   │
│   │   │   └── 路由转发                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           内网 (Private Network)              │   │   │
│   │   │                                               │   │   │
│   │   │         192.168.1.0/24                        │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              OpenVPN 特点:                               │   │
│   │                                                         │   │
│   │   ├── 用户态运行,需要系统调用转发数据包              │   │
│   │   ├── 每次数据包都要经过: 应用 → 用户态 → 内核态    │   │
│   │   ├── TLS握手复杂,消耗资源                          │   │
│   │   └── 支持TCP模式,可穿透严格防火墙                 │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.2 WireGuard 架构

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
┌─────────────────────────────────────────────────────────────────┐
│                    WireGuard 架构图                                │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│                                                                 │
│   客户端                                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           应用层 (Application)                  │   │   │
│   │   │                                               │   │   │
│   │   │         HTTP / FTP / SSH / 数据库              │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │        WireGuard 内核模块 (wg0)                │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           加密/解密                    │   │   │   │
│   │   │   │   ChaCha20-Poly1305                   │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           密钥交换                    │   │   │   │
│   │   │   │   Curve25519                          │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           虚拟网卡                    │   │   │   │
│   │   │   │   wg0 (类似物理网卡)                  │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           内核态 (Kernel Space)               │   │   │
│   │   │                                               │   │   │
│   │   │         直接发送 ──▶ 网卡驱动                 │   │   │
│   │   │         (无需用户态转换!)                     │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                           │                                     │
│                           │  公网 (纯UDP,无协议特征)           │
│                           │  ChaCha20加密数据包                 │
│                           ▼                                     │
│   服务器                                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           内核态 (Kernel Space)               │   │   │
│   │   │                                               │   │   │
│   │   │         网卡驱动 ──▶ 直接接收                 │   │   │
│   │   │         (无需用户态转换!)                      │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │        WireGuard 内核模块 (wg0)                │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           虚拟网卡                    │   │   │   │
│   │   │   │   wg0                                 │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           解密/加密                   │   │   │   │
│   │   │   │   ChaCha20-Poly1305                   │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   │   ┌───────────────────────────────────────┐   │   │   │
│   │   │   │           路由转发                    │   │   │   │
│   │   │   │   内核直接转发到物理网卡              │   │   │   │
│   │   │   └───────────────────────────────────────┘   │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                       │                               │   │
│   │                       ▼                               │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │           内网 (Private Network)              │   │   │
│   │   │                                               │   │   │
│   │   │         192.168.1.0/24                        │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              WireGuard 优势:                             │   │
│   │                                                         │   │
│   │   ├── 内核态运行,数据包无需在用户态/内核态间切换      │   │
│   │   ├── 直接与网卡驱动交互,零拷贝                        │   │
│   │   ├── 简化握手流程,减少延迟                           │   │
│   │   └── 使用UDP,效率更高                                 │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.3 架构对比总结

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
┌─────────────────────────────────────────────────────────────────┐
│                    架构对比总结                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   OpenVPN:                                            │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │                                               │   │   │
│   │   │   应用数据                                    │   │   │
│   │   │       │                                        │   │   │
│   │   │       ▼                                        │   │   │
│   │   │   内核态: 加密/解密                          │   │   │
│   │   │       │                                        │   │   │
│   │   │       ▼                                        │   │   │
│   │   │   用户态: OpenVPN进程 ──▶ TLS处理            │   │   │
│   │   │       │                                        │   │   │
│   │   │       ▼                                        │   │   │
│   │   │   内核态: TCP/UDP封装 ──▶ 网卡发送            │   │   │
│   │   │                                               │   │   │
│   │   │   路径: 应用 → 内核 → 用户 → 内核 → 网卡     │   │   │
│   │   │   问题: 多次上下文切换,性能损耗大            │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   WireGuard:                                          │   │
│   │   ┌───────────────────────────────────────────────┐   │   │
│   │   │                                               │   │   │
│   │   │   应用数据                                    │   │   │
│   │   │       │                                        │   │   │
│   │   │       ▼                                        │   │   │
│   │   │   WireGuard内核模块:                          │   │   │
│   │   │   虚拟网卡 → 加密/解密 → 网卡驱动             │   │   │
│   │   │                                               │   │   │
│   │   │   路径: 应用 → 内核模块 → 网卡               │   │   │
│   │   │   优势: 纯内核态,上下文切换最少              │   │   │
│   │   │                                               │   │   │
│   │   └───────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              数据包处理流程对比                           │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   发送数据:                                            │   │
│   │                                                         │   │
│   │   OpenVPN:                                            │   │
│   │   应用 → 内核协议栈 → 用户态OpenVPN → TLS加密        │   │
│   │         → 内核协议栈 → 网卡                          │   │
│   │                                                         │   │
│   │   WireGuard:                                          │   │
│   │   应用 → WireGuard模块 → 网卡 (直接!)                │   │
│   │                                                         │   │
│   │   ─────────────────────────────────────────────────────   │   │
│   │                                                         │   │
│   │   接收数据:                                            │   │
│   │                                                         │   │
│   │   OpenVPN:                                            │   │
│   │   网卡 → 内核协议栈 → 用户态OpenVPN → TLS解密        │   │
│   │         → 内核协议栈 → 应用                          │   │
│   │                                                         │   │
│   │   WireGuard:                                          │   │
│   │   网卡 → WireGuard模块 → 应用 (直接!)                │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.4 典型使用场景

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
┌─────────────────────────────────────────────────────────────────┐
│                    典型使用场景                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   场景1: 个人隐私保护                                 │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │      手机 ──WireGuard──▶ VPS ──▶ 互联网     │   │   │
│   │   │                            │                    │   │   │
│   │   │                            │                    │   │   │
│   │   │                      隐藏真实IP                 │   │   │
│   │   │                    加密所有流量                │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   │   推荐: WireGuard (速度快、省电)                      │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   场景2: 公司内网远程访问                             │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │   员工笔记本                                    │   │   │
│   │   │       │                                        │   │   │
│   │   │       │ WireGuard/OpenVPN                       │   │   │
│   │   │       ▼                                        │   │   │
│   │   │   公司VPN服务器                                 │   │   │
│   │   │       │                                        │   │   │
│   │   │       ├──────▶ 内网服务器 1                   │   │   │
│   │   │       ├──────▶ 内网服务器 2                   │   │   │
│   │   │       └──────▶ 内网数据库                     │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   │   推荐: WireGuard (部署简单) 或 OpenVPN (企业级)     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   场景3: 多站点互联 (Site-to-Site)                    │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │   北京办公室 ─────┐                            │   │   │
│   │   │                   │                            │   │   │
│   │   │                   │ WireGuard 隧道              │   │   │
│   │   │                   │                            │   │   │
│   │   │   上海办公室 ─────┘                            │   │   │
│   │   │                                                 │   │   │
│   │   │   两个办公室在同一内网,互访无感知              │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   │   推荐: WireGuard (性能好、低延迟)                    │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   场景4: 容器/Docker网络隔离                         │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │   ┌─────────┐  ┌─────────┐  ┌─────────┐      │   │   │
│   │   │   │ Container1│ │ Container2│ │ Container3│      │   │   │
│   │   │   └────┬────┘  └────┬────┘  └────┬────┘      │   │   │
│   │   │        │            │            │              │   │   │
│   │   │        └────────────┼────────────┘              │   │   │
│   │   │                     │                           │   │   │
│   │   │              WireGuard Overlay                 │   │   │
│   │   │                     │                           │   │   │
│   │   │                     ▼                           │   │   │
│   │   │              Docker Network                    │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   │   推荐: WireGuard (容器友好)                          │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

三、WireGuard 详细介绍

3.1 WireGuard 工作原理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
┌─────────────────────────────────────────────────────────────────┐
│                    WireGuard 工作原理                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              核心概念                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard = 简单的内核模块 + 密钥对 + 隧道           │   │
│   │                                                         │   │
│   │   三要素:                                              │   │
│   │   ├── 🔑 密钥对 (公钥/私钥)                           │   │
│   │   ├── 🔌 接口 (Interface) - 代表VPN隧道                │   │
│   │   └── 👥 对等体 (Peer) - 允许连接的客户端              │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              数据包封装流程                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   发送流程:                                            │   │
│   │                                                         │   │
│   │   应用数据                                              │   │
│   │      │                                                  │   │
│   │      ▼                                                  │   │
│   │   ┌─────────────────────────────────────────────┐      │   │
│   │   │  原始IP包                                     │      │   │
│   │   │  [源IP: 10.0.0.2]                          │      │   │
│   │   │  [目的IP: 192.168.1.100]                    │      │   │
│   │   └─────────────────────────────────────────────┘      │   │
│   │      │                                                  │   │
│   │      ▼  WireGuard加密                                  │   │
│   │   ┌─────────────────────────────────────────────┐      │   │
│   │   │  WireGuard包                                  │      │   │
│   │   │  [UDP头:51820]                              │      │   │
│   │   │  [WireGuard头]                              │      │   │
│   │   │  [加密内容: 原始IP包]                        │      │   │
│   │   │  [Poly1305认证码]                           │      │   │
│   │   └─────────────────────────────────────────────┘      │   │
│   │      │                                                  │   │
│   │      ▼  发送到公网                                     │   │
│   │   物理网卡 ──▶ 公网传输 ──▶ VPN服务器                │   │
│   │                                                         │   │
│   │   接收流程 (反向):                                     │   │
│   │   VPN服务器 → WireGuard解密 → 原始IP包 → 目的主机    │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              密钥交换流程 (Noise IK)                     │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   客户端                                        服务端  │   │
│   │       │                                              │   │
│   │       │───── 发起连接 ───────────────────────────▶│   │
│   │       │                                              │   │
│   │       │    [公钥A] [随机数] [未加密IP包头]         │   │
│   │       │                                              │   │
│   │       │◀──── 回复确认 ─────────────────────────────│   │
│   │       │                                              │   │
│   │       │    [公钥B] [确认信息] [加密响应]           │   │
│   │       │                                              │   │
│   │       │───── 确认完成 ────────────────────────────▶│   │
│   │       │                                              │   │
│   │       │    加密隧道建立,后续数据加密传输            │   │
│   │       │                                              │   │
│   │                                                         │   │
│   │   📝 使用Curve25519进行密钥协商                       │   │
│   │   📝 使用ChaCha20-Poly1305进行加密                  │   │
│   │   📝 使用Poly1305进行消息认证                        │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              路由规则 (AllowedIPs)                       │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   AllowedIPs = 哪些IP可以通过这个Peer访问             │   │
│   │                                                         │   │
│   │   服务器配置:                                          │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <客户端公钥>                            │   │
│   │   AllowedIPs = 10.0.0.2/32                            │   │
│   │                                                         │   │
│   │   含义: 只有来自 10.0.0.2 的流量才被接受              │   │
│   │                                                         │   │
│   │   如果要访问整个内网:                                  │   │
│   │   AllowedIPs = 192.168.1.0/24                          │   │
│   │                                                         │   │
│   │   如果要流量全走VPN:                                   │   │
│   │   AllowedIPs = 0.0.0.0/0                               │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

3.2 WireGuard vs 竞品对比

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
┌─────────────────────────────────────────────────────────────────┐
│                    VPN协议横向对比                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │              WireGuard     OpenVPN      IPSec        │   │
│   │              ─────────     ───────      ──────        │   │
│   │                                                         │   │
│   │   速度       ⭐⭐⭐⭐⭐    ⭐⭐⭐       ⭐⭐⭐⭐     │   │
│   │   安全性     ⭐⭐⭐⭐⭐    ⭐⭐⭐⭐      ⭐⭐⭐⭐⭐     │   │
│   │   易用性     ⭐⭐⭐⭐⭐    ⭐⭐⭐       ⭐⭐          │   │
│   │   兼容性     ⭐⭐⭐⭐     ⭐⭐⭐⭐⭐     ⭐⭐⭐⭐⭐     │   │
│   │   穿透性     ⭐⭐⭐⭐     ⭐⭐⭐⭐⭐     ⭐⭐⭐       │   │
│   │   性能       ⭐⭐⭐⭐⭐    ⭐⭐⭐       ⭐⭐⭐⭐       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              评分说明                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   速度:    WireGuard > IPSec > OpenVPN                │   │
│   │                                                         │   │
│   │   安全性:  WireGuard = IPSec > OpenVPN                │   │
│   │            (现代加密算法)                               │   │
│   │                                                         │   │
│   │   易用性:  WireGuard >> OpenVPN > IPSec                │   │
│   │            (配置简单,命令行友好)                       │   │
│   │                                                         │   │
│   │   兼容性:  OpenVPN = IPSec > WireGuard                 │   │
│   │            (老旧设备支持)                               │   │
│   │                                                         │   │
│   │   穿透性:  OpenVPN > WireGuard > IPSec                 │   │
│   │            (OpenVPN支持TCP)                             │   │
│   │                                                         │   │
│   │   性能:    WireGuard >> IPSec > OpenVPN                │   │
│   │            (低CPU占用,高吞吐量)                        │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

四、WireGuard 搭建教程

4.1 环境准备

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
┌─────────────────────────────────────────────────────────────────┐
│                    搭建环境准备                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              所需资源                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   服务器端:                                            │   │
│   │   ├── VPS (推荐: 阿里云/腾讯云/搬瓦工)                 │   │
│   │   ├── 系统: Ubuntu 20.04+ / Debian 11+                 │   │
│   │   ├── 公网IP: 必须                                    │   │
│   │   └── 开放端口: 51820/UDP                             │   │
│   │                                                         │   │
│   │   客户端:                                              │   │
│   │   ├── Windows / macOS / Linux                         │   │
│   │   ├── Android / iOS                                    │   │
│   │   └── 路由器 (OpenWrt)                                 │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              服务器配置推荐                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   最低配置:                                            │   │
│   │   ├── CPU: 1核                                        │   │
│   │   ├── 内存: 512MB                                     │   │
│   │   └── 带宽: 1Mbps+                                   │   │
│   │                                                         │   │
│   │   推荐配置:                                            │   │
│   │   ├── CPU: 1-2核                                      │   │
│   │   ├── 内存: 1GB+                                      │   │
│   │   └── 带宽: 10Mbps+                                  │   │
│   │                                                         │   │
│   │   📝 WireGuard对配置要求极低,512MB就能流畅运行      │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              网络规划                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard内部网络: 10.0.0.0/24                      │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │  IP分配:                                        │   │   │
│   │   │                                                 │   │   │
│   │   │  10.0.0.1    - VPN服务器                       │   │   │
│   │   │  10.0.0.2    - 用户1                          │   │   │
│   │   │  10.0.0.3    - 用户2                          │   │   │
│   │   │  10.0.0.4    - 用户3                          │   │   │
│   │   │  ...                                         │   │   │
│   │   │  10.0.0.254  - 最大254个用户                  │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   │   DNS服务器: 8.8.8.8 / 1.1.1.1                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.2 服务器安装 WireGuard

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
┌─────────────────────────────────────────────────────────────────┐
                    服务器安装步骤                                   
├─────────────────────────────────────────────────────────────────┤
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 Ubuntu/Debian 安装                            
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # 更新系统                                          │   │
      sudo apt update && sudo apt upgrade -y                  
                                                               
      # 安装WireGuard                                     │   │
      sudo apt install wireguard -y                           
                                                               
      # 验证安装                                          │   │
      wg --version                                           
      # 输出: wg-tools v1.0.20210223                       │   │
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 CentOS/RHEL 安装                              
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # 安装EPEL仓库                                       │   │
      sudo dnf install epel-release -y                         
                                                               
      # 安装WireGuard                                      │   │
      sudo dnf install wireguard-tools wireguard-dkms -y     
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 开启IP转发                                     
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # 临时开启                                          │   │
      echo 1 > /proc/sys/net/ipv4/ip_forward                  
                                                               
      # 永久开启                                          │   │
      sudo vi /etc/sysctl.conf                                
                                                               
      # 添加或修改以下行                                   │   │
      net.ipv4.ip_forward = 1                                 
                                                               
      # 应用配置                                          │   │
      sudo sysctl -p                                          
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
└─────────────────────────────────────────────────────────────────┘

4.3 生成密钥对

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
┌─────────────────────────────────────────────────────────────────┐
│                    生成WireGuard密钥                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              生成密钥原理                                │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   WireGuard使用Curve25519非对称加密                     │   │
│   │                                                         │   │
│   │   ┌─────────────────────────────────────────────────┐   │   │
│   │   │                                                 │   │   │
│   │   │   私钥 (PrivateKey)                             │   │   │
│   │   │   ├── 32字节随机数                              │   │   │
│   │   │   ├── 必须保密                                  │   │   │
│   │   │   └── 用于解密接收的数据                        │   │   │
│   │   │                                                 │   │   │
│   │   │   ↓ wg pubkey                                    │   │   │
│   │   │                                                 │   │   │
│   │   │   公钥 (PublicKey)                              │   │   │
│   │   │   ├── 从私钥衍生                                │   │   │
│   │   │   ├── 可以公开                                  │   │   │
│   │   │   └── 用于加密发送的数据                        │   │   │
│   │   │                                                 │   │   │
│   │   └─────────────────────────────────────────────────┘   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              生成服务器密钥                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 创建WireGuard配置目录                              │   │
│   │   sudo mkdir -p /etc/wireguard                         │   │
│   │                                                         │   │
│   │   # 生成服务器私钥                                     │   │
│   │   wg genkey | sudo tee /etc/wireguard/server_private.key │   │
│   │   # 输出示例:                                          │   │
│   │   # aGXPbO6gJ5kX0hY9dP8cR3mN2eT7sL1wQ8xV4yZ6u=      │   │
│   │                                                         │   │
│   │   # 生成服务器公钥 (从私钥衍生)                        │   │
│   │   sudo cat /etc/wireguard/server_private.key | wg pubkey │   │
│   │     | sudo tee /etc/wireguard/server_public.key         │   │
│   │   # 输出示例:                                          │   │
│   │   # Jk3xN8pR2dM5bL7cH0tE9sK6fW1qA4uY3vX8zZ0m=        │   │
│   │                                                         │   │
│   │   # 设置密钥权限 (仅root可读)                          │   │
│   │   sudo chmod 600 /etc/wireguard/server_private.key     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              生成客户端密钥                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 在本地机器生成客户端私钥                           │   │
│   │   wg genkey > client1_private.key                      │   │
│   │                                                         │   │
│   │   # 生成客户端公钥                                     │   │
│   │   cat client1_private.key | wg pubkey > client1_public.key │
│   │                                                         │   │
│   │   # 或者一条命令                                       │   │
│   │   wg genkey | tee client1_private.key | wg pubkey > client1_public.key │
│   │                                                         │   │
│   │   📝 客户端只需要把公钥给服务器                        │   │
│   │   📝 客户端保留私钥,服务器不需要知道                   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              预共享密钥 (可选)                           │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 防止量子计算机攻击 (可选,建议添加)                │   │
│   │   wg genpsk > preshared.key                            │   │
│   │                                                         │   │
│   │   # 服务器和客户端都要配置这个密钥                      │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.4 服务器配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
┌─────────────────────────────────────────────────────────────────┐
│                    服务器配置详解                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              创建配置文件                                │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   sudo vi /etc/wireguard/wg0.conf                      │   │
│   │                                                         │   │
│   │   # 或者                                             │   │
│   │   sudo nano /etc/wireguard/wg0.conf                    │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              完整配置示例                                │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # /etc/wireguard/wg0.conf                           │   │
│   │   #                                                 │   │
│   │   # WireGuard VPN服务器配置文件                        │   │
│   │   #                                                 │   │
│   │                                                         │   │
│   │   [Interface]                                         │   │
│   │   # 服务器私钥                                         │   │
│   │   PrivateKey = aGXPbO6gJ5kX0hY9dP8cR3mN2eT7sL1wQ8xV4yZ6u= │
│   │                                                         │   │
│   │   # WireGuard虚拟网卡IP                                │   │
│   │   Address = 10.0.0.1/24                               │   │
│   │                                                         │   │
│   │   # 监听端口                                          │   │
│   │   ListenPort = 51820                                  │   │
│   │                                                         │   │
│   │   # 服务器启动后执行的命令 (开启NAT转发)               │   │
│   │   PostUp = iptables -A FORWARD -i wg0 -j ACCEPT       │   │
│   │            iptables -A FORWARD -o wg0 -j ACCEPT       │   │
│   │            iptables -t nat -A POSTROUTING              │   │
│   │                 -o eth0 -j MASQUERADE                  │   │
│   │                                                         │   │
│   │   # 服务器停止前执行的命令 (清理规则)                  │   │
│   │   PostDown = iptables -D FORWARD -i wg0 -j ACCEPT     │   │
│   │             iptables -D FORWARD -o wg0 -j ACCEPT     │   │
│   │             iptables -t nat -D POSTROUTING             │   │
│   │                  -o eth0 -j MASQUERADE                │   │
│   │                                                         │   │
│   │   # ============ 用户1 ============                   │   │
│   │   [Peer]                                             │   │
│   │   # 用户1的公钥                                        │   │
│   │   PublicKey = Jk3xN8pR2dM5bL7cH0tE9sK6fW1qA4uY3vX8zZ0m= │
│   │                                                         │   │
│   │   # 用户1的VPN内网IP                                   │   │
│   │   AllowedIPs = 10.0.0.2/32                            │   │
│   │                                                         │   │
│   │   # 预共享密钥 (防量子攻击)                           │   │
│   │   PresharedKey = 8xP4nB7kL2jH9mZ5cV1qW3sT6rA8eF0yU2d=   │   │
│   │                                                         │   │
│   │   # ============ 用户2 ============                   │   │
│   │   [Peer]                                             │   │
│   │   PublicKey = M1pK8qR5fD7nL2bJ9hT3xW6sY0cV2oA5tE8uF1z=   │   │
│   │   AllowedIPs = 10.0.0.3/32                            │   │
│   │                                                         │   │
│   │   # ============ 用户3 (手机) ============           │   │
│   │   [Peer]                                             │   │
│   │   PublicKey = N2qL9tR8gF5mK3cH0dW6xV1sY7bJ2oA8tE5uF0z=   │   │
│   │   AllowedIPs = 10.0.0.4/32                            │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              配置说明                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   [Interface] 部分:                                    │   │
│   │   ├── PrivateKey: 服务器私钥 (保密!)                   │   │
│   │   ├── Address: VPN内部IP地址                           │   │
│   │   ├── ListenPort: UDP监听端口                          │   │
│   │   └── PostUp/PostDown: 启动/停止时的额外命令           │   │
│   │                                                         │   │
│   │   [Peer] 部分:                                         │   │
│   │   ├── PublicKey: 客户端公钥                            │   │
│   │   ├── AllowedIPs: 允许这个客户端使用的IP               │   │
│   │   │   /32 表示精确IP,/24 表示整个网段                 │   │
│   │   └── PresharedKey: 预共享密钥 (可选,防量子攻击)      │   │
│   │                                                         │   │
│   │   📝 PostUp中的eth0是服务器网卡名,需根据实际情况修改  │   │
│   │   📝 用 ip addr 或 ifconfig 查看网卡名                 │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.5 启动服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
┌─────────────────────────────────────────────────────────────────┐
│                    启动和管理WireGuard                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              启动服务                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 设置配置文件权限                                    │   │
│   │   sudo chmod 600 /etc/wireguard/wg0.conf               │   │
│   │                                                         │   │
│   │   # 启动WireGuard                                      │   │
│   │   sudo systemctl start wg-quick@wg0                     │   │
│   │                                                         │   │
│   │   # 设置开机自启                                        │   │
│   │   sudo systemctl enable wg-quick@wg0                   │   │
│   │                                                         │   │
│   │   # 查看运行状态                                       │   │
│   │   sudo systemctl status wg-quick@wg0                   │   │
│   │                                                         │   │
│   │   # 手动启动/停止                                      │   │
│   │   sudo wg-quick up wg0                                 │   │
│   │   sudo wg-quick down wg0                               │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              查看状态                                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 查看WireGuard状态                                  │   │
│   │   sudo wg show                                         │   │
│   │                                                         │   │
│   │   # 输出示例:                                          │   │
│   │   # interface: wg0                                    │   │
│   │   #   public key: Jk3xN8pR2dM5bL7cH0tE9sK6fW1qA4uY3vX8zZ0m=│   │
│   │   #   private key: (hidden)                            │   │
│   │   #   listening port: 51820                           │   │
│   │   #                                                 │   │
│   │   # peer: M1pK8qR5fD7nL2bJ9hT3xW6sY0cV2oA5tE8uF1z=   │   │
│   │   #   allowed ips: 10.0.0.3/32                       │   │
│   │   #                                                 │   │
│   │   # peer: N2qL9tR8gF5mK3cH0dW6xV1sY7bJ2oA8tE5uF0z=   │   │
│   │   #   allowed ips: 10.0.0.4/32                       │   │
│   │                                                         │   │
│   │   # 查看接口IP                                         │   │
│   │   ip addr show wg0                                     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              防火墙配置                                  │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 使用ufw防火墙 (Ubuntu)                             │   │
│   │   sudo ufw allow 51820/udp                            │   │
│   │   sudo ufw enable                                     │   │
│   │                                                         │   │
│   │   # 或者手动iptables                                   │   │
│   │   sudo iptables -A INPUT -p udp --dport 51820 -j ACCEPT │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.6 客户端配置 (Windows)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
┌─────────────────────────────────────────────────────────────────┐
                    Windows客户端配置                               
├─────────────────────────────────────────────────────────────────┤
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 下载安装                                       
   ├─────────────────────────────────────────────────────────┤   
                                                               
      1. 访问官网: https://www.wireguard.com/install/        
                                                               
      2. 下载Windows客户端                                    
         ├── 64: wireguard-installer.exe                    
         └── 或者使用 winget: winget install WireGuard       
                                                               
      3. 安装并启动WireGuard应用                              
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 生成客户端密钥 (Windows上操作)                  
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # 使用WireGuard客户端生成                            │   │
      # 打开WireGuard → 点击 "Generate"                    │   │
                                                               
      # 或者使用PowerShell生成                             │   │
      # 下载wireguard-tools后:                            │   │
      ./wireguard.exe /genkey /privatekey client1_priv.key    
      ./wireguard.exe /pubkey < client1_priv.key             
                                                               
      📝 生成后把公钥发给服务器管理员                          
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 客户端配置文件                                 
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # client1.conf (保存到桌面或任意位置)                │   │
                                                               
      [Interface]                                            
      # 客户端私钥                                         │   │
      PrivateKey = cGXPbO6gJ5kX0hY9dP8cR3mN2eT7sL1wQ8xV4yZ6u= 
                                                               
      # 客户端VPN IP                                       │   │
      Address = 10.0.0.2/24                                  
                                                               
      # DNS服务器 (可选,防止DNS泄露)                      │   │
      DNS = 8.8.8.8, 1.1.1.1                                
                                                               
      [Peer]                                                 
      # 服务器公钥                                          │   │
      PublicKey = Jk3xN8pR2dM5bL7cH0tE9sK6fW1qA4uY3vX8zZ0m= 
                                                               
      # 服务器地址和端口                                   │   │
      Endpoint = vpn.example.com:51820                       
                                                               
      # 允许访问的IP范围                                   │   │
      # 10.0.0.0/24 表示访问整个VPN内网                   │   │
      # 0.0.0.0/0 表示所有流量都走VPN (全局代理)         │   │
      AllowedIPs = 10.0.0.0/24                               
                                                               
      # 保持连接 (防止NAT超时断开)                        │   │
      PersistentKeepalive = 25                                
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 导入配置并连接                                 
   ├─────────────────────────────────────────────────────────┤   
                                                               
      1. 打开WireGuard客户端                                 
      2. 点击 "Import tunnel(s) from file"                  
      3. 选择刚才创建的 client1.conf 文件                    
      4. 点击 "Activate" 连接                               
      5. 看到绿色 "Active" 即连接成功                         
                                                               
      测试连接:                                               
      ping 10.0.0.1                                          
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
└─────────────────────────────────────────────────────────────────┘

4.7 客户端配置 (macOS)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
┌─────────────────────────────────────────────────────────────────┐
                    macOS客户端配置                                
├─────────────────────────────────────────────────────────────────┤
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 安装方式                                       
   ├─────────────────────────────────────────────────────────┤   
                                                               
      方式1: Homebrew (推荐)                                  
      brew install wireguard-tools wireguard-go               
                                                               
      方式2: App Store                                        
      搜索 "WireGuard" 下载官方客户端                         
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 命令行配置                                     
   ├─────────────────────────────────────────────────────────┤   
                                                               
      # 创建配置目录                                       │   │
      sudo mkdir -p /etc/wireguard                           
                                                               
      # 生成密钥                                           │   │
      wg genkey | tee client_priv.key | wg pubkey > client_pub.key 
                                                               
      # 创建配置文件                                       │   │
      sudo vi /etc/wireguard/wg0.conf                        
                                                               
      [Interface]                                            
      PrivateKey = <客户端私钥>                              
      Address = 10.0.0.2/24                                  
      DNS = 8.8.8.8                                          
                                                               
      [Peer]                                                 
      PublicKey = <服务器公钥>                                
      Endpoint = vpn.example.com:51820                       
      AllowedIPs = 10.0.0.0/24                               
      PersistentKeepalive = 25                                
                                                               
      # 启动连接                                          │   │
      sudo wg-quick up wg0                                   
                                                               
      # 断开连接                                          │   │
      sudo wg-quick down wg0                                 
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
   ┌─────────────────────────────────────────────────────────┐   
                 GUI客户端配置                                  
   ├─────────────────────────────────────────────────────────┤   
                                                               
      1. 下载并安装WireGuard (App Store)                     
      2. 点击 "+"  "Add from File or Archive"              
      3. 选择配置文件或手动创建                               
      4. 点击 "Activate"                                    
                                                               
   └─────────────────────────────────────────────────────────┘   
                                                                 
└─────────────────────────────────────────────────────────────────┘

4.8 客户端配置 (Linux)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
┌─────────────────────────────────────────────────────────────────┐
│                    Linux客户端配置                                │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              Ubuntu/Debian                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 安装                                              │   │
│   │   sudo apt install wireguard -y                       │   │
│   │                                                         │   │
│   │   # 生成密钥                                           │   │
│   │   wg genkey | tee client_private.key | wg pubkey > client_public.key │
│   │                                                         │   │
│   │   # 创建配置                                           │   │
│   │   sudo vi /etc/wireguard/wg0.conf                     │   │
│   │                                                         │   │
│   │   [Interface]                                         │   │
│   │   PrivateKey = <客户端私钥>                           │   │
│   │   Address = 10.0.0.2/24                               │   │
│   │   DNS = 8.8.8.8                                       │   │
│   │                                                         │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <服务器公钥>                             │   │
│   │   Endpoint = vpn.example.com:51820                    │   │
│   │   AllowedIPs = 10.0.0.0/24                            │   │
│   │   PersistentKeepalive = 25                             │   │
│   │                                                         │   │
│   │   # 启动                                              │   │
│   │   sudo wg-quick up wg0                                │   │
│   │                                                         │   │
│   │   # 查看状态                                          │   │
│   │   sudo wg show                                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              全流量代理模式                              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 所有流量都走VPN                                    │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <服务器公钥>                            │   │
│   │   Endpoint = vpn.example.com:51820                    │   │
│   │   AllowedIPs = 0.0.0.0/0                              │   │
│   │   PersistentKeepalive = 25                            │   │
│   │                                                         │   │
│   │   # 只代理VPN内网流量                                  │   │
│   │   AllowedIPs = 10.0.0.0/24                            │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.9 客户端配置 (手机 iOS/Android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
┌─────────────────────────────────────────────────────────────────┐
│                    手机客户端配置                                   │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              iOS 配置                                   │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   1. App Store搜索 "WireGuard" 下载                   │   │
│   │   2. 打开APP,点击 "+"                                │   │
│   │   3. 选择 "Create from scratch" 或 "Scan from QR code" │   │
│   │   4. 填写配置信息                                      │   │
│   │   5. 点击 "Save"                                       │   │
│   │   6. 点击 "Allow" 允许VPN配置                         │   │
│   │   7. 点击连接按钮                                      │   │
│   │                                                         │   │
│   │   配置参数:                                            │   │
│   │   ├── Name: VPN连接名称                               │   │
│   │   ├── Private Key: 客户端私钥                         │   │
│   │   ├── Addresses: 10.0.0.4/24                         │   │
│   │   ├── DNS Servers: 8.8.8.8                           │   │
│   │   ├── Public Key: 服务器公钥                          │   │
│   │   ├── Endpoint: 服务器IP:51820                       │   │
│   │   └── Allowed IPs: 10.0.0.0/24                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              Android 配置                               │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   1. Google Play搜索 "WireGuard" 下载                 │   │
│   │   2. 打开APP,点击 "+"                                │   │
│   │   3. 选择 "Scan from QR code" (推荐)                  │   │
│   │      或 "Create from scratch"                          │   │
│   │   4. 填写配置信息                                      │   │
│   │   5. 点击保存                                          │   │
│   │   6. 点击开关连接                                      │   │
│   │                                                         │   │
│   │   📝 Android版本可直接扫码导入配置                     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              生成二维码 (服务器端操作)                   │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 安装qrencode                                      │   │
│   │   sudo apt install qrencode -y                        │   │
│   │                                                         │   │
│   │   # 生成二维码                                        │   │
│   │   # 1. 先创建客户端配置文件                           │   │
│   │   cat > client_phone.conf << 'EOF'                    │   │
│   │   [Interface]                                         │   │
│   │   PrivateKey = <手机私钥>                             │   │
│   │   Address = 10.0.0.4/24                               │   │
│   │   DNS = 8.8.8.8                                       │   │
│   │                                                         │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <服务器公钥>                            │   │
│   │   Endpoint = vpn.example.com:51820                    │   │
│   │   AllowedIPs = 0.0.0.0/0                              │   │
│   │   PersistentKeepalive = 25                             │   │
│   │   EOF                                                 │   │
│   │                                                         │   │
│   │   # 2. 生成二维码                                     │   │
│   │   qrencode -t ansiutf8 < client_phone.conf           │   │
│   │                                                         │   │
│   │   # 3. 手机扫描即可                                   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.10 添加新用户

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
┌─────────────────────────────────────────────────────────────────┐
│                    添加新用户步骤                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              1. 生成用户密钥                             │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 在服务器上操作                                     │   │
│   │   wg genkey > user4_private.key                       │   │
│   │   cat user4_private.key | wg pubkey > user4_public.key │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              2. 添加到服务器配置                         │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   sudo vi /etc/wireguard/wg0.conf                     │   │
│   │                                                         │   │
│   │   # 添加新的[Peer]段                                   │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <user4_public.key的内容>                │   │
│   │   AllowedIPs = 10.0.0.5/32                             │   │
│   │                                                         │   │
│   │   # 重启WireGuard应用配置                             │   │
│   │   sudo wg-quick down wg0                              │   │
│   │   sudo wg-quick up wg0                                │   │
│   │                                                         │   │
│   │   # 或者热重载 (不需要断开连接)                        │   │
│   │   sudo wg syncconf wg0 <(sudo wg-quick strip wg0)     │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              3. 创建客户端配置                          │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   cat > user4.conf << 'EOF'                           │   │
│   │   [Interface]                                         │   │
│   │   PrivateKey = <user4_private.key的内容>               │   │
│   │   Address = 10.0.0.5/24                               │   │
│   │   DNS = 8.8.8.8                                       │   │
│   │                                                         │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <服务器公钥>                            │   │
│   │   Endpoint = vpn.example.com:51820                    │   │
│   │   AllowedIPs = 0.0.0.0/0                              │   │
│   │   PersistentKeepalive = 25                             │   │
│   │   EOF                                                 │   │
│   │                                                         │   │
│   │   # 生成二维码                                         │   │
│   │   qrencode -t ansiutf8 < user4.conf                   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              快速添加脚本                               │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   #!/bin/bash                                        │   │
│   │   # add_user.sh                                      │   │
│   │                                                         │   │
│   │   #!/bin/bash                                        │   │
│   │   SERVER_PUB_KEY="<服务器公钥>"                       │   │
│   │   SERVER_ENDPOINT="vpn.example.com:51820"             │   │
│   │   VPN_NET="10.0.0"                                    │   │
│   │                                                         │   │
│   │   # 获取下一个可用IP                                  │   │
│   │   LAST_IP=$(sudo wg show wg0 peers | wc -l)          │   │
│   │   CLIENT_IP="${VPN_NET}.$((LAST_IP + 2))"             │   │
│   │                                                         │   │
│   │   # 生成密钥                                          │   │
│   │   CLIENT_PRIV_KEY=$(wg genkey)                        │   │
│   │   CLIENT_PUB_KEY=$(echo $CLIENT_PRIV_KEY | wg pubkey) │   │
│   │                                                         │   │
│   │   # 添加到服务器                                      │   │
│   │   sudo wg set wg0 peer $CLIENT_PUB_KEY allowed-ips ${CLIENT_IP}/32 │
│   │                                                         │   │
│   │   # 创建客户端配置                                    │   │
│   │   cat > client_${CLIENT_IP}.conf << 'EOFC'            │   │
│   │   [Interface]                                        │   │
│   │   PrivateKey = $CLIENT_PRIV_KEY                       │   │
│   │   Address = ${CLIENT_IP}/24                          │   │
│   │   DNS = 8.8.8.8                                      │   │
│   │                                                        │   │
│   │   [Peer]                                             │   │
│   │   PublicKey = $SERVER_PUB_KEY                         │   │
│   │   Endpoint = $SERVER_ENDPOINT                          │   │
│   │   AllowedIPs = 0.0.0.0/0                             │   │
│   │   PersistentKeepalive = 25                            │   │
│   │   EOFC                                                │   │
│   │                                                        │   │
│   │   echo "用户已创建: $CLIENT_IP"                       │   │
│   │   echo "配置文件: client_${CLIENT_IP}.conf"           │   │
│   │   qrencode -t ansiutf8 < client_${CLIENT_IP}.conf    │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.11 进阶配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
┌─────────────────────────────────────────────────────────────────┐
│                    进阶配置技巧                                   │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              1. 全流量代理 (科学上网)                    │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 客户端 AllowedIPs = 0.0.0.0/0                     │   │
│   │   # 所有流量都走VPN                                   │   │
│   │                                                         │   │
│   │   # 服务器端需要设置NAT转发                            │   │
│   │   # PostUp已配置:                                     │   │
│   │   # iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              2. 分流模式 (特定流量走VPN)                 │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 只代理访问内网的流量                               │   │
│   │   AllowedIPs = 10.0.0.0/24, 192.168.1.0/24           │   │
│   │                                                         │   │
│   │   # 国内IP直连,海外IP走VPN                           │   │
│   │   # 需要配合路由表 (复杂)                             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              3. 站点间互联 (Site-to-Site)              │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   北京办公室配置:                                      │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <上海服务器公钥>                        │   │
│   │   Endpoint = 202.x.x.x:51820                         │   │
│   │   AllowedIPs = 192.168.2.0/24  # 上海内网             │   │
│   │                                                         │   │
│   │   上海办公室配置:                                      │   │
│   │   [Peer]                                              │   │
│   │   PublicKey = <北京服务器公钥>                        │   │
│   │   Endpoint = 203.x.x.x:51820                         │   │
│   │   AllowedIPs = 192.168.1.0/24  # 北京内网             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              4. 网状拓扑 (Mesh)                         │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 每个节点都互相连接                                 │   │
│   │                                                         │   │
│   │   节点A (10.0.0.1)                                    │   │
│   │   ├── Peer: 节点B (10.0.0.2)                         │   │
│   │   │   AllowedIPs: 10.0.0.2/32                        │   │
│   │   ├── Peer: 节点C (10.0.0.3)                         │   │
│   │   │   AllowedIPs: 10.0.0.3/32                        │   │
│   │   └── Peer: 节点D (10.0.0.4)                         │   │
│   │       AllowedIPs: 10.0.0.4/32                        │   │
│   │                                                         │   │
│   │   📝 适合小规模网络,大规模建议用星型拓扑             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              5. Kubernetes集成                         │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   # 使用WireGuard作为K8s网络插件                     │   │
│   │   kubectl apply -k github.com/yurt-device/wireguard  │   │
│   │                                                         │   │
│   │   # 或者使用tailscale (基于WireGuard的商业方案)       │   │
│   │   curl -fsSL https://tailscale.com/install.sh | sh    │   │
│   │   tailscale up                                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

五、常见问题排查

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
┌─────────────────────────────────────────────────────────────────┐
│                    常见问题排查                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              问题1: 无法连接                             │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   检查项:                                               │   │
│   │   ├── ✅ 服务器端口是否开放 (ufw/iptables)            │   │
│   │   ├── ✅ 客户端配置是否正确                           │   │
│   │   ├── ✅ 公钥/私钥是否匹配                            │   │
│   │   ├── ✅ Endpoint地址是否正确                         │   │
│   │   └── ✅ 服务器防火墙是否放行UDP 51820                │   │
│   │                                                         │   │
│   │   排查命令:                                            │   │
│   │   # 服务器端查看日志                                   │   │
│   │   sudo journalctl -u wg-quick@wg0 -f                  │   │
│   │                                                         │   │
│   │   # 客户端测试端口连通性                               │   │
│   │   nc -vzu vpn.example.com 51820                       │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              问题2: 连接成功但无法上网                   │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   检查项:                                               │   │
│   │   ├── ✅ 服务器IP转发是否开启                          │   │
│   │   │   cat /proc/sys/net/ipv4/ip_forward               │   │
│   │   │   # 输出应为1                                     │   │
│   │   │                                                    │   │
│   │   ├── ✅ NAT是否配置正确                              │   │
│   │   │   iptables -t nat -L -n | grep MASQUERADE        │   │
│   │   │                                                    │   │
│   │   └── ✅ AllowedIPs是否包含0.0.0.0/0                 │   │
│   │                                                         │   │
│   │   修复命令:                                            │   │
│   │   # 开启IP转发                                        │   │
│   │   echo 1 > /proc/sys/net/ipv4/ip_forward             │   │
│   │                                                         │   │
│   │   # 添加NAT规则                                       │   │
│   │   iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              问题3: DNS泄露                            │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   检查项:                                               │   │
│   │   ├── ✅ 客户端是否配置DNS                            │   │
│   │   ├── ✅ DNS是否使用加密DNS (DoH/DoT)                  │   │
│   │   └── ✅ 防火墙是否阻止直连DNS                        │   │
│   │                                                         │   │
│   │   修复方法:                                            │   │
│   │   # 在客户端配置中添加                               │   │
│   │   [Interface]                                         │   │
│   │   DNS = 1.1.1.1, 8.8.8.8                             │   │
│   │                                                         │   │
│   │   # 或者使用加密DNS                                   │   │
│   │   DNS = 1.1.1.1, 1.0.0.1                             │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              问题4: 连接经常断开                         │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   原因:  NAT超时导致连接断开                          │   │
│   │                                                         │   │
│   │   修复:                                               │   │
│   │   # 在客户端配置中添加                               │   │
│   │   [Peer]                                              │   │
│   │   PersistentKeepalive = 25                            │   │
│   │                                                         │   │
│   │   # 25秒发送一次心跳包,保持NAT映射                   │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │              问题5: 无法访问内网特定服务                 │   │
│   ├─────────────────────────────────────────────────────────┤   │
│   │                                                         │   │
│   │   检查项:                                               │   │
│   │   ├── ✅ 服务器路由是否正确                           │   │
│   │   ├── ✅ 目标服务是否运行                            │   │
│   │   └── ✅ 防火墙是否放行服务端口                       │   │
│   │                                                         │   │
│   │   排查:                                                │   │
│   │   # 从VPN客户端ping内网服务器                       │   │
│   │   ping 192.168.1.100                                  │   │
│   │                                                         │   │
│   │   # 测试端口连通性                                     │   │
│   │   nc -zv 192.168.1.100 80                            │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

六、总结

6.1 核心对比

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
┌─────────────────────────────────────────────────────────────────┐
│                    WireGuard vs OpenVPN 总结                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   WireGuard 优势:                                      │   │
│   │   ├── 🚀 速度快 (接近物理网速)                        │   │
│   │   ├── ⚡ 延迟低 (< 1ms)                               │   │
│   │   ├── 🔋 功耗低 (适合移动设备)                        │   │
│   │   ├── 🔒 安全性高 (现代加密算法)                       │   │
│   │   ├── 📝 配置简单 (仅20行)                            │   │
│   │   └── 💻 资源占用少 (< 5% CPU)                       │   │
│   │                                                         │   │
│   │   OpenVPN 优势:                                       │   │
│   │   ├── 🏢 企业级功能 (证书管理、ACL)                   │   │
│   │   ├── 🔀 TCP模式支持 (强防火墙穿透)                   │   │
│   │   ├── 📱 兼容性更好 (老旧设备)                        │   │
│   │   └── 📋 审计成熟 (多年实战检验)                      │   │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

6.2 选择建议

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
┌─────────────────────────────────────────────────────────────────┐
│                    选择建议                                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   选择 WireGuard:                                               │
│   ├── ✅ 个人隐私保护                                         │
│   ├── ✅ 家庭/小型办公室远程访问                             │
│   ├── ✅ 多站点互联 (Site-to-Site)                          │
│   ├── ✅ 高带宽需求 (视频/大文件传输)                        │
│   ├── ✅ 低延迟需求 (游戏/语音)                              │
│   └── ✅ 快速部署场景                                        │
│                                                                 │
│   选择 OpenVPN:                                                │
│   ├── ✅ 企业环境 (需要证书管理)                             │
│   ├── ✅ 严格审计要求                                         │
│   ├── ✅ 需要兼容老旧设备                                     │
│   ├── ✅ 严格防火墙环境 (需要TCP穿透)                        │
│   └── ✅ 需要复杂访问控制策略                                │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

6.3 WireGuard 使用技巧

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
┌─────────────────────────────────────────────────────────────────┐
│                    WireGuard 使用技巧                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   1. 定期更换密钥                                             │
│      └── 建议每3-6个月重新生成密钥对                          │
│                                                                 │
│   2. 启用预共享密钥                                           │
│      └── wg genpsk > preshared.key                           │
│      └── 防止量子计算机攻击                                    │
│                                                                 │
│   3. 限制用户访问                                             │
│      └── AllowedIPs控制用户可访问的IP                        │
│      └── 最小权限原则                                         │
│                                                                 │
│   4. 监控连接状态                                             │
│      └── sudo wg show 查看实时连接                           │
│      └── 配置日志告警                                         │
│                                                                 │
│   5. 使用脚本自动化                                           │
│      └── 用户管理脚本                                          │
│      └── 备份配置脚本                                         │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

WireGuard以其简洁、高效、安全的特点,正在成为VPN领域的新标准。对于大多数场景,WireGuard都是更好的选择。

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计