« MariaDB tunnel serveur distant » : différence entre les versions
Page créée avec « Un service systemd pour créer un tunnel SSH persistant vers MariaDB sur Ubuntu 24.04. Il inclut la reconnexion automatique et vérifie que le port local n’est pas déjà utilisé. ----- == 1️⃣ Créer le script de lancement du tunnel == # Crée un script dans ton home (ou <code>/usr/local/bin</code>) : <syntaxhighlight lang="bash">nano /home/geo/start-mariadb-tunnel.sh</syntaxhighlight> <ol start="2" style="list-style-type: decimal;"> <li>Contenu du s... » |
Aucun résumé des modifications |
||
| (9 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
Un '''tunnel SSH vers un serveur distant''' permet de '''se connecter à MariaDB de façon sécurisée''' sans exposer le port de la base sur Internet. | |||
* Le '''trafic SQL est chiffré''' par SSH, donc personne ne peut l’intercepter. | |||
* Le serveur MariaDB reste '''inaccessible depuis l’extérieur''', ce qui limite fortement les risques d’attaque. | |||
* On peut utiliser des outils locaux (phpMyAdmin, DBeaver, CLI) comme si MariaDB était '''en local''', même si elle est sur un serveur distant. | |||
* Couplé avec <code>autossh</code>, le tunnel devient '''persistant et fiable''', pratique pour accès permanent ou automatisé. | |||
En bref : '''sécurité + confidentialité + simplicité d’accès''' à une base distante. | |||
== 🐋 Solution avec Docker == | |||
* [https://github.com/marmits/phpmyadmin-docker Voir => github.com/marmits/phpmyadmin-docker] | |||
== 🐧 Solution native sur Ubuntu == | |||
Un service systemd pour créer un tunnel SSH persistant vers MariaDB sur Ubuntu 24.04. | Un service systemd pour créer un tunnel SSH persistant vers MariaDB sur Ubuntu 24.04. | ||
Il inclut la reconnexion automatique et vérifie que le port local n’est pas déjà utilisé. | Il inclut la reconnexion automatique et vérifie que le port local n’est pas déjà utilisé. | ||
| Ligne 5 : | Ligne 19 : | ||
----- | ----- | ||
== 1️⃣ Créer le script de lancement du tunnel == | === 1️⃣ Créer le script de lancement du tunnel === | ||
# Crée un script dans ton home (ou <code>/usr/local/bin</code>) : | # Crée un script dans ton home (ou <code>/usr/local/bin</code>) : | ||
<syntaxhighlight lang="bash">nano /home/ | <syntaxhighlight lang="bash">nano /home/john/start-mariadb-tunnel.sh</syntaxhighlight> | ||
<ol start="2" style="list-style-type: decimal;"> | <ol start="2" style="list-style-type: decimal;"> | ||
<li>Contenu du script :</li></ol> | <li>Contenu du script :</li></ol> | ||
| Ligne 20 : | Ligne 34 : | ||
SSH_USER=user | SSH_USER=user | ||
SSH_HOST=12.12.12.12 | SSH_HOST=12.12.12.12 | ||
SSH_KEY=/home/ | SSH_KEY=/home/john/.ssh/id_ed25519_test | ||
SSH_PORT=666 | SSH_PORT=666 | ||
| Ligne 34 : | Ligne 48 : | ||
<li>Rends le script exécutable :</li></ol> | <li>Rends le script exécutable :</li></ol> | ||
<syntaxhighlight lang="bash">chmod +x /home/ | <syntaxhighlight lang="bash">chmod +x /home/john/start-mariadb-tunnel.sh</syntaxhighlight> | ||
----- | ----- | ||
== 2️⃣ Créer le service systemd == | === 2️⃣ Créer le service systemd === | ||
# Crée le fichier de service : | # Crée le fichier de service : | ||
| Ligne 46 : | Ligne 60 : | ||
<li>Contenu du service :</li></ol> | <li>Contenu du service :</li></ol> | ||
<syntaxhighlight lang="ini">[Unit] | <syntaxhighlight lang="ini"> | ||
[Unit] | |||
Description=Tunnel SSH persistant vers MariaDB | Description=Tunnel SSH persistant vers MariaDB | ||
After=network.target | After=network.target | ||
Wants=network-online.target | Wants=network-online.target | ||
StartLimitIntervalSec=0 | |||
[Service] | [Service] | ||
Type=simple | Type=simple | ||
User=geo | User=geo | ||
ExecStart=/home/geo/start-mariadb-tunnel.sh | ExecStart=/home/geo/bash/start-mariadb-tunnel.sh | ||
Restart=always | Restart=always | ||
RestartSec=10 | RestartSec=10 | ||
ExecStartPre=/bin/sleep 10 | |||
[Install] | [Install] | ||
WantedBy=multi-user.target</syntaxhighlight> | WantedBy=multi-user.target | ||
</syntaxhighlight> | |||
* <code>Restart=always</code> → relance automatiquement si le tunnel tombe. | * <code>Restart=always</code> → relance automatiquement si le tunnel tombe. | ||
* <code>RestartSec=10</code> → délai avant de relancer. | * <code>RestartSec=10</code> → délai avant de relancer. | ||
* <code>User= | * <code>User=john</code> → exécute le tunnel avec ton utilisateur Ubuntu pour utiliser la clé privée. | ||
----- | ----- | ||
== 3️⃣ Activer et démarrer le service == | === 3️⃣ Activer et démarrer le service === | ||
<syntaxhighlight lang="bash">sudo systemctl daemon-reload | <syntaxhighlight lang="bash">sudo systemctl daemon-reload | ||
| Ligne 75 : | Ligne 94 : | ||
----- | ----- | ||
== 4️⃣ Vérification == | === 4️⃣ Vérification === | ||
* Statut du service : | * Statut du service : | ||
| Ligne 99 : | Ligne 118 : | ||
----- | ----- | ||
'''Exemple phpMyAdmin :''' | |||
<syntaxhighlight lang="php"> | |||
$i++; | |||
$cfg['Servers'][$i]['verbose'] = 'VPS via SSH'; | |||
$cfg['Servers'][$i]['host'] = '127.0.0.1'; // localhost du tunnel | |||
$cfg['Servers'][$i]['port'] = 33007; // port local du tunnel | |||
$cfg['Servers'][$i]['socket'] = ''; | |||
$cfg['Servers'][$i]['auth_type'] = 'cookie'; | |||
$cfg['Servers'][$i]['user'] = 'johnsql'; | |||
$cfg['Servers'][$i]['password'] = 'passjohnsql'; | |||
$cfg['Servers'][$i]['connect_type'] = 'tcp'; | |||
# Non obligatoire car déja protégé par tunnel | |||
$cfg['Servers'][$i]['ssl'] = true; | |||
$cfg['Servers'][$i]['ssl_ca'] = '/home/john/ca-cert_serverMaria.pem'; | |||
$cfg['Servers'][$i]['ssl_cert'] = '/home/john/client-cert_serverMaria.pem'; | |||
$cfg['Servers'][$i]['ssl_key'] = '/home/john/client-key_serverMaria.pem'; | |||
$cfg['Servers'][$i]['ssl_verify'] = false; // ou true si tu veux vérifier le certificat | |||
</syntaxhighlight> | |||
[[category:Sql]] [[Catégorie: Linux]] | [[category:Sql]] [[Catégorie: Docker]] [[Catégorie: Linux]] [[Catégorie: Debian]] | ||