« PI Routeur » : différence entre les versions
mAucun résumé des modifications |
mAucun résumé des modifications |
||
| (5 versions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 3 : | Ligne 3 : | ||
== Objectif == | == Objectif == | ||
* eth0 (WAN) en **client DHCP** – adresse et passerelle obtenues du réseau amont. | * <code>eth0</code> (WAN) en **client DHCP** – adresse et passerelle obtenues du réseau amont. | ||
* eth1 (USB‑Ethernet) en **IP statique | * <code>eth1</code> (USB‑Ethernet) en **IP statique <code>10.11.11.1/24</code>** – **passerelle** du LAN. | ||
* **Serveur DHCP** via | * **Serveur DHCP** via <code>dnsmasq</code> : plage <code>10.11.11.100–10.11.11.200</code>, passerelle <code>10.11.11.1</code>. | ||
* **Routage IPv4** activé et **NAT** (masquerade) avec | * **Routage IPv4** activé et **NAT** (masquerade) avec <code>nftables</code>. | ||
== Prérequis == | == Prérequis == | ||
* Raspberry Pi 2 | * Raspberry Pi 2 avec Raspberry Pi OS. | ||
* Un adaptateur USB‑Ethernet pour le LAN ( | * Un adaptateur USB‑Ethernet pour le LAN (nommé <code>eth1</code> ou <code>enx<MAC></code>). | ||
* Accès | * Accès <code>sudo</code>. | ||
=== Identifier les interfaces === | === Identifier les interfaces === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
ip -o link show | awk -F': ' '/eth|enx/{print $2}' | ip -o link show | awk -F': ' '/eth|enx/{print $2}' | ||
</syntaxhighlight>Moi j'ai<syntaxhighlight lang="text"> | |||
eth0 | |||
eth1 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Configuration réseau == | ||
=== Bookworm (NetworkManager) === | |||
=== | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo nmcli con add type ethernet ifname eth0 con-name wan ipv4.method auto ipv6.method ignore | |||
sudo nmcli con add type ethernet ifname eth0 con-name wan | 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 add type ethernet ifname eth1 con-name lan | |||
sudo nmcli con up wan | sudo nmcli con up wan | ||
sudo nmcli con up lan | sudo nmcli con up lan | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Anciennes versions (dhcpcd) === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo nano /etc/dhcpcd.conf | sudo nano /etc/dhcpcd.conf | ||
interface eth1 | interface eth1 | ||
static ip_address=10.11.11.1/24 | static ip_address=10.11.11.1/24 | ||
sudo systemctl restart dhcpcd | sudo systemctl restart dhcpcd | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == DHCP (dnsmasq) == | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo apt install -y dnsmasq | sudo apt install -y dnsmasq | ||
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF' | sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF' | ||
interface=eth1 | interface=eth1 | ||
bind-interfaces | bind-interfaces | ||
domain-needed | domain-needed | ||
bogus-priv | bogus-priv | ||
dhcp-range=10.11.11.100,10.11.11.200,255.255.255.0,24h | dhcp-range=10.11.11.100,10.11.11.200,255.255.255.0,24h | ||
dhcp-option=option:router,10.11.11.1 | dhcp-option=option:router,10.11.11.1 | ||
EOF | EOF | ||
sudo systemctl enable --now dnsmasq | sudo systemctl enable --now dnsmasq | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Routage IPv4 et NAT == | |||
= | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf | echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf | ||
| Ligne 100 : | Ligne 58 : | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === nftables === | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo tee /etc/nftables.conf >/dev/null <<'EOF' | sudo tee /etc/nftables.conf >/dev/null <<'EOF' | ||
flush ruleset | flush ruleset | ||
table inet filter { | table inet filter { | ||
chain input { type filter hook input | chain input { type filter hook input priority 0; policy accept; } | ||
chain output { type filter hook output | chain output { type filter hook output priority 0; policy accept; } | ||
chain forward { type filter hook forward priority 0; policy drop; | chain forward { type filter hook forward priority 0; policy drop; | ||
ct state established,related accept | ct state established,related accept | ||
| Ligne 118 : | Ligne 70 : | ||
} | } | ||
} | } | ||
table ip nat { | table ip nat { | ||
chain postrouting { type nat hook postrouting priority 100; | chain postrouting { type nat hook postrouting priority 100; | ||
| Ligne 126 : | Ligne 76 : | ||
} | } | ||
EOF | EOF | ||
sudo systemctl enable --now nftables | sudo systemctl enable --now nftables | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Tests == | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
ip addr show eth0 | ip addr show eth0 | ||
ip addr show eth1 | ip addr show eth1 | ||
journalctl -u dnsmasq | tail -n 50 | |||
journalctl -u dnsmasq | |||
ping 1.1.1.1 | ping 1.1.1.1 | ||
curl -s https://ifconfig.me | curl -s https://ifconfig.me | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | == Script complet == | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
# Routeur Pi 2 – WAN DHCP (eth0), LAN 10.11.11.0/24 (eth1), DHCP & NAT | # Routeur Pi 2 – WAN DHCP (eth0), LAN 10.11.11.0/24 (eth1), DHCP & NAT | ||
set -e | 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 add type ethernet ifname eth0 con-name wan | |||
sudo nmcli con add type ethernet ifname eth1 con-name lan | |||
sudo nmcli con up wan; sudo nmcli con up lan | sudo nmcli con up wan; sudo nmcli con up lan | ||
sudo apt install -y dnsmasq | sudo apt install -y dnsmasq | ||
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF' | sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF' | ||
| Ligne 189 : | Ligne 106 : | ||
EOF | EOF | ||
sudo systemctl enable --now dnsmasq | sudo systemctl enable --now dnsmasq | ||
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-ip-forward.conf | 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 sysctl -p /etc/sysctl.d/99-ip-forward.conf | ||
sudo tee /etc/nftables.conf >/dev/null <<'EOF' | sudo tee /etc/nftables.conf >/dev/null <<'EOF' | ||
flush ruleset | flush ruleset | ||
table inet filter { | table inet filter { | ||
chain input { type filter hook input | chain input { type filter hook input priority 0; policy accept; } | ||
chain output { type filter hook output | chain output { type filter hook output priority 0; policy accept; } | ||
chain forward { type filter hook forward priority 0; policy drop; | chain forward { type filter hook forward priority 0; policy drop; | ||
ct state established,related accept | ct state established,related accept | ||
| Ligne 215 : | Ligne 127 : | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | |||
=== Vérifier le | == 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 : | |||
<syntaxhighlight lang="bash"> | |||
ip -o link show | awk -F': ' '/eth|enx/{print $2}' | |||
</syntaxhighlight> | |||
# Ajouter un override systemd pour attendre le réseau : | |||
<syntaxhighlight lang="bash"> | |||
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 | |||
</syntaxhighlight> | |||
# Vérifier l’écoute DHCP : | |||
<syntaxhighlight lang="bash"> | |||
sudo ss -lunp | grep :67 | |||
</syntaxhighlight> | |||
== 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: <code>/etc/dnsmasq.d/lan.conf</code><syntaxhighlight lang="bash"> | |||
sudo vi /etc/dnsmasq.d/lan.conf | |||
</syntaxhighlight>Et ajouter/modifier la ligne :<syntaxhighlight lang="text"> | |||
dhcp-host=b8:27:ee:cc:f9:ac,10.11.11.10,database | |||
</syntaxhighlight>Le serveur "<code>database</code>" dont l'adresse MAC est <code>b8:27:ee:cc:f9:ac</code> se verra systématiquement affecté l'adresse <code>10.11.11.10</code>. | |||
Pour que le serveur prenne en compte le changement il faut relancer <code>dnsmasq</code><syntaxhighlight lang="bash"> | |||
sudo systemctl restart dnsmasq | |||
</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"> | <syntaxhighlight lang="bash"> | ||
sudo systemctl enable caddy | |||
sudo systemctl restart caddy | |||
</syntaxhighlight> | </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 == | == Références == | ||
* [https://caddyserver.com/docs/ Documentation officielle Caddy] | |||
Dernière version 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.