TOP云拥有分布在全国及海外各地丰富的数据中心节点资源,可以选择我们的云电脑用来挂机,用在游戏挂机、网店挂机、QQ挂机、网赚项目挂机等等方面,减少封号风险,我们的云电脑有电信、联通、移动等各类稳定线路,全部是独享ip地址,有win10、win7等各类个人家庭操作系统,让您的挂机需求得心应手,选购地址:

TOP云总站云服务器购买链接:https://topyun.vip/server/buy.html

TOP云C站云服务器购买链接:https://c.topyun.vip/cart

使用云服务器搭建私有VPN(如OpenVPN、WireGuard)可实现安全远程访问或加密网络流量。以下是分步指南及注意事项:


一、前期准备

  1. 选择云服务器

    • 地区:选择靠近目标用户的机房(如国内用户选香港/新加坡节点)。

    • 系统:Linux(Ubuntu 22.04/CentOS 7+)或 Windows Server(需图形界面支持)。

    • 带宽:按需选择(如1-5Mbps足够个人使用,企业级需更高)。

    • 推荐配置

    • 服务商:阿里云、腾讯云、AWS Lightsail(低成本VPS)等。

  2. 安全基础设置

    • 修改默认SSH端口(如从22改为50022),禁用密码登录,仅允许密钥认证。

    • 配置防火墙(如ufw或iptables),仅放行VPN端口(如OpenVPN的1194/UDP或WireGuard的51820/UDP)。


二、搭建OpenVPN(适合全流量加密)

1. 安装OpenVPN和Easy-RSA(证书管理)

# Ubuntu/Debian
sudo apt update && sudo apt install openvpn easy-rsa -y

# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install openvpn easy-rsa -y

2. 配置PKI(证书颁发机构)

make-cadir ~/openvpn-ca  # 创建证书目录
cd ~/openvpn-ca

编辑vars文件,设置证书参数(如国家、组织名称):

nano vars
# 修改示例:
export KEY_COUNTRY="CN"
export KEY_PROVINCE="Beijing"
export KEY_CITY="Beijing"
export KEY_ORG="YourName"
export KEY_EMAIL="your@email.com"

加载变量并生成CA证书:

source vars
./clean-all
./build-ca # 交互式生成CA证书

3. 生成服务器证书和密钥

./build-key-server server  # 生成服务器证书(名称固定为server)
./build-dh               # 生成Diffie-Hellman密钥(耗时较长)
openvpn --genkey --secret keys/ta.key  # 生成TLS-auth密钥(防DDoS)

4. 配置OpenVPN服务端

复制示例配置文件并编辑:

gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz > /etc/openvpn/server.conf
nano /etc/openvpn/server.conf

关键配置项:

port 1194
proto udp
dev tun
ca /home/youruser/openvpn-ca/keys/ca.crt
cert /home/youruser/openvpn-ca/keys/server.crt
key /home/youruser/openvpn-ca/keys/server.key
dh /home/youruser/openvpn-ca/keys/dh.pem
server 10.8.0.0 255.255.255.0 # VPN子网 push "redirect-gateway def1 bypass-dhcp" # 客户端所有流量走VPN push "dhcp-option DNS 8.8.8.8" # 推送DNS服务器 keepalive 10 120
tls-auth /home/youruser/openvpn-ca/keys/ta.key 0
cipher AES-256-CBC
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn-status.log
verb 3

5. 启用IP转发和NAT(关键步骤!)

echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

配置NAT规则(假设服务器网卡为eth0):

iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
iptables-save > /etc/iptables.rules # 持久化规则(需配置开机加载)

6. 启动OpenVPN

sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

7. 创建客户端证书

cd ~/openvpn-ca
./build-key client1 # 生成客户端证书(名称可自定义)

将以下文件打包发给客户端:

  • ca.crt(CA证书)

  • client1.crt(客户端证书)

  • client1.key(客户端密钥)

  • ta.key(TLS-auth密钥)

  • server.conf(服务端配置参考)


三、搭建WireGuard(轻量级高性能VPN)

1. 安装WireGuard

# Ubuntu/Debian
sudo apt install wireguard resolvconf -y

# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install wireguard-tools -y

2. 生成密钥对

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

记录输出的privatekey(服务器私钥)和publickey(服务器公钥)。

3. 配置WireGuard服务端

创建配置文件:

nano /etc/wireguard/wg0.conf

示例配置:

[Interface]
Address = 10.0.0.1/24  # VPN子网IP
ListenPort = 51820
PrivateKey = <服务器私钥>
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  # 客户端VPN内网IP

4. 启用IP转发和NAT

与OpenVPN相同,需配置net.ipv4.ip_forward=1和iptables规则。

5. 启动WireGuard

wg-quick up wg0
systemctl enable wg-quick@wg0

6. 创建客户端配置

客户端配置文件示例(client.conf):

[Interface]
PrivateKey = <客户端私钥>
Address = 10.0.0.2/24  # 客户端VPN内网IP
DNS = 8.8.8.8

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器公网IP>:51820
AllowedIPs = 0.0.0.0/0  # 全流量走VPN(或指定子网如10.0.0.0/24)
PersistentKeepalive = 25

四、客户端连接

OpenVPN客户端

  • 下载OpenVPN客户端(Windows/macOS/Linux/Android/iOS)。

  • 导入client1.ovpn文件(包含证书和密钥)。

WireGuard客户端

  • 安装WireGuard App,导入client.conf文件。


五、关键注意事项

  1. 安全加固

    • 禁用SSH密码登录,使用密钥认证。

    • 定期更新VPN软件(如apt upgrade openvpn)。

    • 限制VPN登录IP(如仅允许办公IP连接)。

  2. 法律合规性

    • 个人使用通常无问题,但企业级VPN需遵守当地法律(如中国需备案)。

    • 禁止用于非法活动(如绕过审查、攻击他人网络)。

  3. 性能优化

    • 启用TCP BBR加速(sysctl -w net.ipv4.tcp_congestion_control=bbr)。

    • 选择低延迟云服务器(如腾讯云轻量应用服务器)。

  4. 故障排查

    • 检查服务状态:systemctl status openvpn@server 或 wg show。

    • 查看日志:journalctl -u openvpn@server -f。


通过以上步骤,即可搭建私有VPN。WireGuard更适合高性能需求,OpenVPN兼容性更广。根据实际场景选择方案!


不容错过
Powered By TOPYUN 云产品资讯