$ 发布 2026-07-02 | ~9分钟阅读
语境: 从零开始在Vultr和DigitalOcean VPS上搭建WireGuard翻墙服务器,包含详细步骤和配置优化。

Vultr/DigitalOcean VPS搭建WireGuard全流程教程


输出
延迟估计 ~351 ms 置信度 ~0.91

前言

WireGuard 是新一代的 VPN 协议,以其简洁、高效、安全著称。相比传统的 OpenVPN 和 Shadowsocks,WireGuard 速度更快、延迟更低、配置更简单。本文将带你从零开始,在 Vultr 或 DigitalOcean VPS 上搭建属于自己的 WireGuard 服务器。


一、准备工作

1.1 选择 VPS

推荐配置:

  • 最低配置:1核 512M 内存 10G硬盘
  • 推荐配置:1核 1G 内存 25G硬盘
  • 机房选择:日本、新加坡、美国西海岸
  • 系统选择:Ubuntu 22.04 / 24.04 LTS(推荐)

1.2 所需工具

工具用途下载
PuTTY / TerminalSSH 连接服务器系统自带 / 官网下载
WinSCP / FileZilla文件传输(可选)官网下载
WireGuard 客户端连接使用官网下载

1.3 登录 VPS

购买 VPS 后,你会获得:

  • IP 地址
  • root 密码
  • SSH 端口(默认 22)

使用 SSH 连接:

ssh root@你的VPS的IP地址

首次连接会提示确认指纹,输入 yes 回车,然后输入密码登录。


二、WireGuard 一键脚本安装

2.1 推荐脚本:wireguard-install

这是最流行的 WireGuard 一键安装脚本,简单易用。

Step 1:更新系统

apt update && apt upgrade -y

Step 2:安装必要工具

apt install -y curl wget

Step 3:运行一键脚本

wget https://git.io/wireguard -O wireguard-install.sh && bash wireguard-install.sh

或者使用另一个流行脚本:

curl -O https://raw.githubusercontent.com/angristan/wireguard-install/master/wireguard-install.sh
chmod +x wireguard-install.sh
./wireguard-install.sh

2.2 安装配置向导

脚本运行后会提示你配置一些参数:

配置项推荐值说明
IPv4 地址自动检测确认是你的公网 IP
端口51820(默认)可以自定义
客户端名称随便取,如 myphone第一个客户端的名字
DNS 服务器1.1.1.1 或 8.8.8.8Cloudflare 或 Google DNS

按提示输入后,脚本会自动完成安装。

2.3 安装完成

安装成功后会显示:

✅ WireGuard is installed and running.

客户端配置文件已生成:/root/wg0-client-name.conf
二维码已保存为:/root/wg0-client-name.png

三、手动安装 WireGuard(进阶)

如果你想了解详细过程,可以手动安装。

3.1 安装 WireGuard

Ubuntu / Debian:

apt update
apt install -y wireguard wireguard-tools

CentOS / RHEL:

yum install -y epel-release elrepo-release
yum install -y kmod-wireguard wireguard-tools

3.2 生成密钥对

# 生成服务端密钥
wg genkey | tee server_private.key | wg pubkey > server_public.key

# 生成客户端密钥
wg genkey | tee client_private.key | wg pubkey > client_public.key

3.3 配置服务端

创建配置文件:

nano /etc/wireguard/wg0.conf

写入以下内容:

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = 服务端私钥
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

[Peer]
PublicKey = 客户端公钥
AllowedIPs = 10.0.0.2/32

3.4 开启 IP 转发

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p

3.5 启动 WireGuard

wg-quick up wg0
systemctl enable wg-quick@wg0

3.6 配置客户端

客户端配置文件 client.conf

[Interface]
PrivateKey = 客户端私钥
Address = 10.0.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = 服务端公钥
Endpoint = 服务器IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

四、Vultr 专属优化

4.1 Vultr 机房推荐

机房国内延迟速度推荐度
东京(Tokyo)~60ms⭐⭐⭐⭐⭐强烈推荐
新加坡(Singapore)~80ms⭐⭐⭐⭐推荐
洛杉矶(Los Angeles)~140ms⭐⭐⭐可选
西雅图(Seattle)~130ms⭐⭐⭐可选
硅谷(Silicon Valley)~150ms⭐⭐⭐可选

4.2 Vultr 防火墙设置

Vultr 后台需要放行 WireGuard 端口:

  1. 登录 Vultr 后台
  2. 进入你的 VPS 实例
  3. 点击 Settings → Firewall
  4. 添加规则:
    • Protocol: UDP
    • Port: 51820
    • Source: 0.0.0.0/0

4.3 Vultr 网络优化

开启 BBR:

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

验证是否开启成功:

sysctl net.ipv4.tcp_congestion_control

显示 bbr 即为成功。


五、DigitalOcean 专属优化

5.1 DigitalOcean 机房推荐

机房国内延迟速度推荐度
新加坡(SGP1)~80ms⭐⭐⭐⭐推荐
旧金山(SFO3)~140ms⭐⭐⭐可选
纽约(NYC3)~200ms⭐⭐不推荐
法兰克福(FRA1)~180ms⭐⭐不推荐
班加罗尔(BLR1)~120ms⭐⭐⭐可选

5.2 DigitalOcean 防火墙

  1. 登录 DigitalOcean 后台
  2. 进入 Networking → Firewalls
  3. 创建防火墙规则:
    • Type: Custom
    • Protocol: UDP
    • Port Range: 51820
    • Sources: All IPv4, All IPv6
  4. 将防火墙应用到你的 Droplet

5.3 DigitalOcean 网络优化

