Ubuntu 配置 WireGuard 工具

保存时间:2026/4/2 19:13:25


Ubuntu 配置 WireGuard 工具
内容由豆包 AI 生成
ubuntu下有没有配置wireguard的工具
Ubuntu 下有多种配置 WireGuard 的工具,以下是具体介绍:
  • wg 和 wg - quick:WireGuard 自带的工具,包含在wireguard - tools软件包中。wg是底层命令行工具,可直接通过命令行设置 WireGuard 接口的各项参数,如生成密钥、配置监听端口、设置允许的 IP 等。wg - quick基于wg,使用更便捷,通过配置文件来管理 WireGuard 接口,常用于启动、停止和重启 WireGuard 服务。
  • systemd - networkd:Ubuntu 系统中用于管理网络配置的工具,可直接对 WireGuard 进行配置,无需依赖 WireGuard 的用户空间实用程序,通过编写相应的网络配置文件,能实现对 WireGuard 接口的参数设置和管理。
  • network - manager:Ubuntu 常用的网络管理工具,支持配置 WireGuard,可在图形界面中操作,对于不熟悉命令行的用户更友好,能方便地添加、编辑和删除 WireGuard 连接配置。
  • wireguard - ui:这是一个图形化界面工具,可从 GitHub 下载对应版本。安装后,能通过图形界面直观地配置 WireGuard,包括生成密钥、设置对等体、配置网络参数等,适合喜欢图形化操作的用户。
  • Docker:可通过 Docker 部署 WireGuard。先创建相关文件夹用于存放配置文件,然后在 Docker 中新建项目,上传 yaml 配置文件,设置好服务器域名或 IP、监听端口等参数,启动容器后即可生成客户端配置,还可支持扫码连接,方便快捷。
参考 7 篇资料
- wireguard - ui下载网址
WireGuard - UI 的下载网址是https://github.com/ngoduykhanh/wireguard-ui/releases。你可以在该网址找到不同版本的安装包,如截至 2025 年 5 月最新版本为 v0.6.2,下载链接为https://github.com/ngoduykhanh/wireguard-ui/releases/download/v0.6.2/wireguard-ui-v0.6.2-linux-amd64.tar.gz
参考 10 篇资料
- systemd - networkd如何直接管理wireguard
systemd - networkd 可通过编写配置文件来管理 WireGuard,具体步骤如下:
  1. 生成密钥对:使用wg genkey | tee wg - private.key | wg pubkey > wg - public.key命令生成私钥和公钥文件,然后修改文件权限,确保systemd - network用户可读取,如sudo chown root:systemd - network wg - private.key wg - public.key
  2. 创建网络设备配置文件:在/etc/systemd/network/目录下创建一个以.netdev为后缀的文件,如wg0.netdev,内容示例如下:
plaintext
[NetDev]
Name=wg0
Kind=wireguard
MTUBytes=1420

[WireGuard]
PrivateKeyFile=/path/to/wg - private.key
FirewallMark=4242
RouteTable=off
其中Name为接口名称,PrivateKeyFile指向私钥文件路径。
3. 创建网络配置文件:在同一目录下创建一个以.network为后缀的文件,如wg0.network,内容示例如下:
plaintext
[Match]
Name=wg0

[Network]
Address=1.2.3.4/32
DNS=1.1.1.1
RoutingPolicyRules=family both, table main, suppress - prefix - length 0, priority 10
RoutingPolicyRules=family both, invert - rule true, firewall - mark 4242, table 4000, priority 11
Routes=destination 0.0.0.0/0, table 4000, scope link
Routes=destination ::/0, table 4000, scope link

[WireGuardPeers]
PublicKey=<the remote peer's public key here>
Endpoint=wg.example.com:51820
AllowedIPs=0.0.0.0/0, ::/0
PersistentKeepalive=25
Address为本地 WireGuard 接口的 IP 地址,PublicKey为对端公钥,Endpoint为对端地址和端口,AllowedIPs指定允许通过隧道访问的 IP 范围。
4. 重载并启用配置:执行sudo systemctl reload systemd - networkd命令重载配置,然后使用sudo systemctl enable --now systemd - networkd命令启用systemd - networkd服务,WireGuard 接口将根据配置自动启动和管理。
参考 5 篇资料