A robust bash script to automate WireGuard user/peer creation with comprehensive validation, backup management, and safety features.
- β Automated user/peer creation with key generation
- β Flag-based parameter system for flexible configuration
- β Configuration validation before and after modifications
- β Automatic backup system with timestamped files
- β IP conflict detection to prevent address collisions
- β QR code generation for mobile clients
- β Rollback mechanism if configuration fails
- β Comprehensive dependency checking
- β Safe service reloading with validation
- β Organized file structure for backups and client configs
- wireguard-tools (
wg,wg-quick) - Root/sudo access (to modify
/etc/wireguard/files)
- curl (for automatic public IP detection)
- qrencode (for QR code generation)
- systemctl (for service management)
-
Clone the repository:
git clone https://github.com/DiegoSpinola/wireguard-gen.git cd wireguard-gen chmod +x wireguard-gen.sh -
Install dependencies:
# Debian/Ubuntu sudo apt-get install wireguard-tools qrencode curl # RHEL/CentOS/Fedora sudo yum install wireguard-tools qrencode curl # Arch Linux sudo pacman -S wireguard-tools qrencode curl
-
Check dependencies:
sudo ./wireguard-gen.sh -C
sudo ./wireguard-gen.sh -c CONFIG_NAME -u USER_NAME -i CLIENT_IP -o OUTPUT_DIR [OPTIONS]-c CONFIG_NAME- WireGuard config name (e.g., 'wg0' for/etc/wireguard/wg0.conf)-u USER_NAME- Name for the new user/peer-i CLIENT_IP- IP address to assign (e.g., '10.0.0.5/32')-o OUTPUT_DIR- Directory to store client config file
-e SERVER_ENDPOINT- Server's public endpoint (e.g., 'vpn.example.com:51820')-d DNS_SERVERS- Custom DNS servers (DNS disabled by default)-a ALLOWED_IPS- Allowed IPs for client (default: '0.0.0.0/0, ::/0')-k KEEPALIVE- PersistentKeepalive seconds (default: 25, use 0 to disable)-m MTU- MTU size for client interface (default: 1360)-b BACKUP_DIR- Custom backup directory (default: ~/wireguardbk/)-D- Enable DNS with default servers (1.1.1.1, 1.0.0.1)-q- Show QR code installation instructions if qrencode missing-C- Check dependencies and exit-h- Show help message
# Simple user creation with auto-detection
sudo ./wireguard-gen.sh -c wg0 -u john_doe -i 10.252.1.100/32 -o ~/wireguard-clients
# Check dependencies first
./wireguard-gen.sh -C# Custom server endpoint and DNS
sudo ./wireguard-gen.sh -c wg0 -u jane_doe -i 10.252.1.101/32 -o ~/wireguard-clients \
-e vpn.example.com:51820 -d "8.8.8.8, 8.8.4.4"
# Split tunnel configuration (no DNS)
sudo ./wireguard-gen.sh -c wg0 -u bob -i 10.252.1.102/32 -o ~/wireguard-clients \
-a "10.252.0.0/16, 192.168.1.0/24"
# Enable default DNS servers
sudo ./wireguard-gen.sh -c wg0 -u alice -i 10.252.1.103/32 -o ~/wireguard-clients -D
# Custom keepalive and backup location
sudo ./wireguard-gen.sh -c wg0 -u charlie -i 10.252.1.104/32 -o ~/wireguard-clients \
-k 60 -b /backup/wireguard
# Custom MTU for specific network requirements
sudo ./wireguard-gen.sh -c wg0 -u mobile -i 10.252.1.105/32 -o ~/wireguard-clients \
-m 1280The script creates an organized directory structure:
Backup Directory:
~/wireguardbk/ (or custom backup directory)
βββ server-configs/ # Timestamped server config backups
βββ wg0.conf.20240322_143022.backup
Output Directory (specified with -o):
~/wireguard-clients/ (or your specified output directory)
βββ john_doe.conf # Client config (mode 600)
βββ john_doe.png # QR code for mobile import
βββ jane_doe.conf
βββ jane_doe.png
- Path traversal prevention - Blocks malicious paths with '..' patterns
- Username sanitization - Only alphanumeric and safe punctuation (._-)
- IP address validation - Comprehensive IPv4 format and CIDR checking
- Reserved IP detection - Blocks loopback, multicast, and reserved ranges
- Server endpoint validation - Verifies hostname/IP and port format
- MTU validation - Enforces valid range (576-9000)
- Filename safety - Path length limits and overwrite warnings
- Pre-modification validation - Checks existing config before changes
- Post-modification validation - Verifies config after adding peer
- Automatic rollback - Restores backup if validation fails
- Safe service reload - Validates before attempting hot-reload
- Duplicate IP detection - Prevents assigning already-used IPs
- User existence check - Prevents duplicate usernames
- Subnet validation - Ensures IP fits within server's address space
- Secure permissions - Client configs created with mode 600
- Backup integrity - Timestamped backups prevent overwrites
- Safe failure - Cleans up client files if server config fails
The script uses sensible defaults:
- DNS: Disabled (no DNS servers added to client config)
- Keepalive: 25 seconds (good for most NAT situations)
- Allowed IPs: 0.0.0.0/0, ::/0 (full tunnel)
- MTU: 1360 (optimal for most networks)
- Backup location: ~/wireguardbk/
- Base64 padding: Automatically fixed for malformed keys
When qrencode is installed, the script automatically generates QR codes for easy mobile client setup:
- PNG format for universal compatibility
- Optimal size for mobile scanning
- Same filename as config file with .png extension
The script intelligently manages WireGuard service reloading:
- Hot-reload when possible using
wg syncconf - Validation before reload to prevent service disruption
- Graceful fallback with manual restart instructions
- Service status detection to avoid unnecessary operations
If configuration validation fails after adding a peer:
- Immediate rollback to previous working configuration
- Client config cleanup (removes generated files)
- Clear error reporting with actionable messages
- Backup preservation for manual recovery if needed
# Install wireguard-tools
sudo apt-get install wireguard-tools # Debian/Ubuntu
sudo yum install wireguard-tools # RHEL/CentOS# Manually specify server endpoint
sudo ./wireguard-gen.sh -c wg0 -u user -i 10.0.0.5/32 -o ~/clients -e your-server.com:51820# Choose a different IP address
sudo ./wireguard-gen.sh -c wg0 -u user -i 10.0.0.6/32 -o ~/clients# Check your server config syntax
wg-quick strip wg0
# Fix any syntax errors, then try againAlways start troubleshooting with a dependency check:
./wireguard-gen.sh -CIf you need to restore from backup:
# List available backups
ls -la ~/wireguardbk/server-configs/
# Restore a specific backup
sudo cp ~/wireguardbk/server-configs/wg0.conf.20240322_143022.backup /etc/wireguard/wg0.conf
sudo systemctl restart wg-quick@wg0Test configuration syntax without applying:
# Test server config
sudo wg-quick strip wg0
# Test client config
sudo wg-quick strip /path/to/client.confContributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Update documentation
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Always run with sudo for server configuration modifications
- Test in non-production environments first
- Keep backups of working configurations
- Verify client connectivity after peer creation
- Monitor log files for connection issues