自己搭建Wireguard服务
🗒️自己搭建Wireguard服务
技术分享|2025-6-4|Last edited: 2025-6-4
type
status
date
slug
summary
tags
category
icon
password

如何在 VPS 上自行搭建 WireGuard 服务端:

是的,在 VPS 上自行搭建 WireGuard 服务端是非常常见和推荐的做法。
基本步骤(以 Linux VPS 为例):
  1. 选择 VPS 提供商: 首先你需要租用一个 VPS。选择时考虑服务器位置、价格、性能和操作系统(主流 Linux 发行版如 Ubuntu, Debian, CentOS 都支持 WireGuard)。
  1. 系统更新: 连接到你的 VPS (通常通过 SSH),并更新系统软件包。 Bash
    1. sudo apt update && sudo apt upgrade # For Ubuntu/Debian sudo yum update # For CentOS/RHEL
  1. 安装 WireGuard: 根据你的 Linux 发行版,安装 WireGuard 软件包和工具。 Bash
    1. sudo apt install wireguard # For Ubuntu/Debian # For CentOS/Fedora: # sudo yum install epel-release # sudo yum install wireguard-tools
  1. 生成密钥: WireGuard 使用公钥/私钥对进行认证和加密。你需要在服务器上生成一对密钥。 Bash
    1. wg genkey | tee privatekey | wg pubkey > publickey # 这将生成 privatekey (服务器私钥) 和 publickey (服务器公钥)
  1. 配置服务端: 创建 WireGuard 的配置文件,通常是 /etc/wireguard/wg0.conf。在这个文件中,你需要定义服务器的私钥、IP 地址(VPN 内部网络)、监听端口以及每个客户端的公钥和允许的 IP 地址。 Ini, TOML
    1. # /etc/wireguard/wg0.conf [Interface] PrivateKey = <你的服务器私钥> Address = 10.0.0.1/24 # 为 VPN 内部网络分配一个 IP 范围 ListenPort = 51820 # 默认端口,可以更改 # 以下是客户端配置示例,每个客户端添加一个 Peer 段 [Peer] PublicKey = <客户端1的公钥> AllowedIPs = 10.0.0.2/32 # 客户端1在 VPN 内部的 IP # PersistentKeepalive = 25 # 可选,保持连接活跃,防止NAT超时 [Peer] PublicKey = <客户端2的公钥> AllowedIPs = 10.0.0.3/32 # 客户端2在 VPN 内部的 IP
  1. 配置防火墙和 IP 转发: 确保防火墙允许 WireGuard 端口的 UDP 流量通过,并启用 IP 转发,以便 VPN 客户端的流量能够通过服务器访问互联网。
  1. 启动 WireGuard: 启动 WireGuard 接口并使其开机自启。 Bash
    1. sudo wg-quick up wg0 sudo systemctl enable wg-quick@wg0
  1. 客户端配置: 在你的客户端设备(手机、电脑)上安装 WireGuard 客户端,生成客户端的密钥对,然后将客户端的公钥添加到服务器的 wg0.conf 中,并将服务器的公钥、IP、端口等信息添加到客户端的配置中。
优点:
  • 成本效益: VPS 租用成本通常比商业 VPN 服务便宜,尤其适合长期使用。
  • 隐私性强: 你是自己 VPN 的唯一用户(或你信任的用户),没有人会记录你的连接日志(除非你主动配置)。
缺点:
  • 技术门槛: 需要一定的 Linux 命令行和网络知识。
  • 需要自己维护: 服务器的更新、维护和故障排除都需要自己负责。
总的来说,对于具备一定技术能力的个人用户,在 VPS 上自建 WireGuard 是一个非常优秀、高效且自由度高的翻墙解决方案。
 
四大免费翻墙软件翻墙技术的进展2025
Loading...