« PI Routeur » : différence entre les versions

De knowledge
Aller à la navigation Aller à la recherche
mAucun résumé des modifications
Ligne 4 : Ligne 4 :
== Objectif ==
== Objectif ==
* <code>eth0</code> (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 {{Mono|10.11.11.1/24}}** – **passerelle** du LAN.
* <code>eth1</code> (USB‑Ethernet) en **IP statique <code>10.11.11.1/24</code>** – **passerelle** du LAN.
* **Serveur DHCP** via {{Mono|dnsmasq}} : plage **{{Mono|10.11.11.100–10.11.11.200}}**, passerelle **{{Mono|10.11.11.1}}**.
* **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 {{Mono|nftables}}.
* **Routage IPv4** activé et **NAT** (masquerade) avec <code>nftables</code>.
 
; Contexte OS
Sur Raspberry Pi OS **Bookworm**, le réseau est géré par **NetworkManager** (nmcli/nmtui) plutôt que dhcpcd<ref>Raspberry Pi Forums – « Getting Started with Network Manager », décrit la bascule vers NetworkManager et la gestion des connexions via nmcli, Oct 14, 2023. https://forums.raspberrypi.com/viewtopic.php?t=357739</ref><ref>Electronics‑Lab – « Bookworm – the new version of Raspberry Pi OS » (Debian 12), indiquant NetworkManager par défaut, Oct 24, 2023. https://www.electronics-lab.com/bookworm-the-new-version-of-raspberry-pi-os/</ref>. 
Sur Debian/Raspberry, **nftables** est le framework firewall/NAT recommandé<ref>Debian Wiki – « nftables », statut par défaut depuis Debian 10, usage conseillé. https://wiki.debian.org/nftables</ref>.


== Prérequis ==
== Prérequis ==
* Raspberry Pi 2 (ou équivalent) avec Raspberry Pi OS.
* Raspberry Pi 2 avec Raspberry Pi OS.
* Un adaptateur USB‑Ethernet pour le LAN (peut être nommé {{Mono|eth1}} ou {{Mono|enx<MAC>}}).
* Un adaptateur USB‑Ethernet pour le LAN (nommé <code>eth1</code> ou <code>enx&lt;MAC&gt;</code>).
* Accès {{Mono|sudo}}.
* Accès <code>sudo</code>.


=== Identifier les interfaces ===
=== Identifier les interfaces ===
Ligne 23 : Ligne 19 :


== 1) Configuration réseau ==
== 1) Configuration réseau ==
 
