选择VPN类型
- 远程访问VPN(如家庭用户连接公司网络)
协议:OpenVPN、WireGuard、IPsec/L2TP
- 站点到站点VPN(连接两个局域网)
协议:IPsec、OpenVPN
常见搭建方案
方案1:OpenVPN(跨平台支持)
- 安装服务端(以Ubuntu为例):
sudo apt update sudo apt install openvpn easy-rsa
- 配置CA和证书:
make-cadir ~/openvpn-ca cd ~/openvpn-ca nano vars # 修改证书信息 source vars ./clean-all ./build-ca # 创建根证书 ./build-key-server server # 服务器证书 ./build-key client1 # 客户端证书
- 生成Diffie-Hellman和HMAC:
./build-dh openvpn --genkey --secret keys/ta.key
- 配置服务端文件:
- 复制模板并编辑:
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
- 关键配置:
port 1194 proto udp dev tun ca ca.crt cert server.crt key server.key dh dh.pem server 10.8.0.0 255.255.255.0 push "redirect-gateway def1 bypass-dhcp" # 客户端所有流量走VPN
- 复制模板并编辑:
- 启动服务:
sudo systemctl start openvpn@server sudo systemctl enable openvpn@server
方案2:WireGuard(高性能,配置简单)
-
安装服务端(Linux):
sudo apt install wireguard wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
-
配置服务端(
/etc/wireguard/wg0.conf):[Interface] PrivateKey = <服务器私钥> Address = 10.0.0.1/24 ListenPort = 51820 [Peer] # 客户端配置 PublicKey = <客户端公钥> AllowedIPs = 10.0.0.2/32
-
启动服务:
sudo wg-quick up wg0 sudo systemctl enable wg-quick@wg0
方案3:商业VPN服务(快速部署)
- 使用Tailscale/ZeroTier(基于WireGuard,无需公网IP):
curl -fsSL https://tailscale.com/install.sh | sh sudo tailscale up
客户端配置
-
OpenVPN客户端:
- 将生成的
client1.crt、client1.key、ca.crt和ta.key复制到客户端。 - 使用配置文件示例:
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
- 将生成的
-
WireGuard客户端:
-
生成密钥对后配置客户端文件(如手机/电脑):
[Interface] PrivateKey = <客户端私钥> Address = 10.0.0.2/24 [Peer] PublicKey = <服务器公钥> Endpoint = your-server-ip:51820 AllowedIPs = 0.0.0.0/0 # 所有流量走VPN
-
高级设置
- 防火墙规则(开放VPN端口):
sudo ufw allow 1194/udp # OpenVPN sudo ufw allow 51820/udp # WireGuard
- 日志检查:
journalctl -u openvpn@server sudo wg show # WireGuard状态
安全建议
- 使用强加密算法(如AES-256)。
- 定期轮换证书和密钥。
- 限制VPN访问IP(如企业VPN仅允许特定IP段)。
常见问题
- 连接失败:检查服务端端口是否开放,客户端配置是否匹配。
- 速度慢:尝试更换协议(如WireGuard比OpenVPN更快)。
根据需求选择方案:WireGuard适合高性能场景,OpenVPN兼容性更广,商业方案则省去维护成本。


