Skip to main content

CLI tool for managing WireGuard VPN interfaces and clients

Project description

WireGuard Configuration Generator

PyPI Version Python Versions CI License: MIT

A command-line tool for managing WireGuard VPN interfaces and clients. It helps you set up and maintain a WireGuard VPN server, generate client configurations, and render system configuration files for both systemd-networkd and wg-quick.

Motivation

Managing WireGuard configurations by hand gets tedious fast - generating key pairs, tracking IP assignments, keeping server and client configs in sync. This tool was born out of a need to manage a home network VPN where family members and devices come and go. Instead of editing config files and juggling keys manually, wg-gen keeps everything in a local SQLite database and generates correct, ready-to-use configurations with a single command.

Features

  • Create and manage WireGuard interfaces
  • Add and remove VPN clients
  • Generate configuration files for WireGuard clients
  • Export client configuration as QR codes
  • Generate systemd-networkd configuration
  • Generate wg-quick configuration
  • Manage client IP addressing automatically
  • Support for both IPv4 and IPv6
  • SQLite-based persistent storage

Installation

Prerequisites

  • Python 3.10+
  • uv package manager

Step 1: Install uv

pip install uv

On Ubuntu/Debian, where system-managed Python may block pip install, add --break-system-packages:

pip install uv --break-system-packages

Step 2: Install wg-gen

uv tool install wg-gen

This installs wg-gen into an isolated environment and makes the wg-gen command available globally.

Step 3: Verify

wg-gen --help

Upgrading

uv tool upgrade wg-gen

Install from source

uv tool install git+https://github.com/mosquito/wg-gen.git

Usage

Basic Commands

# List available commands
wg-gen --help

# Create a new WireGuard interface
wg-gen interface add wg0 --ipv4 10.0.0.1/24 --ipv6 fd00::1/64 --endpoint vpn.example.com:51820 --listen-port 51820

# List all interfaces
wg-gen interface list

# Add a new client to an interface
wg-gen client add wg0 laptop

# Generate client configuration with QR code
wg-gen client add wg0 phone --qr

# List all clients
wg-gen client list

# Remove a client
wg-gen client remove wg0 phone

# Generate systemd-networkd configuration by default to /etc/systemd/network
wg-gen render systemd

# If you want specific output directory
wg-gen render systemd --output ~/test/networkd

# Generate wg-quick configuration by default to /etc/wireguard
wg-gen render wgquick

# If you want specific output directory
wg-gen render wgquick --output ~/wg-quick

Configuration Options

Interface Configuration

When adding a new interface, the following options are available:

wg-gen interface add <interface_name> [OPTIONS]
Option Description Default
--ipv4 IPv4 interface for server with subnet (e.g., 10.0.0.1/24) None
--ipv6 IPv6 interface for server with subnet (e.g., fd00::1/64) None
--mtu MTU to use for the interface 1420
--listen-port Server listen port Random (1024-65000)
--endpoint Server endpoint host:port for clients Required
--dns DNS servers for clients 1.1.1.1, 8.8.8.8
--allowed-ips Allowed IPs for peers (non-local for all non-local nets) 0.0.0.0/0, 2000::/3
--persistent-keepalive Persistent keepalive seconds 15

Client Configuration

When adding a new client, the following options are available:

wg-gen client add <interface_name> <client_alias> [OPTIONS]
Option Description Default
--preshared-key Use a preshared key for additional security False
--force Overwrite existing client with the same alias on same interface False
--qr Display client configuration as a QR code False

How It Works

  1. The tool maintains a SQLite database of interfaces and clients
  2. When adding an interface, it generates WireGuard keys and stores the configuration
  3. When adding a client, it assigns the next available IP addresses from the interface's subnet
  4. Client configurations include private keys, server endpoint, and allowed IPs
  5. The render commands output configuration files for various init systems

Example Setup

Create a WireGuard Server

# Create interface with IPv4 and IPv6 subnets
wg-gen interface add wg0 \
  --ipv4 10.7.0.1/24 \
  --ipv6 fd00:7::1/64 \
  --endpoint vpn.example.com:51820 \
  --dns 1.1.1.1 9.9.9.9

# Generate systemd-networkd configuration
wg-gen render systemd

# Activate the interface
systemctl restart systemd-networkd

Add Clients

# Add a client named 'laptop'
wg-gen client add wg0 laptop

# Add a client 'phone' with QR code for mobile app
wg-gen client add wg0 phone --qr

Directory Structure

  • wg_gen/: Main package
    • cli/: Command-line interface modules
    • db.py: Database interface
    • keygen.py: Key generation utilities
    • table.py: Table formatting for output
    • __main__.py: Entry point

Database Location

By default, the SQLite database is stored at ~/.local/share/wg-gen/database.sqlite3. You can specify a different location with the --db-path option.

Configuration Files

The tool reads configuration from ~/.local/share/wg-gen/config.ini by default or from the path specified in the WG_GEN_CONFIG environment variable.

Example

[DEFAULT]
# Default configuration for wg-gen will be written to ~/.local/share/wg-gen/config.ini
# when database is created
db_path = ~/.local/share/wg-gen/database.sqlite3

log_level = info

# Default output format for list subcommands, can be 'table', 'json', 'csv', 'tsv'
output_format = table

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wg_gen-0.2.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wg_gen-0.2.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file wg_gen-0.2.2.tar.gz.

File metadata

  • Download URL: wg_gen-0.2.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wg_gen-0.2.2.tar.gz
Algorithm Hash digest
SHA256 616aa2e18e856253edae01f38fd955bfcf78ab310c01c4642256b6fa74ccfbe0
MD5 97734e31912f024e8aa490443f890db8
BLAKE2b-256 959bfac20e07dc30e6576858004759ee11c3e335816dd37266bc78909ac17359

See more details on using hashes here.

Provenance

The following attestation bundles were made for wg_gen-0.2.2.tar.gz:

Publisher: publish.yml on mosquito/wg-gen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wg_gen-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: wg_gen-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wg_gen-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 df38f30202be22bdcadc743eecd4461ce79c48407ab811181beb217217894230
MD5 11da2f5e346ef66756add41b01ee5900
BLAKE2b-256 c00cd5fcd2fef65b0f5cb9f2563589bf4795b8c6b5f03fcac2ede2b25ef2d636

See more details on using hashes here.

Provenance

The following attestation bundles were made for wg_gen-0.2.2-py3-none-any.whl:

Publisher: publish.yml on mosquito/wg-gen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page