170 字
1 分钟
WireGuard 搭建与配置

安装#

Terminal window
apt install wireguard

常用控制命令:

Terminal window
sudo wg-quick up wg0 # 启动
sudo wg-quick down wg0 # 停止
sudo wg show # 状态

开启端口转发#

编辑 /etc/sysctl.conf

net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
Terminal window
sudo sysctl -p

生成密钥对#

Terminal window
cd /etc/wireguard/
umask 077
# 服务端密钥
wg genkey | tee privatekey | wg pubkey > publickey
# 客户端密钥
wg genkey > client1.key
wg pubkey < client1.key > client1.key.pub

服务端配置#

/etc/wireguard/wg0.conf

[Interface]
Address = 10.0.0.1/24
SaveConfig = true
PrivateKey = <server-private-key>
ListenPort = 51820
[Peer]
PublicKey = <client-public-key>
AllowedIPs = 10.0.0.2/32,192.168.0.0/24

启动并设置开机自启:

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

客户端配置#

[Interface]
PrivateKey = <client-private-key>
Address = 10.0.0.2/24
DNS = 8.8.8.8
[Peer]
PublicKey = <server-public-key>
Endpoint = <server-ip>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

iPhone、Windows、Mac 等均可使用相同格式的配置文件导入。

WireGuard 搭建与配置
https://iiii.fun/posts/linux/wireguard-setup/
作者
慶靈
发布于
2025-10-31
许可协议
CC BY-NC-SA 4.0