iaun
iaun
发布于 2026-01-21 / 37 阅读
0
0

WireGuard VPS 互联

前提条件 & 假设

  1. 开放机器的 51820 (udp) 端口

  2. 定义 WireGuard 网段,如:192.168.128.0/24

  3. 假设存在 3 个节点,IP 分别为:192.168.128.1192.168.128.2192.168.128.3

版本

  • 系统:Ubuntu 24.04

安装 (所有节点)

  1. 安装 WireGuard

sudo apt-get update
sudo apt-get install -y wireguard
  1. 生成密钥

mkdir -p $HOME/wireguard
cd $HOME/wireguard
wg genkey | tee private.key | wg pubkey > public.key
  1. 创建配置文件 (在所有节点创建)

格式如下,写入到 /etc/wireguard/wg0.conf

[Interface]
Address = <当前节点的VPN IP>/24
PrivateKey = <当前节点的Private Key>
ListenPort = 51820

[Peer]
PublicKey = <其他节点1的Public Key>
AllowedIPs = <其他节点1的VPN IP>/32
Endpoint = <其他节点1的公网IP>:51820

……

[Peer]
PublicKey = <其他节点n的Public Key>
AllowedIPs = <其他节点n的VPN IP>/32
Endpoint = <其他节点n的公网IP>:51820

e.g.

[Interface]
Address = 192.168.128.1/24
PrivateKey = ***
ListenPort = 51820

[Peer]
PublicKey = ***
AllowedIPs = 192.168.128.2/32
Endpoint = node2.example.com:51820

[Peer]
PublicKey = ***
AllowedIPs = 192.168.128.3/32
Endpoint = node3.example.com:51820
  1. 启动服务

sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0
  1. 重启网卡

sudo wg-quick down wg0
sudo wg-quick up wg0

可选

设置 MTU

在 /etc/wireguard/wg0.conf 的 [Interface] 中,添加:

MTU = XXXX

e.g.:

MTU = 1380


评论