购买搬瓦工 VPS
- 访问官网 BandwagonHost,注册账号并购买适合的 VPS(推荐选择 KVM 架构,支持 OpenVPN/WireGuard)。
- 记下 VPS 的 IP 地址、SSH 端口、root 密码(或 SSH 密钥)。
连接 VPS(通过 SSH)
- Linux/macOS:使用终端输入:
ssh root@你的VPS_IP -p SSH端口
- Windows:使用 PuTTY 连接。
安装 VPN 服务(任选其一)
一键安装 OpenVPN(推荐新手)
运行一键脚本(如 angristan/openvpn-install):
wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh
- 按提示操作(默认配置即可)。
- 脚本完成后,会在
/root/生成客户端配置文件(如client.ovpn)。
手动安装 WireGuard
# 生成密钥 wg genkey | tee /etc/wireguard/privatekey | wg pubkey | tee /etc/wireguard/publickey # 配置服务端(编辑文件) nano /etc/wireguard/wg0.conf
示例配置:
[Interface] PrivateKey = <服务器私钥> Address = 10.0.0.1/24 ListenPort = 51820 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 = <客户端公钥> AllowedIPs = 10.0.0.2/32
启动 WireGuard:
wg-quick up wg0 systemctl enable wg-quick@wg0
配置防火墙
-
开放端口:
# OpenVPN 默认端口 1194 ufw allow 1194/udp # WireGuard 默认端口 51820 ufw allow 51820/udp
-
启用防火墙:
ufw enable
客户端连接
-
OpenVPN:
- 下载
/root/client.ovpn文件,导入 OpenVPN 客户端(如 OpenVPN Client)。
- 下载
-
WireGuard:
-
创建客户端配置(需匹配服务器公钥和IP),示例:
[Interface] PrivateKey = <客户端私钥> Address = 10.0.0.2/24 [Peer] PublicKey = <服务器公钥> Endpoint = 你的VPS_IP:51820 AllowedIPs = 0.0.0.0/0
-
导入 WireGuard 客户端(如 WireGuard App)。
-
测试连接
- 客户端连接后,访问 IP 检测网站 确认 IP 已变为 VPS 的 IP。
常见问题
- 连接失败:检查防火墙、端口是否开放,VPS 是否正常运行。
- 速度慢:尝试更换 VPS 机房(搬瓦工支持迁移数据中心)。
通过以上步骤,你可以快速在搬瓦工 VPS 上搭建 VPN,如需更高级配置(如 Shadowsocks、Trojan),可参考相关脚本或文档。
