3台节点示例

1.安装 WireGuard(3台都要)

sudo apt update
sudo apt install wireguard -y

2.生成密钥(每台执行)

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

3.节点配置

A(中心节点)配置

/etc/wireguard/wg0.conf

[Interface]
PrivateKey = A的私钥
Address = 10.0.0.1/24
ListenPort = 51820

# B
[Peer]
PublicKey = B的公钥
AllowedIPs = 10.0.0.2/32

# C
[Peer]
PublicKey = C的公钥
AllowedIPs = 10.0.0.3/32

A 必须开启转发

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

B 节点配置

/etc/wireguard/wg0.conf

[Interface]
PrivateKey = B的私钥
Address = 10.0.0.2/24

[Peer]
PublicKey = A的公钥
Endpoint = 1.1.1.1:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25

C 节点配置

/etc/wireguard/wg0.conf

[Interface]
PrivateKey = C的私钥
Address = 10.0.0.3/24

[Peer]
PublicKey = A的公钥
Endpoint = 1.1.1.1:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25

4.启动 WireGuard(每台执行)

sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0

5.WireGuard 常用命令

# 查看所有接口
sudo wg

# 查看指定接口
sudo wg show wg0

# 查看 IP
ip addr show wg0

# 查看路由
ip route

# 启动接口
sudo wg-quick up wg0

# 停止接口
sudo wg-quick down wg0

# 重启接口
sudo wg-quick down wg0 && sudo wg-quick up wg0

# 开机自启
sudo systemctl enable wg-quick@wg0

# 立即启动
sudo systemctl start wg-quick@wg0

# 查看状态
systemctl status wg-quick@wg0

# 关闭自启
sudo systemctl disable wg-quick@wg0

# 停止
sudo systemctl stop wg-quick@wg0