« PI Routeur » : différence entre les versions

De knowledge
Aller à la navigation Aller à la recherche
mAucun résumé des modifications
Ligne 125 : Ligne 125 :
EOF
EOF
sudo systemctl enable --now nftables
sudo systemctl enable --now nftables
</syntaxhighlight>
== 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>
</syntaxhighlight>

Version du 29 novembre 2025 à 23:50

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}'

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

  1. Vérifier le nom exact :
ip -o link show | awk -F': ' '/eth|enx/{print $2}'
  1. 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
  1. Vérifier l’écoute DHCP :
sudo ss -lunp | grep :67