在Proxmox非特权容器中安装WireGuard VPN的完整指南

本文详细介绍了在基于Proxmox的虚拟化环境中,如何在非特权容器内安装和配置新兴VPN技术WireGuard。内容涵盖从主机内核模块安装到容器内用户空间工具配置的全过程,适合具备Linux系统管理经验的用户参考。

在Proxmox非特权容器中安装WireGuard

发布日期:2019年4月14日

WireGuard是VPN领域的新星——相比传统VPN技术它确实有诸多优势,但本文不会讨论这些,因为网上已有大量相关信息。这篇博文仅解释如何在非特权容器中设置WireGuard。在我的案例中,所有操作都在Proxmox服务器上完成。让我们开始吧:

在Proxmox主机上配置

首先需要在Proxmox主机上运行内核模块。由于Proxmox基于Debian,我们只需从未稳定版本中固定WireGuard包,这是Debian项目在这种情况下推荐的方式。

1
2
3
4
echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable-wireguard.list
printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
apt update
apt install wireguard pve-headers

如果遇到以下情况:

1
2
3
4
5
Loading new wireguard-0.0.20190406 DKMS files...
Building for 4.15.18-9-pve
Module build for kernel 4.15.18-9-pve was skipped since the
kernel headers for this kernel does not seem to be installed.
Setting up linux-headers-4.9.0-8-amd64 (4.9.144-3.1) ...

需要确保为当前内核安装了pve-headers。如果之后才安装,则需要调用:

1
dkms autoinstall

两种情况下我们都用以下命令测试:

1
modprobe wireguard

如果这能正常工作,我们在启动时自动加载模块,因为主机不知道容器后续需要这个模块。

1
echo "wireguard" >> /etc/modules-load.d/modules.conf

配置容器环境

现在创建我们的非特权容器(在我的案例中也是Debian 9),然后安装用户空间工具:

1
2
3
echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable-wireguard.list
printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
apt update

现在需要特别注意的是——我们只想要用户空间工具,不需要其他东西。

1
apt-get install --no-install-recommends wireguard-tools

可以通过临时创建wg0设备来进行简单测试,验证一切是否正常。

1
ip link add wg0 type wireguard

没有输出意味着一切正常。至此我们完成了所有配置,其余步骤与在容器外运行WireGuard相同——只需选择适合您的配置指南即可。

文章分类:HowTo, IT Security, Linux, Networking

comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计