一、服务器部署(Linux)
1️⃣ 安装
Debian / Ubuntu:
apt update
apt install wireguard -y
2️⃣ 生成密钥
wg genkey | tee server_private.key | wg pubkey > server_public.key
查看:
cat server_private.key
cat server_public.key
记好。
3️⃣ 配置服务器
创建:
vim /etc/wireguard/wg0.conf
内容:
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = 服务器私钥
PostUp = sysctl -w net.ipv4.ip_forward=1
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
⚠ 把 eth0 改成你服务器真实网卡:
ip a
4️⃣ 开启服务
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0
查看状态:
wg
5️⃣ 开放防火墙
ufw allow 51820/udp
📱 二、客户端配置
1️⃣ 生成客户端密钥
wg genkey | tee client_private.key | wg pubkey > client_public.key
2️⃣ 把客户端公钥加到服务器
编辑服务器:
vim /etc/wireguard/wg0.conf
添加:
[Peer]
PublicKey = 客户端公钥
AllowedIPs = 10.0.0.2/32
重启:
systemctl restart wg-quick@wg0
3️⃣ 客户端配置文件
例如 Windows / Mac / Linux 客户端:
[Interface]
PrivateKey = 客户端私钥
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = 服务器公钥
Endpoint = 服务器IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
🔥 客户端平台
-
Windows → 官方 WireGuard 客户端
-
macOS → 官方客户端
-
Linux → wireguard-tools
-
Android → 官方
-
iOS → 官方
跨平台真的很舒服。
🎯 重要参数解释
| 参数 | 作用 |
|---|---|
| Address | VPN 内网地址 |
| AllowedIPs | 走 VPN 的流量范围 |
| PersistentKeepalive | 保持 NAT 映射 |
| MASQUERADE | 允许访问公网 |
🌍 全局代理 vs 分流
如果你:
AllowedIPs = 0.0.0.0/0
就是全流量走 VPN。
如果你只想代理某些网段:
比如:
AllowedIPs = 8.8.8.8/32
就只代理那部分。
⚡ 优点
✔ UDP
✔ ChaCha20 加密
✔ 延迟低
✔ CPU 占用低
✔ 比 OpenVPN 快很多
🧠 现实建议
如果你:
-
只是想稳定远程
-
或自己用
👉 WireGuard 是完美选择。
如果你:
-
在国内环境
-
要抗检测
那可能要配合端口伪装(比如 wg + udp2raw)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
