Skill

Generate Secure VPN Setup Scripts

Skill for automated VPN setup scripts - OpenVPN and WireGuard server config, firewall rules, and client generation.

Maintainer of this project? Claim this page to edit the listing.


91
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Automate the creation of secure and cross-platform VPN setup scripts for OpenVPN and WireGuard. This asset handles server configuration, client generation, and essential security hardening.

Outcomes

What it gets done

01

Generate OpenVPN server configuration and PKI scripts.

02

Automate WireGuard server setup and client key generation.

03

Configure firewall rules and IP forwarding for VPN traffic.

04

Implement security best practices for VPN deployment.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-vpn-setup-script | bash

Overview

VPN Setup Script Generator

A skill for automated VPN setup scripts - OpenVPN and WireGuard server installation, PKI and key generation, client provisioning, firewall/NAT rules, and monitoring and backup scripts. Use it for scripted, server-side VPN deployment and administration, not manual GUI-based VPN client setup.

What it does

This skill writes secure, automated VPN setup scripts for OpenVPN, WireGuard, IPSec, and L2TP across Linux distributions, covering security-first configuration (AES-256-GCM or ChaCha20-Poly1305 ciphers, certificate-based authentication, perfect forward secrecy, firewall rules and IP forwarding, secure random key generation) and cross-platform compatibility (Ubuntu, CentOS, Debian, handling apt/yum/dnf package-manager differences and systemd versus init systems).

The OpenVPN server-installation script detects the OS, installs openvpn/easy-rsa, sets up a PKI via easyrsa init-pki, build-ca, gen-dh, and build-server-full, and generates a TLS-auth key with openvpn --genkey secret pki/ta.key. The server configuration sets cipher AES-256-GCM, auth SHA256, a 10.8.0.0/24 VPN subnet, and pushed DNS/redirect-gateway options. The WireGuard server setup installs the wireguard package, generates a keypair with wg genkey | tee privatekey | wg pubkey > publickey, detects the server's public IP and interface, and writes a wg0.conf with PostUp/PostDown iptables rules for NAT masquerading. A client-generation function creates a per-client keypair, appends a [Peer] block to the server config, and writes a client configuration file:

[Interface]
PrivateKey = $CLIENT_PRIVATE_KEY
Address = $CLIENT_IP/24
DNS = 8.8.8.8

[Peer]
PublicKey = $SERVER_PUBLIC_KEY
Endpoint = $SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

Firewall configuration enables IP forwarding and applies iptables rules accepting the VPN port and tunnel interface traffic, forwarding established connections, and masquerading outbound NAT traffic, saved via iptables-save or the iptables service. Certificate-management best practices call for unique per-client certificates, certificate revocation lists (CRLs), 4096-bit RSA minimum key sizes, regular rotation, and securely permissioned private keys. Performance tuning sets net.core.default_qdisc = fq and net.ipv4.tcp_congestion_control = bbr along with enlarged TCP buffer sizes. Additional operational scripts cover status monitoring (checking whether OpenVPN or WireGuard is active and reporting connected clients), automated timestamped backups of /etc/openvpn and /etc/wireguard as a tarball, and connection-testing (DNS resolution, internet connectivity via ping, and external-IP checks for leak detection).

When to use - and when NOT to

Use it when scripting automated OpenVPN or WireGuard server setup, client provisioning, firewall/NAT configuration, or VPN monitoring and backup. It is not a manual, GUI-based VPN client setup guide - it is scoped to scripted, server-side VPN deployment and administration.

Inputs and outputs

Given a target Linux server and a VPN protocol choice, it produces an OS-aware installation script, server and client configuration files, firewall/NAT rules, and monitoring, backup, and connection-testing scripts.

Integrations

Targets OpenVPN (with easy-rsa PKI) and WireGuard (wg/wg-quick) on Ubuntu, CentOS, and Debian, using iptables/sysctl for networking and systemd for service management.

Who it's for

System administrators and infrastructure engineers automating secure VPN server deployment on Linux.

Source README

VPN Setup Script Expert

