搭建WireGuard


  1. 选择一台 VPS 主机:首先,选择一台合适的 VPS 主机,确保其操作系统为支持 WireGuard 的 Linux 发行版,如
    Ubuntu、Debian、CentOS 等。
  2. 安装 WireGuard:使用 VPS 主机的终端连接工具,如 SSH,以 root 用户登录 VPS 主机,然后使用以下命令安装
    WireGuard:
# Ubuntu / Debian
apt-get update && apt-get install wireguard

# CentOS / RedHat
yum update && yum install epel-release && yum install wireguard-dkms wireguard-tools
  1. 生成公私钥:使用以下命令生成 WireGuard 配置所需的公私钥:
# 生成私钥
wg genkey > privatekey

# 生成公钥
wg pubkey < privatekey > publickey
  1. 配置 WireGuard:在 VPS 主机上创建一个 WireGuard 配置文件 wg0.conf,将以下配置信息添加到该文件中:
[Interface]
Address = 10.0.0.1/24
PrivateKey = <VPS主机私钥>
ListenPort = 51820

[Peer]
PublicKey = <客户端公钥>
AllowedIPs = 10.0.0.2/32
  1. 启动 WireGuard:使用以下命令启动 WireGuard:
wg-quick up wg0
  1. 配置客户端:在客户端上安装 WireGuard,生成公私钥,并创建客户端的 WireGuard 配置文件,将以下配置信息添加到该文件中:
[Interface]
Address = 10.0.0.2/24
PrivateKey = <客户端私钥>
DNS = 8.8.8.8

[Peer]
PublicKey = <VPS主机公钥>
AllowedIPs = 0.0.0.0/0
Endpoint = <VPS主机IP地址>:51820
PersistentKeepalive = 15

其中,Address 是客户端的 WireGuard 网络地址,PrivateKey 是客户端的私钥,DNS 是客户端的 DNS 服务器地址。Peer 是指 VPS 主机的配置信息,PublicKey 是 VPS 主机的公钥,AllowedIPs 是指客户端可以访问的 IP 地址范围,Endpoint 是指 VPS 主机的 IP 地址和端口号,PersistentKeepalive 是指客户端和服务器之间的心跳间隔时间。

  1. 启动客户端:在客户端上使用以下命令启动 WireGuard:
wg-quick up wg0
  1. 测试连接:使用 ping 命令测试客户端和 VPS 主机之间的连接,如:
ping 10.0.0.1

如果返回的结果是连通的,则说明 WireGuard 连接成功。

声明:樱迷博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 搭建WireGuard


Record and Share Technology