在 Debian 12 上配置 WireGuard

服务商我选择了 Hostwinds。Hostwinds 在国内知名度不算高,但在国外拥有稳固的用户群体。Hostwinds 是相对更可靠的,且支持支付宝付款。

选择 Package:

以下是 VPS 的具体配置:

Billing Cycle:1 Months ($4.99/mo)

Location:Seattle

Operating System:Debian 12

Package:CPU: 1 Core | RAM: 1GB | Disk: 30GB

该配置月流量为 1T,理论带宽在 50-200 Mbps。位于西雅图的服务器平均延迟在 140-230ms 之间。

选择完成后,在 Billing 页面 Creat New Agreement 支付宝签约 Hostwinds 即可免密付款,每月自动扣费。

等待实例创建。若 Unfound Instance Error,重建即可。

WireGuard:

实例创建稍后,会发送邮件通知,包含 IP、用户名、密码等,于是建立 SSH 连接:

C:Usersuser>ssh root@xxx.xxx.xxx
确认指纹
输入root密码

更新系统并检查内核:

WireGuard 需要较新的内核(确认版本高于 5.6,Debian 12 默认满足)。运行以下命令:

apt update && apt upgrade -y
reboot

服务端安装与配置 WireGuard:

第一步:安装 WireGuard 工具

apt install wireguard-tools resolvconf -y

第二步:生成服务器与客户端密钥对

cd /etc/wireguard
umask 077
wg genkey | tee server-private.key | wg pubkey > server-public.key
wg genkey | tee client-private.key | wg pubkey > client-public.key

第三步:创建服务器配置文件

使用 nano /etc/wireguard/wg0.conf 创建并编辑配置文件,内容如下:

[Interface]
# 服务器端配置
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <粘贴 server-private.key 的内容>
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# 启用IP转发(用于流量中转)
PostUp = sysctl -w net.ipv4.ip_forward=1

[Peer]
# 第一个客户端配置
PublicKey = <粘贴 client-public.key 的内容>
AllowedIPs = 10.0.0.2/32

第四步:创建客户端配置文件

在本地电脑创建 client.conf 文件,内容如下:

[Interface]
# 客户端配置
PrivateKey = <粘贴 client-private.key 的内容>
Address = 10.0.0.2/32
DNS = 8.8.8.8

[Peer]
PublicKey = <粘贴 server-public.key 的内容>
Endpoint = <你的服务器公网IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

第五步:配置系统与防火墙

  1. 启用IP转发(已在上面的 PostUp 设置,此为持久化):

    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    sysctl -p
  2. 配置防火墙(允许VPN 端口和 SSH):

    apt install ufw -y
    ufw allow 51820/udp
    ufw allow 22/tcp
    ufw --force enable

    下载 ufw 时,有时报错:

    Temporary failure resolving ‘deb.debian.org

    从报错信息来看,核心问题是 DNS 解析失败,导致系统无法连接 Debian 官方软件源下载 ufw 及依赖包。

    首先确认服务器能正常访问外网,且 DNS 服务可用:

    # 1. 测试网络连通性(ping公网IP,比如谷歌DNS)
    ping -c 3 8.8.8.8
    
    # 2. 测试DNS解析(直接解析deb.debian.org)
    nslookup deb.debian.org  # 若提示"can't resolve",则确认为DNS问题
    
    • ping 8.8.8.8 失败:检查服务器网络配置(网关、路由),或联系服务商确认网络权限。
    • ping 成功但 nslookup 失败:重点解决 DNS 配置问题(下一步)。

    Debian 系统的 DNS 配置默认存于 /etc/resolv.conf,需手动配置可靠 DNS 服务器:

    临时配置 DNS(立即生效,重启后失效):

    # 备份原有resolv.conf(可选)
    cp /etc/resolv.conf /etc/resolv.conf.bak
    
    # 写入可靠DNS(推荐组合:谷歌+Cloudflare+阿里,兼顾国内外)
    echo "nameserver 8.8.8.8" > /etc/resolv.conf
    echo "nameserver 1.1.1.1" >> /etc/resolv.conf
    echo "nameserver 223.5.5.5" >> /etc/resolv.conf
    
    # 测试DNS是否生效
    nslookup deb.debian.org  # 若返回IP,则解析正常

第六步:启动 WireGuard 服务

systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0
systemctl status wg-quick@wg0
# 检查状态为“active (running)”

客户端连接:

将准备好的 client.conf 文件导入到各设备的 WireGuard 官方客户端,连接。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注