zh

WireGuard VPS配置:搭建自托管VPN服务器

WireGuard是目前最快速、最现代化的VPN协议,其精简的代码库(仅约4000行)相比OpenVPN和IPSec更易于审计和维护。在离岸VPS上自建WireGuard服务器,你将拥有完整的控制权,无需信任任何第三方VPN服务商。Anubiz Host的冰岛VPS提供不受监控的网络环境,是搭建个人WireGuard服务器的理想基础设施。

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

安装WireGuard并生成密钥对

在Debian/Ubuntu系统上安装WireGuard只需几个命令。连接SSH后执行以下步骤:
apt update && apt install -y wireguard
# 生成服务器密钥对
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_public.key
记录公钥内容,配置客户端时需要用到。同样为每个客户端生成密钥对:
wg genkey | tee client1_private.key | wg pubkey > client1_public.key
WireGuard使用Curve25519椭圆曲线加密,密钥对生成后立即可用,无需证书颁发机构。

配置服务器wg0.conf文件

创建WireGuard服务器配置文件,将SERVER_PRIVATE_KEY替换为实际私钥,SERVER_IP替换为VPS的公网IP:
cat > /etc/wireguard/wg0.conf << EOF
[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

[Peer]
PublicKey = CLIENT1_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32
EOF
启用IP转发并启动服务:
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
systemctl enable --now wg-quick@wg0
wg show
防火墙需开放UDP 51820端口:
ufw allow 51820/udp

客户端配置与连接

在客户端(Windows/macOS/Linux/Android/iOS)安装WireGuard应用后,创建配置文件:
[Interface]
PrivateKey = CLIENT1_PRIVATE_KEY
Address = 10.0.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
「AllowedIPs = 0.0.0.0/0」表示所有流量都通过VPN隧道路由,实现全局代理效果。如果只需代理特定流量,可以填写具体IP段。 连接成功后,通过以下命令验证服务器端状态:
wg show wg0
输出中可以看到已连接的客户端、传输数据量和最近握手时间。WireGuard的握手每隔约3分钟自动刷新,保持连接活跃。

性能优化与安全加固

WireGuard默认性能已经很出色,但以下调整能进一步提升体验。调整MTU避免IP分片问题:
# 在[Interface]段添加:
MTU = 1420
通过脚本批量管理客户端,添加新客户端时无需重启服务:
wg set wg0 peer NEW_CLIENT_PUBLIC_KEY allowed-ips 10.0.0.X/32
wg-quick save wg0
安全加固建议:定期更换密钥对(每季度),设置强防火墙规则限制非WireGuard端口的访问,启用fail2ban防止端口扫描。Anubiz Host冰岛节点提供DDoS防护,配合WireGuard的混淆特性,能有效防止VPN流量被识别和封锁。

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online