前提条件 & 假设
开放机器的 51820 (udp) 端口
定义 WireGuard 网段,如:192.168.128.0/24
假设存在 3 个节点,IP 分别为:192.168.128.1,192.168.128.2,192.168.128.3
版本
系统:Ubuntu 24.04
安装 (所有节点)
安装 WireGuard
sudo apt-get update
sudo apt-get install -y wireguard生成密钥
mkdir -p $HOME/wireguard
cd $HOME/wireguard
wg genkey | tee private.key | wg pubkey > public.key创建配置文件 (在所有节点创建)
格式如下,写入到 /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>:51820e.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启动服务
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0重启网卡
sudo wg-quick down wg0
sudo wg-quick up wg0可选
设置 MTU
在 /etc/wireguard/wg0.conf 的 [Interface] 中,添加:
MTU = XXXXe.g.:
MTU = 1380