wireguard(5)wg-quick使用

wg-quickWireGuard 的一个便捷脚本,用于快速配置和管理 WireGuard VPN 连接。它简化了手动配置网络接口和 WireGuard 的过程

用法

wg-quick cmd help

➜  ~ 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 | down | save | strip ] [ CONFIG_FILE | INTERFACE ]

参数解释

  1. up

    • 功能: 启动指定的 WireGuard 接口并应用配置。
    • 用法: wg-quick up CONFIG_FILEwg-quick up INTERFACE
    • 说明:
      • CONFIG_FILE 是 WireGuard 配置文件(通常以 .conf 结尾,例如 wg0.conf)。
      • INTERFACE 是配置中定义的网络接口名称(例如 wg0)。
      • 执行后,WireGuard 会根据配置文件建立隧道并配置相关的网络设置(如 IP 地址、路由等)。
    • 示例:
      1
      wg-quick up /etc/wireguard/wg0.conf
      1
      wg-quick up wg0
  2. down

    • 功能: 关闭指定的 WireGuard 接口并移除相关配置。
    • 用法: wg-quick down CONFIG_FILEwg-quick down INTERFACE
    • 说明:
      • 停止 WireGuard 隧道并清理网络设置。
      • 如果接口不存在或未启动,可能不会有任何效果。
    • 示例:
      1
      wg-quick down wg0
  3. save

    • 功能: 保存当前运行的 WireGuard 配置到文件中。
    • 用法: wg-quick save CONFIG_FILEwg-quick save INTERFACE
    • 说明:
      • 将当前接口的运行时状态(包括动态生成的密钥或端点)保存到配置文件中。
      • 适用于需要持久化当前配置的场景。
    • 示例:
      1
      wg-quick save wg0
  4. strip

    • 功能: 输出一个简化的 WireGuard 配置,去除与 wg-quick 特有的扩展配置(如路由表修改)。
    • 用法: wg-quick strip CONFIG_FILEwg-quick strip INTERFACE
    • 说明:
      • 生成的配置只包含 WireGuard 核心参数,适用于直接使用 wg 命令而不是 wg-quick
    • 示例:
      1
      wg-quick strip wg0.conf
  5. CONFIG_FILE

    • 说明: 指定 WireGuard 的配置文件路径,通常位于 /etc/wireguard/ 目录下或者 /usr/local/etc/wireguard/ 目录下,文件格式为 .conf
    • 示例: /etc/wireguard/wg0.conf
  6. INTERFACE

    • 说明: 指定网络接口名称,通常与配置文件名一致(去掉 .conf 后缀)。
    • 示例: wg0

注意事项

  • 权限: 运行 wg/wg-quick 通常需要超级用户权限(sudo),因为它涉及网络接口和路由表的修改。
  • 配置文件格式: wg-quick 支持标准的 WireGuard 配置,同时扩展了一些功能(如 PreUpPostUpPreDownPostDown 脚本)。
  • 依赖: 确保系统已安装 WireGuard 工具(wireguard-tools)。

典型用法示例

  1. 启动 VPN:

    1
    sudo wg-quick up wg0

    这会根据 wg0.conf 文件启动 WireGuard 接口。

  2. 关闭 VPN:

    1
    sudo wg-quick down wg0
  3. 保存当前配置:

    1
    sudo wg-quick save wg0
  4. 生成简洁配置:

    1
    sudo wg-quick strip wg0.conf > wg0-stripped.conf