返回

Debian12虚拟迷你Alpine部署Wireguard

介绍在 debian12 下如何使用 kvm 进行虚拟化 alpine3.21 并安装 wireguard
2025-06-03
1404 字 · 约 4 分钟阅读

在公网部署了 Wireguard 服务后,在本地部署一个 Wireguard 客户端用于方便其他同事在家回网访问公司的网络

但不建议直接在内网的物理主机安装/运行 VPN 服务,而推荐用容器、虚拟机或专用网关分离风险,Wireguard 会调整 iptables 和其他策略,会影响其他服务的稳定性

不采用 docker 运行主要因为:

  • 网络配置较主机略复杂(如 NAT、端口映射、host 网络)
  • 需注意 WireGuard 需要宿主机有内核支持,且容器需加 --cap-add=NET_ADMIN

假设所在的物理机是 proxmox 或者是其他已经完成初步虚拟机方案搭建,这里以 debian12 + kvm 为例

创建一个 4C/512M/1G 迷你 Alpine 虚拟机,执行命令如下:

virt-install \
    -n alpine3.21 \
    --os-type=Linux \
    --os-variant=alpinelinux3.17 \
    --ram=512 \
    --vcpus=4 \
    --disk path=/var/lib/libvirt/images/alpine3.21.img,bus=virtio,size=1 \
    --graphics vnc,listen=0.0.0.0 --noautoconsole \
    --cdrom /var/lib/libvirt/images/alpine-standard-3.21.3-x86_64.iso \
    --network network=host-bridge

以上命令请确保:

  • host-bridge 桥接网络已简历
  • /var/lib/libvirt/images/alpine-standard-3.21.3-x86_64.iso 系统镜像文件存在

初始化系统后,取消社区仓库的注释(删除 community 前面的 #):

# vi /etc/apk/repositories

http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.21/main
http://mirrors.tuna.tsinghua.edu.cn/alpine/v3.21/community
http://mirrors.ustc.edu.cn/alpine/v3.21/main
http://mirrors.ustc.edu.cn/alpine/v3.21/community

安装必要软件

apk update
apk add wireguard-tools vim ip6tables

安装完成后,添加 wireguard 客户端配置:

# vim /etc/wireguard/wg0.conf 

[Interface]
PrivateKey = 
Address = 10.8.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = 
PresharedKey = 
AllowedIPs = 10.8.0.1/32
PersistentKeepalive = 25
Endpoint =

启动 wireguard 测试

wg-quick up wg0

设置开机自启:

systemctl enable wg-quick@wg0

这样一个压缩出来不到 1G 大小的网关服务镜像就制作好了

留言

发表留言