Ressources informatiques

Ressources informatiques

Ressources informatiques

Configurer le Raspberry Pi en routeur WiFi

Objectif :

Configurer le Raspberry Pi en routeur WiFi

Activer le WiFi

x@rpiX:~ $ sudo raspi-config

Configurer l'adresse IP WiFI du Raspberry PI

Sauvegarder la configuration initiale

x@rpiX:~ $ sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.sav

Configurez l'adresse IP wlan0

Exemple : on souhaite que l'interface wlan0 ait pour adresse IP : 192.168.2.254. pour cela ajoutez les deux lignes ci-dessous au fichier /etc/dhcpcd.conf

interface wlan0
static ip_address=192.168.2.254/24

Configurer le point d'accès WiFi

Installer

x@rpiX:~ $ sudo apt install hostapd

Configurer

Modifier le fichier /etc/hostapd/hostapd.conf

Exemple de configuration :

x@rpiX:~ $ sudo cat /etc/hostapd/hostapd.conf
country_code=FR
interface=wlan0
ssid=myRPiAccessPoint
hw_mode=g
channel=7
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=xxxx
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Indiquer au processus hostapd où se trouve le fichier de configuration

Il faut pour cela modifier le fichier /etc/default/hostapd
Sauvegarde :

x@rpiX:~ $ sudo mv /etc/default/hostapd /etc/default/hostapd.origin

Il faut renseigner la directive DAEMON_CONF. Effectuez la modification du fichier /etc/default/hostapd à l'aide de l'éditeur de votre choix : nano, ... Résultat de la modification :

x@rpiX:~ $ sudo cat /etc/default/hostapd
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Redémarrer le service hostapd
x@rpiX:~ $ sudo systemctl unmask hostapd
x@rpiX:~ $ sudo systemctl enable hostapd
x@rpiX:~ $ sudo systemctl start hostapd

Valider l'installation

Vérifier l'état du service hostapd
x@rpiX:~ $ sudo systemctl status hostapd
● hostapd.service - Access point and authentication server for Wi-Fi and Ethernet
     Active: active (running) 
                rpiX systemd[1]: Starting Access point and authentication server for Wi-Fi and Ethernet...
                rpiX hostapd[1251]: Configuration file: /etc/hostapd/hostapd.conf
                rpiX hostapd[1251]: wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
                rpiX hostapd[1251]: Using interface wlan0 with hwaddr xx:xx:xx:xx:xx:xx and ssid "myRPiAccessPoint"
                rpiX hostapd[1251]: wlan0: interface state COUNTRY_UPDATE->ENABLED
                rpiX hostapd[1251]: wlan0: AP-ENABLED
                rpiX systemd[1]: Started Access point and authentication server for Wi-Fi and Ethernet.
Vérifiez la configuration de l'interface wlan0
x@rpiX:~ $ ip a | grep -A2 wlan0:
3: wlan0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.254/24 brd 192.168.2.255 scope global noprefixroute wlan0
x@rpiX:~ $ iwconfig wlan0
wlan0     IEEE 802.11  Mode:Master  Tx-Power=31 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
x@rpiX:~ $ iw wlan0 info
Interface wlan0
        ifindex 3
        wdev 0x1
        addr xx:xx:xx:xx:xx:xx
        ssid myRPiAccessPoint
        type AP
        wiphy 0
        channel 7 (2447 MHz), width: 20 MHz, center1: 2447 MHz
        txpower 31.00 dBm
x@rpiX:~ $ sudo iwlist wlan0 scan | grep Channel:
                    Channel:1
                    Channel:10
                    Channel:1
                    Channel:6

Configurer et installer un serveur DHCP

Installer

px@rpiX:~ $  sudo apt install dnsmasq

Configurer

Sauvegarder la configuration d'origine
x@rpiX:~ $ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.origin
Configurer le serveur DHCP

Exemple :

Modifiez le fichier /etc/dnsmasq.conf à l'aide de l'éditeur de votre choix : nano, ...

Résultat de la modification :

x@rpiX:~ $ sudo cat /etc/dnsmasq.conf
interface=wlan0
dhcp-range=192.168.2.1,192.168.2.4,255.255.255.0,24h
Redémarrer le service
x@rpiX:~ $ sudo systemctl restart dnsmasq
Visualiser l'état du service
x@rpiX:~ $ sudo systemctl status dnsmasq
● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
     Active: active (running) since 
                rpiX systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
                rpiX dnsmasq[1354]: started, version 2.85 cachesize 150
                rpiX dnsmasq[1354]: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset auth cryptohash DN>
                rpiX dnsmasq-dhcp[1354]: DHCP, IP range 192.168.2.1 -- 192.168.2.4, lease time 1d
                rpiX dnsmasq[1354]: reading /run/dnsmasq/resolv.conf
                rpiX dnsmasq[1354]: using nameserver 192.168.1.254#53
                rpiX dnsmasq[1354]: read /etc/hosts - 5 addresses
                rpiX systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.

Se connecter au réseau WiFi

Depuis Windows, le réseau apparaît

Configurer le Raspberry Pi en point d'accès WiFi

Connectez-vous au réseau WiFi, puis visualisez la configuration obtenu

