WireGuard VPN 配置与管理
WireGuard 是基于 UDP 的现代 VPN,配置简洁、性能高。它用公钥识别节点,接口和 peer 配置在 /etc/wireguard/*.conf,常用 wg-quick 启停。
wg genkey | tee privatekey | wg pubkey > publickey
wg genpsk > preshared.key
wg show
wg show wg0
wg-quick up wg0
wg-quick down wg0
wg syncconf wg0 <(wg-quick strip wg0)
systemctl enable --now wg-quick@wg0
/etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <server-private-key>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = <client-public-key>
PresharedKey = <preshared-key>
AllowedIPs = 10.8.0.2/32
AllowedIPs 同时决定路由和允许的来源;写太宽会路由异常。PersistentKeepalive = 25 才能穿过 NAT。wg-quick 的 PostUp 规则失败会导致接口起不来;先手动验证。wg syncconf 可热加载;改接口地址等仍需重启。wg — WireGuard 管理ip — 地址与路由nft / iptables — 防火墙