VPN Setup Script Generator
Creates secure, automated VPN setup scripts for various protocols including OpenVPN, WireGuard, and IPSec with proper security configurations.
автор: VibeBaza
curl -fsSL https://vibebaza.com/i/vpn-setup-script | bash
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"
}