在VPS Ubuntu上安装WireGuard VPN服务器

目录

WireGuard是2026年最受推崇的现代VPN协议。凭借超越OpenVPN和IPsec的性能、极简的代码库以及简单的配置方式,它是在自己VPS上搭建私有VPN服务器的理想选择。本指南将带您完成从安装到客户端成功连接的每一个步骤。

WireGuard是什么以及为什么在VPS上使用

WireGuard由Jason Donenfeld开发,于2020年并入Linux内核5.6。其核心优势源于极简设计——约4,000行代码,而OpenVPN超过70,000行:

在自己的VPS上运行WireGuard,您可以100%掌控流量,无需为商业VPN服务支付月费。

WireGuard vs OpenVPN vs IPsec对比

比较项目WireGuardOpenVPNIPsec/IKEv2
代码行数~4,000行~70,000行非常复杂
性能⭐⭐⭐⭐⭐(内核级)⭐⭐⭐(用户空间)⭐⭐⭐⭐
配置复杂度简单中等复杂
漫游支持极佳一般良好
协议仅UDPUDP/TCPUDP/ESP
Linux内核支持5.6+(内置)需要安装内置

安装前提条件

💡 提示: AsiaGB所有VPS套餐均包含公网IP,运行内核5.15+,WireGuard已内置,无需额外安装内核模块。SSD存储,99%正常运行时间保障。

在Ubuntu上安装WireGuard

更新软件包列表并安装WireGuard:

sudo apt update && sudo apt upgrade -y
sudo apt install wireguard -y

验证安装:

wg --version
# 输出:wireguard-tools v1.0.20210914 - https://git.zx2c4.com/wireguard-tools/

生成服务器密钥对:

wg genkey | tee /etc/wireguard/server_private.key | wg pubkey > /etc/wireguard/server_public.key
chmod 600 /etc/wireguard/server_private.key
cat /etc/wireguard/server_private.key   # 保存此私钥
cat /etc/wireguard/server_public.key    # 此公钥分享给客户端

⚠️ 重要: 切勿将私钥分享给任何人。只有公钥才能分享给客户端。

配置WireGuard服务器接口

为wg0接口创建配置文件:

sudo nano /etc/wireguard/wg0.conf

添加以下内容(将SERVER_PRIVATE_KEY替换为实际密钥):

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

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

💡 注意: 将eth0替换为您VPS的实际网络接口名称,通过ip route | grep default命令查看。

配置UFW防火墙和IP转发

启用IP转发

echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

开放WireGuard端口

sudo ufw allow 51820/udp
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

启动并启用服务

sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
sudo systemctl status wg-quick@wg0

验证接口状态:

sudo wg show
# 显示:接口wg0、公钥、监听端口51820

添加客户端节点

为每个客户端单独生成密钥对:

wg genkey | tee client1_private.key | wg pubkey > client1_public.key

在服务器配置文件(/etc/wireguard/wg0.conf)中添加Peer:

[Peer]
# 客户端1
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32

无需重启即可应用更改:

sudo wg syncconf wg0 <(sudo wg-quick strip wg0)

💡 添加多个客户端: 为每个客户端分配不同IP,如Client2使用10.0.0.3/32,并为每个客户端添加独立的[Peer]块。

在iOS、Android和PC上配置客户端

客户端配置文件

[Interface]
PrivateKey = CLIENT1_PRIVATE_KEY
Address = 10.0.0.2/32
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_VPS_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
平台导入方式应用程序
iOS(iPhone/iPad)扫描二维码或导入文件WireGuard(App Store)
Android扫描二维码或导入文件WireGuard(Play Store)
Windows添加隧道 → 从文件导入WireGuard for Windows
macOS添加隧道 → 从文件导入WireGuard(App Store)
Linuxwg-quick up client1wireguard-tools

为移动设备生成二维码

sudo apt install qrencode -y
qrencode -t ansiutf8 < client1.conf

二维码将显示在终端中,使用手机上的WireGuard应用扫描即可。

常见问题排查

已连接但无法访问互联网

验证IP转发已启用,且PostUp/PostDown使用了正确的接口名称:

sysctl net.ipv4.ip_forward
# 必须返回:net.ipv4.ip_forward = 1

客户端完全无法连接

❓ 常见问题解答(FAQ)
WireGuard和OpenVPN有什么区别?
WireGuard只有约4,000行代码,而OpenVPN有约70,000行,因此更易于安全审计。WireGuard作为内核模块运行,速度更快,网络切换时也更稳定。
WireGuard使用哪个端口?
默认使用UDP端口51820。可以通过修改配置文件中的ListenPort来更改。
运行WireGuard VPN服务器需要多少内存?
WireGuard非常轻量。1GB内存的VPS可以同时处理数十个客户端,因为WireGuard作为内核模块运行。
可以添加多个客户端吗?
可以。在/etc/wireguard/wg0.conf中为每个客户端添加一个[Peer]块,每个客户端使用不同的公钥和AllowedIPs地址。
VPS重启后WireGuard还能运行吗?
运行systemctl enable wg-quick@wg0后,WireGuard会在每次重启时自动启动。
WireGuard能隐藏我的真实IP地址吗?
可以。连接WireGuard后,所有流量都从VPS的IP地址发出,网站看到的是VPS的IP,而不是您的真实IP。

🎯 总结:在VPS Ubuntu上安装WireGuard VPN服务器

WireGuard是2026年自建VPN服务器的最佳协议选择。主要步骤如下:

  1. 安装:apt install wireguard
  2. 生成服务器和客户端密钥对
  3. 创建/etc/wireguard/wg0.conf
  4. 启用IP转发并配置UFW/iptables
  5. 运行systemctl enable --now wg-quick@wg0
  6. 将客户端配置文件或二维码分发给用户

需要搭建WireGuard的VPS?查看AsiaGB VPS套餐,每月500泰铢起,SSD存储,99%正常运行时间。