You are an expert in creating secure, automated VPN setup scripts for various VPN protocols including OpenVPN, WireGuard, IPSec, and L2TP. You understand network security principles, certificate management, firewall configuration, and system administration across Linux distributions.

Core VPN Setup Principles

Security-First Configuration

  • Use strong encryption ciphers (AES-256-GCM, ChaCha20-Poly1305)
  • Implement proper certificate-based authentication
  • Configure secure key exchange protocols
  • Enable perfect forward secrecy
  • Implement proper firewall rules and IP forwarding
  • Use secure random number generation for keys

Cross-Platform Compatibility

  • Support major Linux distributions (Ubuntu, CentOS, Debian)
  • Handle package manager differences (apt, yum, dnf)
  • Account for systemd vs init systems
  • Provide client configuration files for multiple platforms

OpenVPN Setup Scripts

Server Installation Script

#!/bin/bash
### OpenVPN Server Setup Script

set -euo pipefail

### Detect OS and set variables
if [[ -f /etc/debian_version ]]; then
    OS="debian"
    apt-get update
    apt-get install -y openvpn easy-rsa iptables-persistent
elif [[ -f /etc/redhat-release ]]; then
    OS="centos"
    yum install -y epel-release
    yum install -y openvpn easy-rsa iptables-services
fi

### Setup PKI
make-cadir /etc/openvpn/easy-rsa
cd /etc/openvpn/easy-rsa

### Configure easy-rsa vars
cat > vars << EOF
set_var EASYRSA_REQ_COUNTRY    "US"
set_var EASYRSA_REQ_PROVINCE   "CA"
set_var EASYRSA_REQ_CITY       "San Francisco"
set_var EASYRSA_REQ_ORG        "VPN Server"
set_var EASYRSA_REQ_EMAIL      "admin@vpnserver.com"
set_var EASYRSA_REQ_OU         "IT Department"
set_var EASYRSA_KEY_SIZE       4096
set_var EASYRSA_ALGO           rsa
set_var EASYRSA_CA_EXPIRE      7300
set_var EASYRSA_CERT_EXPIRE    3650
EOF

source ./vars
./easyrsa init-pki
./easyrsa --batch build-ca nopass
./easyrsa gen-dh
./easyrsa build-server-full server nopass
openvpn --genkey secret pki/ta.key

### Copy certificates
cp pki/ca.crt pki/issued/server.crt pki/private/server.key pki/dh.pem pki/ta.key /etc/openvpn/

OpenVPN Server Configuration

### Generate server.conf
cat > /etc/openvpn/server.conf << 'EOF'
port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-auth ta.key 0
cipher AES-256-GCM
auth SHA256
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
EOF

WireGuard Setup Scripts

WireGuard Server Setup

#!/bin/bash
### WireGuard Server Setup

set -euo pipefail

### Install WireGuard
if command -v apt-get &> /dev/null; then
    apt-get update
    apt-get install -y wireguard
elif command -v yum &> /dev/null; then
    yum install -y elrepo-release epel-release
    yum install -y kmod-wireguard wireguard-tools
fi

### Generate server keys
cd /etc/wireguard
wg genkey | tee privatekey | wg pubkey > publickey
chmod 600 privatekey

### Get server IP and interface
SERVER_IP=$(ip route get 8.8.8.8 | awk '{print $7; exit}')
INTERFACE=$(ip route get 8.8.8.8 | awk '{print $5; exit}')
PRIVATE_KEY=$(cat privatekey)

### Create server configuration
cat > wg0.conf << EOF
[Interface]
PrivateKey = $PRIVATE_KEY
Address = 10.66.66.1/24
ListenPort = 51820
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o $INTERFACE -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o $INTERFACE -j MASQUERADE

EOF

### Enable IP forwarding
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

### Start WireGuard
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0

Client Generation Function

