« PI Routeur » : différence entre les versions
mAucun résumé des modifications |
mAucun résumé des modifications |
||
| Ligne 173 : | Ligne 173 : | ||
sudo systemctl restart dnsmasq | sudo systemctl restart dnsmasq | ||
</syntaxhighlight>Et c'est tout. | </syntaxhighlight>Et c'est tout. | ||
= Installer et configurer Caddy sur Raspberry Pi = | |||
== Pré-requis == | |||
* Raspberry Pi (Pi 2 ou supérieur) | |||
* Raspberry Pi OS (Bookworm ou Bullseye) | |||
* Accès root ou sudo | |||
* Ports 80 et 443 ouverts si vous utilisez Let's Encrypt | |||
== Installation de Caddy == | |||
<syntaxhighlight lang="bash"> | |||
sudo apt update | |||
sudo apt install -y caddy | |||
</syntaxhighlight> | |||
== Vérifier l'installation == | |||
<syntaxhighlight lang="bash"> | |||
caddy version | |||
systemctl status caddy | |||
</syntaxhighlight> | |||
== Configuration de base == | |||
Le fichier de configuration principal est : <code>/etc/caddy/Caddyfile</code> | |||
Exemple minimal pour un reverse proxy : | |||
<syntaxhighlight lang="text"> | |||
example.com { | |||
reverse_proxy 10.11.11.50:8080 | |||
} | |||
</syntaxhighlight> | |||
== Reverse Proxy pour plusieurs domaines == | |||
Vous pouvez définir plusieurs blocs : | |||
<syntaxhighlight lang="text"> | |||
d1.mondomaine.com { | |||
reverse_proxy 10.11.11.50:8080 | |||
} | |||
d2.mondomaine.com { | |||
reverse_proxy 10.11.11.60:8080 | |||
} | |||
</syntaxhighlight> | |||
== HTTPS automatique == | |||
Si vos domaines pointent vers l'IP publique du Raspberry Pi et que les ports 80/443 sont accessibles, Caddy gère automatiquement Let's Encrypt. | |||
Pour un réseau interne, utilisez TLS interne : | |||
<syntaxhighlight lang="text"> | |||
monapp.local { | |||
reverse_proxy 10.11.11.70:8080 | |||
tls internal | |||
} | |||
</syntaxhighlight> | |||
== Activer et recharger la configuration == | |||
<syntaxhighlight lang="bash"> | |||
sudo systemctl enable caddy | |||
sudo systemctl restart caddy | |||
</syntaxhighlight> | |||
== Vérifier le statut et les logs == | |||
<syntaxhighlight lang="bash"> | |||
systemctl status caddy | |||
journalctl -u caddy --no-pager | tail -n 50 | |||
</syntaxhighlight> | |||
== Points clés == | |||
* Chaque bloc dans le Caddyfile correspond à un domaine ou sous-domaine. | |||
* HTTPS est automatique pour les domaines publics. | |||
* Pour le LAN, utilisez <code>tls internal</code>. | |||
* Les modifications du Caddyfile sont prises en compte après <code>systemctl restart caddy</code>. | |||
== Références == | |||
* [https://caddyserver.com/docs/ Documentation officielle Caddy] | |||
Version actuelle datée du 30 novembre 2025 à 19:22
Routeur Raspberry Pi 2 : WAN DHCP, LAN 10.11.11.0/24, DHCP & NAT
Objectif
eth0(WAN) en **client DHCP** – adresse et passerelle obtenues du réseau amont.eth1(USB‑Ethernet) en **IP statique10.11.11.1/24** – **passerelle** du LAN.- **Serveur DHCP** via
dnsmasq: plage10.11.11.100–10.11.11.200, passerelle10.11.11.1. - **Routage IPv4** activé et **NAT** (masquerade) avec
nftables.
Prérequis
- Raspberry Pi 2 avec Raspberry Pi OS.
- Un adaptateur USB‑Ethernet pour le LAN (nommé
eth1ouenx<MAC>). - Accès
sudo.
Identifier les interfaces
ip -o link show | awk -F': ' '/eth|enx/{print $2}'
Moi j'ai
eth0
eth1
Configuration réseau
Bookworm (NetworkManager)
sudo nmcli con add type ethernet ifname eth0 con-name wan ipv4.method auto ipv6.method ignore
sudo nmcli con add type ethernet ifname eth1 con-name lan ipv4.method manual ipv4.addresses 10.11.11.1/24 ipv4.never-default yes ipv6.method ignore
sudo nmcli con up wan
sudo nmcli con up lan
Anciennes versions (dhcpcd)
sudo nano /etc/dhcpcd.conf
interface eth1
static ip_address=10.11.11.1/24
sudo systemctl restart dhcpcd
DHCP (dnsmasq)
sudo apt install -y dnsmasq
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF'
interface=eth1
bind-interfaces
domain-needed
bogus-priv
dhcp-range=10.11.11.100,10.11.11.200,255.255.255.0,24h
dhcp-option=option:router,10.11.11.1
EOF
sudo systemctl enable --now dnsmasq
Routage IPv4 et NAT
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf
sudo sysctl -p /etc/sysctl.d/99-ip-forward.conf
nftables
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
flush ruleset
table inet filter {
chain input { type filter hook input priority 0; policy accept; }
chain output { type filter hook output priority 0; policy accept; }
chain forward { type filter hook forward priority 0; policy drop;
ct state established,related accept
iif "eth1" oif "eth0" accept
}
}
table ip nat {
chain postrouting { type nat hook postrouting priority 100;
oif "eth0" masquerade
}
}
EOF
sudo systemctl enable --now nftables
Tests
ip addr show eth0
ip addr show eth1
journalctl -u dnsmasq | tail -n 50
ping 1.1.1.1
curl -s https://ifconfig.me
Script complet
#!/bin/bash
# Routeur Pi 2 – WAN DHCP (eth0), LAN 10.11.11.0/24 (eth1), DHCP & NAT
set -e
sudo nmcli con add type ethernet ifname eth0 con-name wan ipv4.method auto ipv6.method ignore
sudo nmcli con add type ethernet ifname eth1 con-name lan ipv4.method manual ipv4.addresses 10.11.11.1/24 ipv4.never-default yes ipv6.method ignore
sudo nmcli con up wan; sudo nmcli con up lan
sudo apt install -y dnsmasq
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF'
interface=eth1
bind-interfaces
domain-needed
bogus-priv
dhcp-range=10.11.11.100,10.11.11.200,255.255.255.0,24h
dhcp-option=option:router,10.11.11.1
EOF
sudo systemctl enable --now dnsmasq
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf
sudo sysctl -p /etc/sysctl.d/99-ip-forward.conf
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
flush ruleset
table inet filter {
chain input { type filter hook input priority 0; policy accept; }
chain output { type filter hook output priority 0; policy accept; }
chain forward { type filter hook forward priority 0; policy drop;
ct state established,related accept
iif "eth1" oif "eth0" accept
}
}
table ip nat {
chain postrouting { type nat hook postrouting priority 100;
oif "eth0" masquerade
}
}
EOF
sudo systemctl enable --now nftables
Dépannage : dnsmasq « unknown interface eth1 »
Ce message apparaît si dnsmasq démarre avant que l’interface soit prête ou si le nom d’interface est incorrect.
Symptômes
- `systemctl status dnsmasq` → `unknown interface eth1`
- Service en échec au démarrage.
Causes
- Interface USB non encore disponible au moment du lancement.
- Nom d’interface incorrect (vérifier avec `ip link show`).
Solutions
- Vérifier le nom exact :
ip -o link show | awk -F': ' '/eth|enx/{print $2}'
- Ajouter un override systemd pour attendre le réseau :
sudo mkdir -p /etc/systemd/system/dnsmasq.service.d
echo -e "[Unit]\nAfter=network-online.target\nWants=network-online.target" | \
sudo tee /etc/systemd/system/dnsmasq.service.d/override.conf
sudo systemctl daemon-reload
sudo systemctl restart dnsmasq
- Vérifier l’écoute DHCP :
sudo ss -lunp | grep :67
Administration
Forcer des IP statiques
On peut rendre l'IP statique sur le client mais le plus propre est de laisser le client en DHCP mais reconnaitre l'adresse MAC du client et configurer le serveur DHCP pour lui donner TOUJOURS la même IP.
Il faut simplement éditer le fichier: /etc/dnsmasq.d/lan.conf
sudo vi /etc/dnsmasq.d/lan.conf
Et ajouter/modifier la ligne :
dhcp-host=b8:27:ee:cc:f9:ac,10.11.11.10,database
Le serveur "database" dont l'adresse MAC est b8:27:ee:cc:f9:ac se verra systématiquement affecté l'adresse 10.11.11.10.
Pour que le serveur prenne en compte le changement il faut relancer dnsmasq
sudo systemctl restart dnsmasq
Et c'est tout.
Installer et configurer Caddy sur Raspberry Pi
Pré-requis
- Raspberry Pi (Pi 2 ou supérieur)
- Raspberry Pi OS (Bookworm ou Bullseye)
- Accès root ou sudo
- Ports 80 et 443 ouverts si vous utilisez Let's Encrypt
Installation de Caddy
sudo apt update
sudo apt install -y caddy
Vérifier l'installation
caddy version
systemctl status caddy
Configuration de base
Le fichier de configuration principal est : /etc/caddy/Caddyfile
Exemple minimal pour un reverse proxy :
example.com {
reverse_proxy 10.11.11.50:8080
}
Reverse Proxy pour plusieurs domaines
Vous pouvez définir plusieurs blocs :
d1.mondomaine.com {
reverse_proxy 10.11.11.50:8080
}
d2.mondomaine.com {
reverse_proxy 10.11.11.60:8080
}
HTTPS automatique
Si vos domaines pointent vers l'IP publique du Raspberry Pi et que les ports 80/443 sont accessibles, Caddy gère automatiquement Let's Encrypt.
Pour un réseau interne, utilisez TLS interne :
monapp.local {
reverse_proxy 10.11.11.70:8080
tls internal
}
Activer et recharger la configuration
sudo systemctl enable caddy
sudo systemctl restart caddy
Vérifier le statut et les logs
systemctl status caddy
journalctl -u caddy --no-pager | tail -n 50
Points clés
- Chaque bloc dans le Caddyfile correspond à un domaine ou sous-domaine.
- HTTPS est automatique pour les domaines publics.
- Pour le LAN, utilisez
tls internal. - Les modifications du Caddyfile sont prises en compte après
systemctl restart caddy.