wg

The wg command is a tool provided by WireGuard for managing WireGuard interfaces and peers. It allows you to configure, view, and manage WireGuard settings directly from the command line.

Purpose

The wg command provides various subcommands and options to create, configure, and monitor WireGuard VPN interfaces and their peers. It allows administrators to perform tasks such as generating keys, setting configuration parameters, and displaying interface status.

Common wg Commands and Options

1. Key Management

  • Generate a private key:

    wg genkey

    Example:

    sudo wg genkey | tee privatekey
  • Generate a public key from a private key:

    wg pubkey < privatekey

    Example:

    sudo wg pubkey < privatekey > publickey

2. Interface Management

  • Bring up a WireGuard interface:

    sudo ip link add dev wg0 type wireguard
  • Assign an IP address to the interface:

    sudo ip address add dev wg0 10.0.0.1/24
  • Set the private key for the interface:

    sudo wg set wg0 private-key /path/to/privatekey
  • Set the listen port for the interface:

    sudo wg set wg0 listen-port 51820
  • Bring up the interface:

    sudo ip link set up dev wg0

3. Peer Management

  • Add a peer to the interface:

  • Set persistent keepalive interval:

4. Viewing Configuration and Status

  • View the current configuration of an interface:

  • View the full configuration in configuration file format:

  • View all WireGuard interfaces and their status:

Example Commands

Generating Key Pairs

Generate a private key and derive the public key:

Setting Up an Interface and Adding a Peer

  1. Create the interface:

  2. Assign an IP address:

  3. Set the private key:

  4. Set the listen port:

  5. Add a peer:

  6. Bring up the interface:

Viewing the Status of the Interface

To see the current status and configuration of the interface:

Configuration File Example

Here is an example configuration file /etc/wireguard/wg0.conf:

To bring up the interface using the configuration file:

To bring it down:

Conclusion

The wg command is a central tool for managing WireGuard VPN interfaces and peers. By leveraging its capabilities, administrators can efficiently set up, configure, and monitor secure VPN connections. The simplicity and performance of WireGuard, combined with the flexibility of the wg command, make it an excellent choice for modern VPN setups.

Last updated