generate_wireguard_client() {
    local CLIENT_NAME=$1
    local CLIENT_IP=$2
    
    # Generate client keys
    wg genkey | tee ${CLIENT_NAME}-private.key | wg pubkey > ${CLIENT_NAME}-public.key
    
    CLIENT_PRIVATE_KEY=$(cat ${CLIENT_NAME}-private.key)
    CLIENT_PUBLIC_KEY=$(cat ${CLIENT_NAME}-public.key)
    SERVER_PUBLIC_KEY=$(cat /etc/wireguard/publickey)
    
    # Add client to server config
    cat >> /etc/wireguard/wg0.conf << EOF

[Peer]
PublicKey = $CLIENT_PUBLIC_KEY
AllowedIPs = $CLIENT_IP/32
EOF
    
    # Generate client config
    cat > ${CLIENT_NAME}.conf << EOF
[Interface]
PrivateKey = $CLIENT_PRIVATE_KEY
Address = $CLIENT_IP/24
DNS = 8.8.8.8

[Peer]
PublicKey = $SERVER_PUBLIC_KEY
Endpoint = $SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF
    
    # Restart WireGuard
    systemctl restart wg-quick@wg0
    
    echo "Client configuration saved as ${CLIENT_NAME}.conf"
}

Firewall and Network Configuration

Automated Firewall Setup

configure_firewall() {
    local VPN_PROTOCOL=$1
    local VPN_PORT=$2
    
    # Enable IP forwarding
    echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
    sysctl -p
    
    # Configure iptables
    iptables -A INPUT -p $VPN_PROTOCOL --dport $VPN_PORT -j ACCEPT
    iptables -A INPUT -i tun+ -j ACCEPT
    iptables -A FORWARD -i tun+ -j ACCEPT
    iptables -A FORWARD -i tun+ -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -A FORWARD -i eth0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
    iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    
    # Save iptables rules
    if command -v iptables-save &> /dev/null; then
        iptables-save > /etc/iptables/rules.v4
    elif command -v service &> /dev/null; then
        service iptables save
    fi
}

Best Practices and Security

Certificate Management

  • Generate unique certificates for each client
  • Implement certificate revocation lists (CRL)
  • Use strong key sizes (4096-bit RSA minimum)
  • Rotate certificates regularly
  • Store private keys securely with proper permissions

Performance Optimization

### Optimize network stack for VPN
cat >> /etc/sysctl.conf << EOF
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728
EOF

Monitoring and Logging

setup_monitoring() {
    # Create status check script
    cat > /usr/local/bin/vpn-status.sh << 'EOF'
#!/bin/bash
echo "=== VPN Connection Status ==="
if systemctl is-active --quiet openvpn@server; then
    echo "OpenVPN: Running"
    echo "Connected clients: $(cat /var/log/openvpn/openvpn-status.log | grep -c "^CLIENT_LIST")"
elif systemctl is-active --quiet wg-quick@wg0; then
    echo "WireGuard: Running"
    wg show wg0
else
    echo "VPN: Not running"
fi
EOF
    chmod +x /usr/local/bin/vpn-status.sh
}

Automated Backup

backup_vpn_config() {
    BACKUP_DIR="/root/vpn-backup-$(date +%Y%m%d)"
    mkdir -p $BACKUP_DIR
    
    # Backup configurations and certificates
    if [[ -d /etc/openvpn ]]; then
        cp -r /etc/openvpn $BACKUP_DIR/
    fi
    if [[ -d /etc/wireguard ]]; then
        cp -r /etc/wireguard $BACKUP_DIR/
    fi
    
    tar -czf "$BACKUP_DIR.tar.gz" $BACKUP_DIR
    rm -rf $BACKUP_DIR
    echo "Backup created: $BACKUP_DIR.tar.gz"
}

Troubleshooting and Validation

Connection Testing

test_vpn_connection() {
    echo "Testing VPN connectivity..."
    
    # Test DNS resolution
    if nslookup google.com > /dev/null 2>&1; then
        echo "✓ DNS resolution working"
    else
        echo "✗ DNS resolution failed"
    fi
    
    # Test internet connectivity
    if ping -c 3 8.8.8.8 > /dev/null 2>&1; then
        echo "✓ Internet connectivity working"
    else
        echo "✗ Internet connectivity failed"
    fi
    
    # Check for IP leaks
    EXTERNAL_IP=$(curl -s ipinfo.io/ip)
    echo "External IP: $EXTERNAL_IP"
}

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.