在 Linux VPS 上搭建 VPN(如 OpenVPN 或 WireGuard)可以为你提供安全的远程访问和网络加密,以下是两种主流方案的快速指南:
方案1:WireGuard(推荐)
WireGuard 是现代、高效且配置简单的 VPN 协议。
步骤
-
连接 VPS
ssh root@your_vps_ip
-
安装 WireGuard
sudo apt update && sudo apt install wireguard -y # Debian/Ubuntu sudo yum install epel-release && sudo yum install wireguard-tools -y # CentOS
-
生成密钥对
umask 077 wg genkey | tee privatekey | wg pubkey > publickey
-
配置服务端 创建
/etc/wireguard/wg0.conf[Interface] Address = 10.0.0.1/24 PrivateKey = <粘贴服务端的privatekey> 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 = <客户端的publickey> AllowedIPs = 10.0.0.2/32
-
启用 IP 转发
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf sysctl -p
-
启动 WireGuard
wg-quick up wg0 systemctl enable wg-quick@wg0
-
客户端配置 创建客户端配置文件(如
client.conf):[Interface] PrivateKey = <客户端的privatekey> Address = 10.0.0.2/24 [Peer] PublicKey = <服务端的publickey> Endpoint = your_vps_ip:51820 AllowedIPs = 0.0.0.0/0 PersistentKeepalive = 25
方案2:OpenVPN
OpenVPN 功能丰富,但配置稍复杂。
快速安装(使用脚本)
-
运行自动化脚本
wget https://git.io/vpn -O openvpn-install.sh && bash openvpn-install.sh
- 按提示输入配置(如 IP、端口、协议等)。
- 脚本会自动生成客户端配置文件(
.ovpn文件)。
-
下载客户端配置
scp root@your_vps_ip:~/client.ovpn ~/
-
客户端使用
- 下载 OpenVPN Client 导入
.ovpn文件即可连接。
- 下载 OpenVPN Client 导入
防火墙设置
确保放行 VPN 端口:
# OpenVPN ufw allow 1194/udp # 默认端口
注意事项
-
安全建议
- 使用非默认端口。
- 限制 VPN 端口的访问 IP(如仅允许本国 IP)。
- 定期更新软件:
sudo apt upgrade。
-
性能优化
- WireGuard 更适合移动设备(低延迟)。
- OpenVPN 兼容性更广(支持 TCP 模式)。
-
故障排查
- 检查日志:
journalctl -u wg-quick@wg0或tail -f /var/log/syslog。 - 测试连通性:
ping 10.0.0.1(服务端内网 IP)。
- 检查日志:
选择 WireGuard 更轻量,适合个人使用;OpenVPN 则适合企业复杂场景,根据需求选择即可!








京公网安备11000000000001号
京ICP备11000001号