搭建VPN(虚拟专用网络)可以用于安全访问内网资源、加密网络流量或绕过地理限制。以下是常见的VPN搭建方法,分为自建服务器和使用第三方工具两种方案

自建VPN服务器

选择协议

  • OpenVPN:开源、跨平台、安全性高(推荐)。
  • WireGuard:速度快、配置简单,适合移动设备。
  • IPSec/L2TP:兼容性好,但配置复杂。
  • SoftEther:支持多协议,适合高级用户。

搭建步骤(以OpenVPN为例)

Linux服务器(Ubuntu/CentOS)

  1. 安装OpenVPN

    # Ubuntu/Debian
    sudo apt update && sudo apt install openvpn easy-rsa
    # CentOS/RHEL
    sudo yum install epel-release && sudo yum install openvpn easy-rsa
  2. 生成证书

    make-cadir ~/openvpn-ca
    cd ~/openvpn-ca
    source vars
    ./clean-all
    ./build-ca          # 生成CA证书
    ./build-key-server server  # 服务器证书
    ./build-key client1 # 客户端证书
    ./build-dh          # Diffie-Hellman参数
    openvpn --genkey --secret ta.key  # TLS-auth密钥
  3. 配置服务器

    • 复制示例配置:
      cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
      gunzip /etc/openvpn/server.conf.gz
    • 编辑/etc/openvpn/server.conf,确保包含:
      proto udp
      dev tun
      ca ~/openvpn-ca/keys/ca.crt
      cert ~/openvpn-ca/keys/server.crt
      key ~/openvpn-ca/keys/server.key
      dh ~/openvpn-ca/keys/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"
      keepalive 10 120
      tls-auth ~/openvpn-ca/keys/ta.key 0
      cipher AES-256-CBC
      user nobody
      group nogroup
      persist-key
      persist-tun
      status openvpn-status.log
      verb 3
  4. 启动服务

    sudo systemctl start openvpn@server
    sudo systemctl enable openvpn@server
  5. 配置客户端

    • ~/openvpn-ca/keys下载client1.crtca.crtclient1.keyta.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
      tls-auth ta.key 1
      cipher AES-256-CBC
      verb 3

Windows/macOS客户端

  • 使用OpenVPN GUI或Tunnelblick导入.ovpn文件即可连接。

WireGuard快速搭建

  1. 服务器安装

    # Ubuntu
    sudo apt install wireguard resolvconf
    # 生成密钥
    wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
  2. 配置服务器(/etc/wireguard/wg0.conf

    [Interface]
    PrivateKey = <服务器私钥>
    Address = 10.0.0.1/24
    ListenPort = 51820
    PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
    PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
    [Peer]
    PublicKey = <客户端公钥>
    AllowedIPs = 10.0.0.2/32
  3. 客户端配置

    • 生成客户端密钥,并在服务器配置中添加[Peer]

    • 客户端配置文件示例:

      [Interface]
      PrivateKey = <客户端私钥>
      Address = 10.0.0.2/24
      DNS = 8.8.8.8
      [Peer]
      PublicKey = <服务器公钥>
      Endpoint = your_server_ip:51820
      AllowedIPs = 0.0.0.0/0

使用第三方工具(快速方案)

  1. Outline VPN(基于Shadowsocks)

    • 一键安装脚本:
      bash -c "$(wget -qO- https://raw.githubusercontent.com/Jigsaw-Code/outline-server/master/src/server_manager/install_scripts/install_server.sh)"
    • 通过管理界面生成客户端访问密钥。
  2. SoftEther VPN

    提供图形化界面,支持L2TP/IPSec/OpenVPN等多种协议。


注意事项

  1. 安全建议

    • 防火墙开放VPN端口(如OpenVPN的UDP 1194,WireGuard的UDP 51820)。
    • 禁用root登录,使用SSH密钥认证。
    • 定期更新证书和软件。
  2. 网络优化

    • 启用BBR加速(Linux):
      echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
      echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
      sysctl -p
  3. 法律合规

    某些国家/地区限制VPN使用,需遵守当地法律法规。

搭建VPN(虚拟专用网络)可以用于安全访问内网资源、加密网络流量或绕过地理限制。以下是常见的VPN搭建方法,分为自建服务器和使用第三方工具两种方案

扫码下载飞鸟加速器

扫码下载飞鸟加速器

138-7634-5921
扫码下载飞鸟加速器

扫码下载飞鸟加速器