> 在外面用公共 WiFi 担心数据被窃取?想远程访问家里的 NAS 和服务器?WireGuard 是目前最轻量、最快、最安全的 VPN 方案,配置简单到令人感动。
---
## 为什么选 WireGuard?
| 对比 | WireGuard | OpenVPN | IPsec |
|------|-----------|---------|-------|
| 代码量 | ~4000 行 | ~70000 行 | ~400000 行 |
| 配置复杂度 | 极简 | 复杂 | 很复杂 |
| 性能 | 极高 | 中等 | 中等 |
| 内核集成 | Linux 5.6+ 内置 | 需要额外安装 | 内置 |
| 漫游支持 | 优秀 | 一般 | 差 |
---
## 安装
```bash
apt update && apt install wireguard -y
```
---
## 服务端配置
### 1. 生成密钥对
```bash
cd /etc/wireguard
wg genkey | tee server_private.key | wg pubkey > server_public.key
chmod 600 server_private.key
```
### 2. 创建配置文件 /etc/wireguard/wg0.conf
```ini
[Interface]
PrivateKey = 服务器私钥
Address = 10.0.0.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = 客户端公钥
AllowedIPs = 10.0.0.2/32
```
### 3. 开启 IP 转发
```bash
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
```
### 4. 启动
```bash
systemctl enable wg-quick@wg0 --now
wg show
```
---
## 客户端配置
```ini
[Interface]
PrivateKey = 客户端私钥
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = 服务器公钥
Endpoint = 服务器公网IP:51820
AllowedIPs = 0.0.0.0/0
```
如果只想访问内网资源(不走 VPN 上网),把 AllowedIPs 改为 10.0.0.0/24, 192.168.1.0/24。
---
WireGuard 配置真的就这么简单。搭好之后在任何网络环境下都能安全访问家里的设备,速度快、延迟低,几乎感觉不到 VPN 的存在。