Networking~15 min更新于 2026-08-13
如何在VPS上设置WireGuard VPN?
在您的VPS上安装WireGuard,生成密钥对,并使用简单的配置文件配置服务器和客户端。然后启动接口并测试连接。
本指南将帮助您在VPS上设置自己的WireGuard VPN服务器,为您提供安全快速的加密隧道。您将完全控制您的VPN,无日志且无第三方依赖。
前提条件:具有公共IP地址的VPS(任何MurmurHost VPS套餐均可,每月$8起)、root或sudo访问权限,以及本地客户端(Windows、macOS、Linux或移动设备)。
- 1
安装WireGuard
通过SSH登录您的VPS并安装WireGuard。在Ubuntu/Debian上:sudo apt update && sudo apt install wireguard。在CentOS/RHEL上:sudo yum install epel-release && sudo yum install wireguard-tools。 - 2
生成服务器密钥
为服务器生成私钥和公钥对:wg genkey | tee server_private.key | wg pubkey > server_public.key。保持私钥机密。 - 3
创建服务器配置
创建/etc/wireguard/wg0.conf,内容如下,将ServerPrivateKey替换为您的服务器私钥,将ServerIP替换为您的VPS公网IP:[Interface] Address = 10.0.0.1/24 SaveConfig = true ListenPort = 51820 PrivateKey = ServerPrivateKey [Peer] PublicKey = ClientPublicKey AllowedIPs = 10.0.0.2/32
- 4
启用IP转发
在服务器上启用IP转发:echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf && sudo sysctl -p。如果您希望客户端访问互联网,还要使用iptables设置NAT:sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE(将eth0替换为您的主接口)。 - 5
启动WireGuard
启动WireGuard接口并设置开机自启:sudo systemctl start wg-quick@wg0和sudo systemctl enable wg-quick@wg0。 - 6
生成客户端密钥和配置
在您的本地机器上,生成客户端密钥对:wg genkey | tee client_private.key | wg pubkey > client_public.key。创建客户端配置文件(例如client.conf),内容如下:[Interface] PrivateKey = ClientPrivateKey Address = 10.0.0.2/24 DNS = 1.1.1.1 [Peer] PublicKey = ServerPublicKey Endpoint = ServerIP:51820 AllowedIPs = 0.0.0.0/0
- 7
添加客户端到服务器
在服务器上,将客户端的公钥添加到配置中:sudo wg set wg0 peer ClientPublicKey allowed-ips 10.0.0.2/32。如果您使用了配置文件,请重启 WireGuard:sudo systemctl restart wg-quick@wg0。 - 8
测试连接
将客户端配置导入您的 WireGuard 客户端应用(或在 Linux 上使用wg-quick up client.conf)。Ping 服务器:ping 10.0.0.1。然后检查您的公网 IP 以确认流量通过 VPN 路由。
确保您的VPS防火墙允许UDP端口51820。MurmurHost包含DDoS防护,但您可能仍想配置防火墙以增加安全性。由于MurmurHost是离岸且免KYC的,您可以完全私密地设置VPN,如果您愿意,可以使用Monero或Bitcoin支付。
常见问题
WireGuard的默认端口是什么?+
默认端口是51820/UDP,但您可以在配置文件中将其更改为任何端口。
我可以在MurmurHost VPS上使用WireGuard吗?+
是的,任何MurmurHost VPS套餐都支持WireGuard。入门级VPS每月8美元,配备1个vCPU、2 GB内存和25 GB NVMe,对于个人VPN绰绰有余。
如何让WireGuard在启动时自动运行?+
使用systemd:
sudo systemctl enable wg-quick@wg0。这将在每次服务器启动时启动VPN接口。WireGuard比OpenVPN更快吗?+
是的,WireGuard通常更快更高效,因为它使用现代密码学并在内核中运行。它还有更小的代码库,使其更易于审计和安全。