自建VPN服务器
选择协议
常见VPN协议:
- OpenVPN:开源、安全,适合大多数场景(推荐)。
- WireGuard:高性能、配置简单,适合移动设备。
- IPSec/L2TP:兼容性好,但配置复杂。
安装与配置(以OpenVPN为例)
Linux服务器(Ubuntu/Debian)
-
安装OpenVPN
sudo apt update sudo apt install openvpn easy-rsa
-
生成证书
make-cadir ~/openvpn-ca cd ~/openvpn-ca nano vars # 修改证书信息(如国家、组织名) source vars ./clean-all ./build-ca # 生成CA证书 ./build-key-server server # 服务器证书 ./build-key client1 # 客户端证书 ./build-dh # Diffie-Hellman密钥
-
配置服务器
cp ~/openvpn-ca/keys/{server.crt,server.key,ca.crt,dh2048.pem} /etc/openvpn/ cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/ gzip -d /etc/openvpn/server.conf.gz nano /etc/openvpn/server.conf修改关键参数:
proto udp dev tun ca ca.crt cert server.crt key server.key dh dh2048.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" push "dhcp-option DNS 8.8.8.8" -
启动服务
sudo systemctl start openvpn@server sudo systemctl enable openvpn@server
-
配置客户端
- 从服务器下载
ca.crt、client1.crt、client1.key。 - 创建客户端配置文件(如
client.ovpn):client dev tun proto udp remote your_server_ip 1194 resolv-retry infinite nobind persist-key persist-tun ca ca.crt cert client1.crt key client1.key
- 从服务器下载
Windows/macOS客户端
- 安装OpenVPN客户端,导入
.ovpn文件即可连接。
其他自建方案
-
WireGuard:更轻量,适合高速需求。
# Ubuntu安装 sudo apt install wireguard wg genkey | tee privatekey | wg pubkey > publickey
配置示例(
/etc/wireguard/wg0.conf):[Interface] PrivateKey = <服务器私钥> Address = 10.0.0.1/24 ListenPort = 51820 [Peer] PublicKey = <客户端公钥> AllowedIPs = 10.0.0.2/32
使用第三方VPN服务
如果不想自建,可选择付费服务:
- 推荐提供商:
- NordVPN、ExpressVPN、Surfshark(隐私政策透明,速度快)。
- ProtonVPN(瑞士隐私保护,免费版可用)。
- 步骤:
- 注册账号,下载客户端应用。
- 登录后一键连接。
注意事项
- 安全性:
- 自建VPN需定期更新证书和软件。
- 避免使用默认端口(如1194),可改为非常用端口。
- 防火墙:
sudo ufw allow 1194/udp # OpenVPN sudo ufw allow 51820/udp # WireGuard
- 法律合规:
部分国家限制VPN使用,需遵守当地法律。
常见问题
- 连接失败:检查服务器IP、端口、防火墙规则。
- 速度慢:尝试更换协议(如WireGuard)或服务器位置。
如果需要更具体的配置(如 Raspberry Pi 或 AWS 部署),可进一步说明需求!








