如何在 Debian 13 上配置 WireGuard
简介
WireGuard 是一种快速、现代且在密码学上可靠的 VPN 协议,直接内置于 Linux 内核中。 与 OpenVPN 或 IPSec 等较重的方案相比,WireGuard 轻量、易于配置且性能出色, 因此非常适合用来保护经由您在 AVS ISP 的 VPS的流量。
Debian 13 “Trixie”(2025 年 8 月 9 日发布)在默认软件源中即包含对 WireGuard 的原生支持, 因此无需 backports,也不需要第三方源。
前置条件
开始之前,请确认您已具备:
- 一台来自 AVS ISP 的 Debian 13 (Trixie) VPS
- 服务器上的 root 权限或
sudo权限 - 一台作为 WireGuard 客户端的本机(Linux、Windows、macOS、Android、iOS)
- 在 AVS ISP VPS 的防火墙或安全组中开放 UDP 51820 端口
第 1 步 — 更新系统
通过 SSH 登录 VPS,并更新全部软件包:
apt update && apt upgrade -y
第 2 步 — 安装 WireGuard
WireGuard 位于 Debian 13 的默认软件源中:
apt install -y wireguard wireguard-tools iptables
验证安装:
wg --version
第 3 步 — 生成服务器密钥
先设置严格的 umask 以保护私钥文件,再生成服务器密钥对:
umask 077
wg genkey | tee /etc/wireguard/server.key | wg pubkey > /etc/wireguard/server.pub
查看并保存密钥:
cat /etc/wireguard/server.key # 服务器私钥(务必保密!)
cat /etc/wireguard/server.pub # 服务器公钥(分发给客户端)
第 4 步 — 生成客户端密钥
直接在服务器上为第一个客户端生成密钥对:
wg genkey | tee /etc/wireguard/client.key | wg pubkey > /etc/wireguard/client.pub
cat /etc/wireguard/client.key # 客户端私钥
cat /etc/wireguard/client.pub # 客户端公钥
第 5 步 — 确认网络接口
查看 VPS 主网络接口的名称(通常是 eth0 或 enp1s0):
ip address show
记下接口名称,下一步会用到。
第 6 步 — 配置 WireGuard 服务器
创建服务器配置文件:
nano /etc/wireguard/wg0.conf
粘贴以下内容,并替换其中的占位值:
[Interface]
# 服务器的 VPN IP 地址
Address = 10.8.0.1/24
# WireGuard 监听的 UDP 端口
ListenPort = 51820
# 在此粘贴服务器私钥
PrivateKey = <SERVER_PRIVATE_KEY>
# 隧道启动时开启 IP 转发和 NAT
# 请将 eth0 替换为您实际的网络接口(例如 enp1s0)
PostUp = echo 1 > /proc/sys/net/ipv4/ip_forward; \
iptables -A FORWARD -i wg0 -j ACCEPT; \
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = echo 0 > /proc/sys/net/ipv4/ip_forward; \
iptables -D FORWARD -i wg0 -j ACCEPT; \
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# ── 对端(客户端)─────────────────────────────────────────
[Peer]
# 在此粘贴客户端公钥
PublicKey = <CLIENT_PUBLIC_KEY>
# 分配给该客户端的 VPN IP
AllowedIPs = 10.8.0.2/32
保存并关闭(Ctrl+O、Enter、Ctrl+X)。
第 7 步 — 让 IP 转发持久生效
编辑 /etc/sysctl.conf,使 IP 转发在重启后依然有效:
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
第 8 步 — 启动 WireGuard 并设为开机自启
启动 WireGuard 服务并加入开机自启:
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0
确认接口已启动:
wg show
ip address show wg0
您应当看到 wg0 接口,地址为 10.8.0.1。
第 9 步 — 配置客户端
在您的客户端设备上创建 WireGuard 配置文件
(例如 wg0.conf,或导入 WireGuard 应用):
[Interface]
# 客户端的 VPN IP
Address = 10.8.0.2/32
# 客户端私钥(第 4 步生成)
PrivateKey = <CLIENT_PRIVATE_KEY>
# 可选:将 VPS 用作 DNS 解析器
# DNS = 10.8.0.1
[Peer]
# 服务器公钥(来自第 3 步)
PublicKey = <SERVER_PUBLIC_KEY>
# 您在 AVS ISP 的 VPS 公网 IP 和 WireGuard 端口
Endpoint = <YOUR_VPS_PUBLIC_IP>:51820
# 将全部流量经由 VPN 转发
# 如需分流(仅 VPN 子网),请改为:10.8.0.0/24
AllowedIPs = 0.0.0.0/0, ::/0
# 在 NAT 防火墙后保持连接存活
PersistentKeepalive = 25
在客户端启动隧道:
# Linux 客户端
wg-quick up wg0
在 Windows / macOS / Android / iOS 上,请将配置文件或二维码导入 官方 WireGuard 应用。
第 10 步 — 开放防火墙端口
如果您在 AVS ISP 的 VPS 上运行了本地的 ufw 或 iptables 防火墙,请放行 UDP 51820 端口:
# 使用 ufw
ufw allow 51820/udp
ufw reload
# 直接使用 iptables
iptables -A INPUT -p udp --dport 51820 -j ACCEPT
同时请在 AVS ISP 控制面板中确认该端口已在 VPS 的防火墙或安全组设置里开放。
第 11 步 — 验证连接
在客户端启动隧道并测试连通性:
wg-quick up wg0
ping 10.8.0.1 # ping VPN 服务器网关
curl https://ifconfig.me # 应返回您在 AVS ISP 的 VPS 公网 IP
在服务器上确认对端会话处于活动状态:
wg show
您应当看到该对端,并带有较新的 latest handshake 时间戳。
添加更多客户端
每新增一个客户端,重复第 4 步和第 9 步,然后在服务器的
/etc/wireguard/wg0.conf 中添加新的 [Peer] 段:
[Peer]
PublicKey = <NEW_CLIENT_PUBLIC_KEY>
AllowedIPs = 10.8.0.3/32
在不中断现有连接的情况下重载 WireGuard:
wg addconf wg0 <(wg-quick strip wg0)
# 或者重启服务
systemctl restart wg-quick@wg0
常用命令
| 操作 | 命令 |
|---|---|
| 查看隧道状态 | wg show |
| 启动隧道 | systemctl start wg-quick@wg0 |
| 停止隧道 | systemctl stop wg-quick@wg0 |
| 重启隧道 | systemctl restart wg-quick@wg0 |
| 查看 WireGuard 日志 | journalctl -u wg-quick@wg0 -f |
| 检查内核模块 | lsmod | grep wireguard |
安全建议
- 切勿分享您的私钥。 对端之间只交换公钥。
- 将 SSH 访问限制到特定 IP,或仅允许基于密钥的认证。
- 可以考虑把 WireGuard 的默认端口(51820)改为非标准 UDP 端口, 以减少端口扫描器带来的噪声。
- 长期运行的部署应定期轮换密钥对。
- 位于 NAT 之后的客户端请设置
PersistentKeepalive = 25,以免会话中断。
本指南面向使用 Debian 13 “Trixie” 的 AVS ISP VPS 客户。 WireGuard® 是 Jason A. Donenfeld 的注册商标。