=== Bookworm (NetworkManager) ===
=== 1.1 Bookworm (NetworkManager) ===
NetworkManager stocke des « connexions » persistantes dans {{Mono|/etc/NetworkManager/system-connections/*.nmconnection}} et se gère avec {{Mono|nmcli}}<ref>Raspberry Pi Forums – « Getting Started with Network Manager », nmcli/nmtui, chemins des .nmconnection. https://forums.raspberrypi.com/viewtopic.php?t=357739</ref><ref>Raspberry Pi Education AU – « Raspberry Pi Network Manager » (nmcli, nmtui, fichiers .nmconnection), 2024. https://education.raspberrypiaustralia.online/network-manager/raspberry-pi-network-manager</ref>.
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# WAN sur eth0 : DHCP client
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
  ipv4.method auto ipv6.method ignore
 
# LAN sur eth1 : IP statique 10.11.11.1/24 (sans route par défaut)
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
 
# Activer les connexions
sudo nmcli con up wan
sudo nmcli con up wan
sudo nmcli con up lan
sudo nmcli con up lan
</syntaxhighlight>
</syntaxhighlight>


=== 1.2 Anciennes versions (dhcpcd) ===
=== Anciennes versions (dhcpcd) ===
Si le système utilise {{Mono|dhcpcd}} (pré‑Bookworm), définir l’IP statique du LAN dans {{Mono|/etc/dhcpcd.conf}}<ref>Raspberry Pi Guide – « Create wireless access point » utilise dhcpcd pour IP statique avant Bookworm. https://raspberrypi-guide.github.io/networking/create-wireless-access-point</ref>.
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo nano /etc/dhcpcd.conf
sudo nano /etc/dhcpcd.conf
# Ajouter :
interface eth1
interface eth1
static ip_address=10.11.11.1/24
static ip_address=10.11.11.1/24
# Redémarrer :
sudo systemctl restart dhcpcd
sudo systemctl restart dhcpcd
</syntaxhighlight>
</syntaxhighlight>


== 2) Serveur DHCP (dnsmasq) ==
== 2) DHCP (dnsmasq) ==
Installer et configurer **dnsmasq** pour émettre des baux sur le LAN. Les options suivantes viennent de la {{Mono|man}} officielle et du fichier d’exemple upstream<ref>Man page officielle dnsmasq – options DHCP/DNS (domain-needed, bogus-priv, dhcp-range, dhcp-option). https://dnsmasq.org/docs/dnsmasq-man.html</ref><ref>dnsmasq.conf.example (GitHub, projet officiel), syntaxe et options illustrées. https://github.com/imp/dnsmasq/blob/master/dnsmasq.conf.example</ref>.
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo apt update
sudo apt install -y dnsmasq
sudo apt install -y dnsmasq
# Fichier dédié au LAN
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF'
sudo tee /etc/dnsmasq.d/lan.conf >/dev/null <<'EOF'
# Écoute sur l'interface LAN uniquement
interface=eth1
interface=eth1
bind-interfaces
bind-interfaces
# Bonnes pratiques DNS
domain-needed
domain-needed
bogus-priv
bogus-priv
# Plage DHCP, masque, bail
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
# Passerelle annoncée
dhcp-option=option:router,10.11.11.1
dhcp-option=option:router,10.11.11.1
# (Optionnel) DNS fourni aux clients :
# dhcp-option=option:dns-server,10.11.11.1
# Écouter le DNS local si besoin :
# listen-address=10.11.11.1
EOF
EOF
sudo systemctl enable --now dnsmasq
sudo systemctl enable --now dnsmasq
</syntaxhighlight>
</syntaxhighlight>


; Notes
== 3) Routage IPv4 et NAT ==
* {{Mono|bind-interfaces}} limite l’écoute à {{Mono|eth1}} (évite d’exposer le service côté WAN)<ref>Man dnsmasq – option bind-interfaces. https://dnsmasq.org/docs/dnsmasq-man.html</ref>.
* Liste des options DHCP lisibles : {{Mono|dhcp-option=option:router}}, {{Mono|option:dns-server}}, etc.<ref>Kuan‑Yi Li – « Human‑Readable DHCP Options for DNSMASQ », 2020. https://blog.abysm.org/2020/06/human-readable-dhcp-options-for-dnsmasq/</ref>.
* dnsmasq est recommandé pour de petits réseaux et figure dans de nombreux guides Raspberry<ref>Raspberry Pi Guide – AP avec dnsmasq/hostapd (DHCP). https://raspberrypi-guide.github.io/networking/create-wireless-access-point</ref>.
 
== 3) Routage IPv4 et NAT (nftables) ==
 
=== 3.1 Activer l'IP forwarding ===
Le noyau doit autoriser le passage des paquets entre interfaces<ref>IT‑Connect – « Configurer le NAT sous nftables » (mention de net.ipv4.ip_forward=1). https://www.it-connect.fr/chapitres/configurer-le-nat-sous-nftables/</ref><ref>Gentoo Wiki – « Nftables/Examples », rappel d’activer l’IP forwarding. https://wiki.gentoo.org/wiki/Nftables/Examples</ref>.
<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 55 :
</syntaxhighlight>
</syntaxhighlight>


=== 3.2 Règles nftables (filtrage + NAT) ===
=== nftables ===
**nftables** est le framework de firewall/NAT par défaut recommandé sous Debian ; le service charge {{Mono|/etc/nftables.conf}} au démarrage<ref>Debian Wiki – « nftables » (par défaut, recommandé). https://wiki.debian.org/nftables</ref><ref>Server‑World – « Debian 12 Bookworm : Nftables : Enable Service » – service systemd, fichier /etc/nftables.conf. https://www.server-world.info/en/note?os=Debian_12&p=nftables&f=1</ref>. 
Le **masquerade** (SNAT dynamique) convient aux IP WAN changeantes<ref>Red Hat Security Guide – « Configuring NAT using nftables » (masquerade vs SNAT). https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/security_guide/sec-configuring_nat_using_nftables</ref><ref>nftables wiki – « Performing NAT » (hooks, priorités, masquerade). https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_%28NAT%29</ref>.
 
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
#!/usr/sbin/nft -f
flush ruleset
flush ruleset
# Filtrage : autorise LAN->WAN + retours ; bloque le reste en forward
table inet filter {
table inet filter {
   chain input  { type filter hook input   priority 0; policy accept; }
   chain input  { type filter hook input priority 0; policy accept; }
   chain output  { type filter hook output priority 0; policy accept; }
   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 67 :
   }
   }
}
}
# NAT (masquerade sur la sortie WAN)
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 73 :
}
}
EOF
EOF
sudo systemctl enable --now nftables
sudo systemctl enable --now nftables
sudo nft list ruleset
</syntaxhighlight>
</syntaxhighlight>


== 4) Tests ==
== 4) Tests ==
=== 4.1 Interfaces ===
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
ip addr show eth0
ip addr show eth0
ip addr show eth1
ip addr show eth1
</syntaxhighlight>
journalctl -u dnsmasq | tail -n 50
* {{Mono|eth0}} doit être en **DHCP** (adresse fournie par le réseau amont).
* {{Mono|eth1}} doit être en **{{Mono|10.11.11.1/24}}**. 
*(Avec NetworkManager : {{Mono|nmcli con show}} pour voir les connexions actives<ref>Raspberry Pi Forums – nmcli, gestion des connexions. https://forums.raspberrypi.com/viewtopic.php?t=357739</ref>.)*
 
=== 4.2 Baux DHCP (dnsmasq) ===
<syntaxhighlight lang="bash">
journalctl -u dnsmasq --no-pager | tail -n 50
</syntaxhighlight>
Les clients du switch LAN doivent recevoir **{{Mono|10.11.11.100–200}}** et **passerelle {{Mono|10.11.11.1}}**<ref>Man dnsmasq – fonctionnement DHCP et options. https://dnsmasq.org/docs/dnsmasq-man.html</ref>.
 
=== 4.3 Routage + NAT ===
Depuis un client du LAN :
<syntaxhighlight lang="bash">
ping 1.1.1.1
ping 1.1.1.1
curl -s https://ifconfig.me
curl -s https://ifconfig.me
</syntaxhighlight>
</syntaxhighlight>
L’adresse publique observée doit être celle de {{Mono|eth0}} (NAT masquerade)<ref>Red Hat – NAT masquerading expliqué. https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/security_guide/sec-configuring_nat_using_nftables</ref>.


== 5) Dépannage & conseils ==
== Script complet ==
* **Nom d’interface USB** : si l’USB‑Ethernet n’est pas {{Mono|eth1}} (ex. {{Mono|enx<MAC>}}), remplacer ce nom partout (dnsmasq, nftables, nmcli).
* **Conflits DHCP** : un **seul** serveur DHCP sur le LAN (ne pas laisser un autre routeur servir la même plage)<ref>OPNsense docs – rôle de dnsmasq, remarques sur DHCP et topologies petites/moyennes. https://docs.opnsense.org/manual/dnsmasq.html</ref>.
* **Sécurité dnsmasq** : {{Mono|bind-interfaces}} évite d’exposer le service côté WAN<ref>Man dnsmasq – bind-interfaces. https://dnsmasq.org/docs/dnsmasq-man.html</ref>.
* **Persistance firewall** : {{Mono|systemctl enable nftables}} assure le chargement au boot<ref>Server‑World – comportement de nftables.service (charge /etc/nftables.conf au démarrage). https://www.server-world.info/en/note?os=Debian_12&p=nftables&f=1</ref>.
* **Iptables (héritage)** : possible, mais Debian recommande **nftables**<ref>Debian Wiki – nftables recommandé par défaut. https://wiki.debian.org/nftables</ref>.
* **Bookworm & fallback Link‑Local (LLA)** : le fallback DHCP→LLA via NM n’est pas activé par défaut dans Bookworm ; prévu dans Trixie (Debian suivant)<ref>GitHub raspberrypi/bookworm-feedback #418 – discussion LLA fallback NM, mai–juin 2025. https://github.com/raspberrypi/bookworm-feedback/issues/418</ref>.
 
== 6) Script d’automatisation (Bookworm) ==
<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
# 1) Réseau (NetworkManager)
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 \
  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 nmcli con up wan; sudo nmcli con up lan
# 2) DHCP (dnsmasq)
sudo apt update
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 103 :
EOF
EOF
sudo systemctl enable --now dnsmasq
sudo systemctl enable --now dnsmasq
# 3) Routage IPv4
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
# 4) NAT & firewall (nftables)
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
#!/usr/sbin/nft -f
flush ruleset
flush ruleset
table inet filter {
table inet filter {
   chain input  { type filter hook input   priority 0; policy accept; }
   chain input  { type filter hook input priority 0; policy accept; }
   chain output  { type filter hook output priority 0; policy accept; }
   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 214 : Ligne 123 :
sudo systemctl enable --now nftables
sudo systemctl enable --now nftables
</syntaxhighlight>
</syntaxhighlight>
== Annexes ==
=== Vérifier le modèle de Raspberry Pi ===
<syntaxhighlight lang="bash">
cat /proc/device-tree/model
</syntaxhighlight>
== Références ==
<references />

Version du 29 novembre 2025 à 23:16

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 statique 10.11.11.1/24** – **passerelle** du LAN.
  • **Serveur DHCP** via dnsmasq : plage 10.11.11.100–10.11.11.200, passerelle 10.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é eth1 ou enx<MAC>).
  • Accès sudo.

Identifier les interfaces

ip -o link show | awk -F': ' '/eth|enx/{print $2}'

1) 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

2) 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

3) 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

4) 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