wireguard(4)wg和wg-quick区别

在 WireGuard 项目中,wgwg-quick 是两个核心的用户空间工具,用于管理和配置 WireGuard VPN。它们都属于 wireguard-tools 软件包,但功能和使用场景有所不同。


1. wg 介绍

wg 是 WireGuard 的基础命令行工具,用于直接管理 WireGuard 接口和对等方(peers)。它是一个低级工具,提供了对 WireGuard 内核模块的细粒度控制,适合需要手动管理或脚本化的用户

主要功能

  • 查看状态:显示当前 WireGuard 接口的状态,包括对等方的连接信息、传输数据量等。
  • 配置接口:设置或修改 WireGuard 接口的属性(如私钥、监听端口)。
  • 管理对等方:添加、删除或更新对等方的公钥、允许的 IP 地址(AllowedIPs)等。
  • 实时操作:支持动态调整配置,无需重启接口。

a230c5d0062d130e102880192133e099.png

常用命令

➜  ~ wg --help
Usage: wg <cmd> [<args>]

Available subcommands:
  show: Shows the current configuration and device information  ### 查看配置及设备信息
  showconf: Shows the current configuration of a given WireGuard interface, for use with `setconf'
  set: Change the current configuration, add peers, remove peers, or change peers
  setconf: Applies a configuration file to a WireGuard interface  ### 应用配置
  addconf: Appends a configuration file to a WireGuard interface
  syncconf: Synchronizes a configuration file to a WireGuard interface
  genkey: Generates a new private key and writes it to stdout  ### 生成私钥
  genpsk: Generates a new preshared key and writes it to stdout
  pubkey: Reads a private key from stdin and writes a public key to stdout  ### 根据私钥生成公钥
You may pass `--help' to any of these subcommands to view usage.
  • 1.查看所有接口状态:
    wg show
    wg

需要root权限

输出示例:

interface: wg0
  public key: <公钥>
  private key: (hidden)
  listening port: 51820

peer: <对等方公钥>
  endpoint: 192.168.1.100:51820
  allowed ips: 10.0.0.2/32
  latest handshake: 1 minute, 23 seconds ago
  transfer: 1.23 MiB received, 2.45 MiB sent
  • 2.添加对等方:
    wg set wg0 peer <对等方公钥> allowed-ips 10.0.0.2/32 endpoint 192.168.1.100:51820

  • 3.设置私钥和监听端口:
    wg set wg0 private-key ./privatekey listen-port 51820

特点

  • 轻量:仅提供核心功能,无额外封装。
  • 灵活:适合嵌入脚本或与其他工具集成。
  • 手动性:需要用户自行管理网络接口(例如通过 ip 命令创建和配置 wg0)。

2. wg-quick 介绍

wg-quick 是一个更高层次的脚本工具,基于 wg 构建,旨在简化 WireGuard 的配置和使用。它通过读取配置文件(通常以 .conf 结尾)自动完成接口创建、配置和网络设置,适合快速部署 VPN 的用户。

主要功能

  • 一键启动/停止:根据配置文件启动或关闭 WireGuard 接口。
  • 自动网络配置:处理 IP 地址分配、路由表设置等。
  • 集成管理:将 wg 的功能与网络配置(如 ip 命令)结合,减少手动操作。

常用命令

➜  ~ wg-quick 
Usage: wg-quick [ up | down | save | strip ] [ CONFIG_FILE | INTERFACE ]

  CONFIG_FILE is a configuration file, whose filename is the interface name
  followed by `.conf'. Otherwise, INTERFACE is an interface name, with
  configuration found at:
  /etc/wireguard/INTERFACE.conf /usr/local/etc/wireguard/INTERFACE.conf.
  It is to be readable by wg(8)'s `setconf' sub-command, with the exception
  of the following additions to the [Interface] section, which are handled
  by wg-quick:

  - Address: may be specified one or more times and contains one or more
    IP addresses (with an optional CIDR mask) to be set for the interface.
  - DNS: an optional DNS server to use while the device is up.
  - MTU: an optional MTU for the interface; if unspecified, auto-calculated.
  - Table: an optional routing table to which routes will be added; if
    unspecified or `auto', the default table is used. If `off', no routes
    are added. Besides `auto' and `off', only `main' is supported on
    this platform.
  - PreUp, PostUp, PreDown, PostDown: script snippets which will be executed
    by bash(1) at the corresponding phases of the link, most commonly used
    to configure DNS. The string `%i' is expanded to INTERFACE.
  - SaveConfig: if set to `true', the configuration is saved from the current
    state of the interface upon shutdown.

See wg-quick(8) for more info and examples.
  • 1.启动接口:
    wg-quick up wg0
    或直接指定配置文件:
    wg-quick up /etc/wireguard/wg0.conf

  • 2.停止接口:
    wg-quick down wg0

配置文件示例

配置文件通常位于 /etc/wireguard/wg0.conf

[Interface]
PrivateKey = <服务器私钥>
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey = <客户端公钥>
AllowedIPs = 10.0.0.2/32
Endpoint = 192.168.1.100:51820
  • wg-quick up wg0 会根据此文件创建 wg0 接口,设置 IP 地址,并配置路由。

特点

  • 便捷:自动化网络配置,适合初学者或快速部署。
  • 封装性:隐藏了底层细节(如 ip linkip addr 等命令)。
  • 依赖性:需要 Bash 和一些网络工具(如 ipresolvconf)。

3. wgwg-quick 的功能区别

特性 wg wg-quick
功能定位 低级工具,直接管理 WireGuard 接口和对等方 高级脚本,简化 VPN 配置和启动
配置方式 命令行参数或管道输入 基于 .conf 配置文件
网络设置 不处理(需配合 ip 等工具) 自动配置 IP、路由等
使用复杂度 较高,需手动操作 较低,一键式操作
灵活性 高,适合脚本和动态调整 中等,受限于配置文件
典型场景 调试、动态管理、嵌入式系统 快速部署、日常使用
依赖性 仅依赖 WireGuard 内核模块 依赖 Bash 和网络工具

举例说明

  • 使用 wg 的手动流程

    1. 创建接口:ip link add wg0 type wireguard
    2. 设置 IP:ip addr add 10.0.0.1/24 dev wg0
    3. 配置 WireGuard:wg set wg0 private-key ./privatekey listen-port 51820
    4. 启动接口:ip link set wg0 up
    5. 添加对等方:wg set wg0 peer <公钥> allowed-ips 10.0.0.2/32
  • 使用 wg-quick 的简化流程

    1. 编辑 wg0.conf 文件。
    2. 运行:wg-quick up wg0(自动完成上述所有步骤)。

4. 选择建议

  • **用 wg**:
    • 需要动态调整对等方或调试 WireGuard。
    • 在脚本中集成 WireGuard 功能。
    • 对网络配置有完全控制需求。
  • **用 wg-quick**:
    • 快速搭建 VPN,无需深入了解底层细节。
    • 使用静态配置文件的标准场景。
    • 希望减少手动操作。

总结

wg 是 WireGuard 的核心管理工具,提供了灵活性和控制力;wg-quick 则是其封装版本,专注于便捷性和自动化。两者相辅相成,满足了从高级用户到初学者的不同需求。