« PI Routeur » : différence entre les versions
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 | * <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 === | ||
| Ligne 23 : | Ligne 19 : | ||
== 1) Configuration réseau == | == 1) 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> | ||
== 2) | == 2) 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> | ||
== 3) Routage IPv4 et NAT == | |||
== 3) 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 55 : | ||
</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 67 : | ||
} | } | ||
} | } | ||
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 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 4) Tests == | == 4) 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 103 : | ||
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 214 : | Ligne 123 : | ||
sudo systemctl enable --now nftables | sudo systemctl enable --now nftables | ||
</syntaxhighlight> | </syntaxhighlight> | ||
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 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}'
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