WireGuard — VPN rapide avec génération de config client

Installe WireGuard, génère les clés serveur et client, configure le routage IP et affiche la config client + QR code. Prêt à l'emploi en 2 minutes.

vpnwireguardréseausécurité
$ curl scripts.ysavary.fr/wireguard | bash

WireGuard — VPN

WireGuard est un VPN moderne, rapide et simple à configurer. Ce script installe le serveur et génère automatiquement une config client importable.

Variables optionnelles :

Ce que fait le script :

Après l'install :
Importer la config sur l'app WireGuard ou scanner le QR code.

bash
#!/bin/bash
set -euo pipefail

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info()  { echo -e "${GREEN}[INFO]${NC}  $*"; }
warn()  { echo -e "${YELLOW}[WARN]${NC}  $*"; }
error() { echo -e "${RED}[ERR]${NC}   $*"; exit 1; }

[[ $EUID -ne 0 ]] && error "Ce script doit être exécuté en root."

WG_PORT="${WG_PORT:-51820}"
WG_CLIENTS="${WG_CLIENTS:-1}"
WG_NET="${WG_NET:-10.0.0.0/24}"
WG_DNS="1.1.1.1, 9.9.9.9"
WG_DIR="/etc/wireguard"
CLIENT_DIR="$WG_DIR/clients"

# Détection de l'IP publique et de l'interface réseau
PUBLIC_IP=$(curl -fsSL --max-time 5 https://api.ipify.org || hostname -I | awk '{print $1}')
NET_IFACE=$(ip route | grep default | awk '{print $5}' | head -1)

[[ -z "$NET_IFACE" ]] && error "Impossible de détecter l'interface réseau principale."

# Calcul du réseau VPN
WG_BASE=$(echo "$WG_NET" | cut -d'/' -f1 | sed 's/\.[0-9]*$//')
WG_SERVER_IP="${WG_BASE}.1"

info "=== Étape 1/5 — Installation de WireGuard ==="
apt-get update -qq
apt-get install -y wireguard wireguard-tools iptables

# qrencode pour afficher le QR code
apt-get install -y qrencode 2>/dev/null || warn "qrencode non disponible, pas de QR code."

# ── Génération des clés serveur ───────────────────────────────────────────────
info "=== Étape 2/5 — Génération des clés serveur ==="
mkdir -p "$WG_DIR" "$CLIENT_DIR"
chmod 700 "$WG_DIR"

SERVER_PRIV=$(wg genkey)
SERVER_PUB=$(echo "$SERVER_PRIV" | wg pubkey)
SERVER_PSK=$(wg genpsk)

echo "$SERVER_PRIV" > "$WG_DIR/server.key"
echo "$SERVER_PUB"  > "$WG_DIR/server.pub"
chmod 600 "$WG_DIR/server.key"

# ── Génération des clés client(s) ────────────────────────────────────────────
info "=== Étape 3/5 — Génération de $WG_CLIENTS client(s) ==="

declare -a CLIENT_CONFIGS=()
PEERS_BLOCK=""

for i in $(seq 1 "$WG_CLIENTS"); do
    CLIENT_IP="${WG_BASE}.$((i + 1))/32"
    CLIENT_PRIV=$(wg genkey)
    CLIENT_PUB=$(echo "$CLIENT_PRIV" | wg pubkey)

    CLIENT_CONF="$CLIENT_DIR/client${i}.conf"

    cat > "$CLIENT_CONF" <<EOF
[Interface]
PrivateKey = ${CLIENT_PRIV}
Address    = ${WG_BASE}.$((i + 1))/24
DNS        = ${WG_DNS}

[Peer]
PublicKey    = ${SERVER_PUB}
PresharedKey = ${SERVER_PSK}
Endpoint     = ${PUBLIC_IP}:${WG_PORT}
AllowedIPs   = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
    chmod 600 "$CLIENT_CONF"
    CLIENT_CONFIGS+=("$CLIENT_CONF")

    PEERS_BLOCK+="
[Peer]
# Client $i
PublicKey    = ${CLIENT_PUB}
PresharedKey = ${SERVER_PSK}
AllowedIPs   = ${WG_BASE}.$((i + 1))/32
"
done

# ── Configuration serveur wg0 ─────────────────────────────────────────────────
info "=== Étape 4/5 — Configuration du serveur wg0 ==="
cat > "$WG_DIR/wg0.conf" <<EOF
[Interface]
PrivateKey = ${SERVER_PRIV}
Address    = ${WG_SERVER_IP}/24
ListenPort = ${WG_PORT}

# Forwarding et NAT
PostUp   = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ${NET_IFACE} -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ${NET_IFACE} -j MASQUERADE
${PEERS_BLOCK}
EOF
chmod 600 "$WG_DIR/wg0.conf"

# ── IP Forwarding ─────────────────────────────────────────────────────────────
info "=== Étape 5/5 — Activation IP Forwarding ==="
sysctl -w net.ipv4.ip_forward=1 > /dev/null
sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
grep -q 'net.ipv4.ip_forward' /etc/sysctl.conf \
    && sed -i 's/#*net.ipv4.ip_forward.*/net.ipv4.ip_forward=1/' /etc/sysctl.conf \
    || echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf

# ── Démarrage ─────────────────────────────────────────────────────────────────
systemctl enable --now wg-quick@wg0

sleep 2

echo ""
info "=== WireGuard opérationnel ==="
echo ""
wg show wg0 2>/dev/null || true
echo ""
echo "  Serveur  : ${PUBLIC_IP}:${WG_PORT}"
echo "  Réseau   : ${WG_NET}"
echo "  Config   : $WG_DIR/wg0.conf"
echo ""

# ── Affichage config client(s) + QR ──────────────────────────────────────────
for i in "${!CLIENT_CONFIGS[@]}"; do
    CONF="${CLIENT_CONFIGS[$i]}"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "  Config client $((i+1)) — $CONF"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    cat "$CONF"
    echo ""
    if command -v qrencode &>/dev/null; then
        info "QR Code client $((i+1)) :"
        qrencode -t ansiutf8 < "$CONF"
    fi
    echo ""
done

echo "  Récupérer le fichier client :"
echo "  scp root@${PUBLIC_IP}:${CLIENT_DIR}/client1.conf ./client1.conf"
echo ""
warn "Port ${WG_PORT}/UDP doit être ouvert sur le firewall !"
echo "  iptables -A INPUT -p udp --dport ${WG_PORT} -j ACCEPT && netfilter-persistent save"
echo ""