开启 TCP BBR:

# Ubuntu 22.04+ 默认已支持
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

六、客户端配置

6.1 Windows 客户端

  1. WireGuard 官网 下载 Windows 版
  2. 安装并打开 WireGuard
  3. 点击「Add Tunnel」→「Import tunnel(s) from file」
  4. 选择生成的 .conf 文件
  5. 点击「Activate」连接

6.2 macOS 客户端

  1. 从 App Store 搜索「WireGuard」安装
  2. 打开 WireGuard
  3. 点击「+」→「Import tunnel(s) from file」
  4. 选择配置文件
  5. 点击开关连接

6.3 iOS 客户端

  1. App Store 搜索「WireGuard」安装
  2. 打开 WireGuard
  3. 点击「+」→「Scan from QR code」
  4. 扫描服务器上生成的二维码
  5. 开启连接

生成二维码命令:

apt install -y qrencode
qrencode -t ansiutf8 < /etc/wireguard/wg0.conf

6.4 Android 客户端

  1. Google Play 搜索「WireGuard」安装
  2. 打开应用
  3. 点击「+」→「Scan from QR code」或「Import from file」
  4. 添加后点击开关连接

七、管理和维护

7.1 添加新客户端

重新运行脚本:

bash wireguard-install.sh

选择 Add a new user,输入用户名即可。

7.2 查看连接状态

wg show

查看所有连接:

wg show wg0

7.3 重启 WireGuard

wg-quick down wg0
wg-quick up wg0

或使用 systemctl:

systemctl restart wg-quick@wg0

7.4 卸载 WireGuard

重新运行脚本,选择卸载选项:

bash wireguard-install.sh

选择 Uninstall WireGuard


八、性能优化

8.1 内核参数优化

编辑 /etc/sysctl.conf

nano /etc/sysctl.conf

添加以下内容:

# 网络优化
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.netdev_max_backlog = 250000
net.core.somaxconn = 4096

# TCP 优化
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000

# BBR 拥塞控制
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

生效:

sysctl -p

8.2 MTU 调整

如果速度不理想,可以尝试调整 MTU:

在客户端配置中添加:

[Interface]
...
MTU = 1420

常见 MTU 值测试:

  • 1500(默认)
  • 1420(推荐)
  • 1400
  • 1380

8.3 UDP 优化

# 增大 UDP 缓冲区
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864

九、安全加固

9.1 修改 SSH 端口

nano /etc/ssh/sshd_config

找到 Port 22,修改为其他端口,如 Port 2222,然后重启 SSH:

systemctl restart sshd

9.2 禁用密码登录,使用密钥登录

生成密钥对(本地执行):

ssh-keygen -t ed25519

将公钥复制到服务器:

ssh-copy-id -i ~/.ssh/id_ed25519.pub root@你的IP

然后禁用密码登录:

nano /etc/ssh/sshd_config

设置:

PasswordAuthentication no

重启 SSH:

systemctl restart sshd

9.3 安装防火墙

apt install -y ufw
ufw allow 22/tcp    # SSH 端口
ufw allow 51820/udp # WireGuard 端口
ufw enable

9.4 定期更新系统

apt update && apt upgrade -y

十、常见问题

10.1 连接不上

检查清单:

  1. VPS 是否正常运行
  2. 防火墙是否放行 UDP 端口
  3. 配置文件密钥是否正确
  4. IP 地址和端口是否正确
  5. 客户端网络是否正常

调试命令:

# 查看 WireGuard 状态
wg show

# 查看服务是否运行
systemctl status wg-quick@wg0

# 查看日志
journalctl -u wg-quick@wg0 -f

10.2 速度慢

可能原因:

  • 机房距离太远
  • VPS 带宽不足
  • 网络高峰期
  • MTU 设置不合理

解决方法:

  1. 尝试不同机房
  2. 调整 MTU 值
  3. 开启 BBR
  4. 升级 VPS 配置

10.3 经常断线

可能原因:

  • 网络不稳定
  • 客户端休眠
  • 服务器资源不足

解决方法:

  1. 客户端设置 PersistentKeepalive = 25
  2. 检查服务器负载
  3. 更换机房

10.4 无法访问某些网站

可能原因:

  • DNS 问题
  • IP 被封禁
  • 网站本身的问题

解决方法:

  1. 更换 DNS(如 1.1.1.1、8.8.8.8)
  2. 更换 VPS IP
  3. 确认网站是否正常

十一、WireGuard vs 其他协议

协议速度延迟安全性配置难度推荐度
WireGuard⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Shadowsocks⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
OpenVPN⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
V2Ray⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
Trojan⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐

十二、总结

12.1 搭建流程回顾

  1. 购买 VPS(推荐 Vultr 或 DigitalOcean)
  2. 使用一键脚本安装 WireGuard
  3. 生成客户端配置
  4. 安装客户端并导入配置
  5. 连接使用
  6. 按需优化和维护

12.2 推荐方案

用户类型推荐 VPS推荐协议月预算
个人轻度Vultr $5/月WireGuard~¥35/月
个人中度DigitalOcean $12/月WireGuard~¥85/月
多人共享Vultr $24/月WireGuard~¥170/月

12.3 最终建议

  1. 新手推荐使用一键脚本:简单快捷,不容易出错
  2. 先月付测试:满意再考虑年付
  3. 定期备份配置:保存好密钥和配置文件
  4. 注意安全:修改 SSH 端口,禁用密码登录
  5. 遵守法规:合理使用,遵守当地法律

关注 润土分享 获取更多 VPS 搭建教程!

相关阅读:VPS 推荐汇总

1.9k 词 · 2.5k 令牌