WireGuard 是一款小巧而快速的 VPN。在您自己的 VPS 上运行它大约只需十分钟,并且能给您一个固定地址和完全的控制权。本指南使用具有 root 权限的 Debian 或 Ubuntu。其他系统上的命令略有不同,如果有不一致的地方,请查阅官方的 WireGuard 文档。
以 root 身份登录并运行:
apt update && apt install -y wireguard
umask 077
wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub
wg genkey | tee /root/client1.key | wg pubkey > /root/client1.pub服务器和每个客户端都有一个私钥(保密)和一个公钥(与对方共享)。
用 ip route get 1.1.1.1 找到网络接口的名称(通常是 eth0 或 ens3)。然后创建 /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = (contents of server.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
[Peer]
PublicKey = (contents of client1.pub)
AllowedIPs = 10.8.0.2/32请把 eth0 替换为您的接口名称。开启转发并启动隧道:
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-wg.conf && sysctl --system
systemctl enable --now wg-quick@wg0在防火墙中放行 WireGuard 端口和 SSH,不需要的一律不要开放。使用 ufw:
ufw allow 22/tcp
ufw allow 51820/udp
ufw enable在客户端上创建如下配置,并导入 WireGuard 应用:
[Interface]
PrivateKey = (contents of client1.key)
Address = 10.8.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = (contents of server.pub)
Endpoint = YOUR.SERVER.IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25连接后,在任意“我的 IP 是什么”页面上查看您的公网地址。它应该显示服务器的地址。
相关内容:VPN 服务器托管 · 为 Xray、V2Ray 或 AmneziaWG 选择 VPS · 测试 IP · No-KYC VPS · 常见问题
查看 Fast VPS 套餐(每月 $7.99 起)、NVMe VPS 套餐或独立服务器。只需邮箱地址和昵称即可注册,可用 Monero、Bitcoin、Litecoin、USDT、USDC、SOL 或 TRX 支付,也可用银行卡支付。新的 VPS 订单享有 24 小时保障。付款前,您可以对我们的测试 IP 执行 ping,并查看实时状态页面。