C:\Users\x>ipconfig /all
Carte réseau sans fil Wi-Fi :

   Suffixe DNS propre à la connexion. . . :
   Description. . . . . . . . . . . . . . : MediaTek Wi-Fi 6 MT7921 Wireless LAN Card
   Adresse physique . . . . . . . . . . . : XX-XX-XX-XX-XX-XX
   DHCP activé. . . . . . . . . . . . . . : Oui
   Configuration automatique activée. . . : Oui
   Adresse IPv6 de liaison locale. . . . .: 
   Adresse IPv4. . . . . . . . . . . . . .: 192.168.2.4(préféré)
   Masque de sous-réseau. . . . . . . . . : 255.255.255.0
   Bail obtenu. . . . . . . . . . . . . . : lundi 3 .... .... 18:18:12
   Bail expirant. . . . . . . . . . . . . : mardi 4 .... .... 18:18:12
   Passerelle par défaut. . . . . . . . . : 192.168.2.254
   Serveur DHCP . . . . . . . . . . . . . : 192.168.2.254
   IAID DHCPv6 . . . . . . . . . . . : 
   DUID de client DHCPv6. . . . . . . . :  
   Serveurs DNS. . .  . . . . . . . . . . : 192.168.2.254
   NetBIOS sur Tcpip. . . . . . . . . . . : Activé
C:\Users\johnDoe>C:\Users\johnDoe>route print 0.0.0.0
IPv4 Table de routage
===========================================================================
Itinéraires actifs :
Destination réseau    Masque réseau  Adr. passerelle   Adr. interface Métrique
          0.0.0.0          0.0.0.0    192.168.2.254      192.168.2.4     60
===========================================================================

Essayez de vous connecter à l'aide d'une session ssh

C:\Users\X>ssh x@192.168.2.254
The authenticity of host '192.168.2.254 (192.168.2.254)' can't be established.
x@rpiX:~ $
x@rpiX:~ $ sudo journalctl -xe | grep -A4 DHCPDISCOVER
                rpiX dnsmasq-dhcp[1354]: DHCPDISCOVER(wlan0)
                rpiX dnsmasq-dhcp[1354]: DHCPOFFER(wlan0) 192.168.2.4
                rpiX dnsmasq-dhcp[1354]: DHCPREQUEST(wlan0) 192.168.2.4 
                rpiX dnsmasq-dhcp[1354]: DHCPACK(wlan0) 192.168.2.4 
                rpiX sshd[1530]: Accepted password for doe from 192.168.2.4 port 51106 ssh2

Activer le routage

Dans le fichier /etc/sysctl.conf, à l'aide de la commande sudo nano /etc/sysctl.conf. décommentez la ligne ci-dessous en supprimant le caractère # qui la précède

adminX@broker:~$ grep -e net.ipv4.ip_forward  /etc/sysctl.conf
net.ipv4.ip_forward=1

Rechargez la configuration à l'aide de la commande ci-dessous :

x@rpiX:~ $ sudo sysctl --system

La configuration effectuée ci-dessus doit apparaître

Mettre en place le NAT et les règles de sécurité

Activer le NAT

x@rpiX:~ $ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Autoriser wlan0 -> eth0

x@rpiX:~ $ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Autoriser eth0 -> wlan0 uniquement pour les connexions établies

x@rpiX:~ $ sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT 

Visualisez la configuration

x@rpiX:~ $ sudo iptables -S -v
-P INPUT ACCEPT -c 590 52807
-P FORWARD ACCEPT -c 0 0
-P OUTPUT ACCEPT -c 433 45456
-A FORWARD -i wlan0 -o eth0 -c 0 0 -j ACCEPT
-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -c 0 0 -j ACCEPT
x@rpiX:~ $ sudo iptables -S -v -t nat
-P PREROUTING ACCEPT -c 50 5063
-P INPUT ACCEPT -c 50 5063
-P OUTPUT ACCEPT -c 0 0
-P POSTROUTING ACCEPT -c 0 0
-A POSTROUTING -o eth0 -c 0 0 -j MASQUERADE

Sauvegarder la configuration

x@rpiX:~ $ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Visualisez la configuration

x@rpiX:~ $ cat /etc/iptables.ipv4.nat
# Generated by iptables-save v1.8.7 on Mon Jul  
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A FORWARD -i wlan0 -o eth0 -j ACCEPT
-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on 
# Generated by iptables-save v1.8.7
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT
# Completed on 

Recharger la configuration au démarrage du RPi

Ajouter la ligne ci-dessous, avant la ligne exit 0, dans le fichier /etc/rc.local, à l'aide de la commande sudo nano /etc/rc.local

x@rpiX:~ $ sudo iptables-restore  < /etc/iptables.ipv4.nat

Valider la configuration

x@rpiX:~ $ journalctl -xe
    rpiX hostapd[515]: wlan0: STA xx:xx:xx:xx:xx:xx RADIUS: starting accounting session  
    rpiX hostapd[515]: wlan0: STA xx:xx:xx:xx:xx:xx WPA: pairwise key handshake completed (RSN)
    rpiX dnsmasq-dhcp[540]: DHCPDISCOVER(wlan0) 
    rpiX dnsmasq-dhcp[540]: DHCPOFFER(wlan0) 192.168.2.1 
    rpiX dnsmasq-dhcp[540]: DHCPREQUEST(wlan0) 192.168.2.1 
    rpiX dnsmasq-dhcp[540]: DHCPACK(wlan0) 192.168.2.1