Skip to content

How to connect raspberry pi to the internet

connect-raspberry-pi-internet

Vous pouvez connecter un Raspberry Pi au réseau local via Ethernet ou via WLAN. Pour utiliser un WLAN avec un Raspberry Pi, l’une des deux conditions suivantes doit être remplie :

  • Raspberry Pi avec adaptateur WLAN intégré.
  • Adaptateur WLAN externe pour USB si le Raspberry Pi n’a pas de WLAN intégré.

En général, les adaptateurs WLAN externes pour USB sont détectés automatiquement. Mais tous les adaptateurs WLAN ne sont pas également adaptés. Par exemple, le pilote pour chaque adaptateur WLAN n’est pas intégré dans le système d’exploitation du Raspberry Pi. Vous devrez peut-être l’installer ultérieurement ou même compiler le code source. En règle générale, il est préférable d’éviter les deux et d’utiliser un adaptateur WLAN qui est explicitement adapté au fonctionnement sur un Raspberry Pi et aux distributions Linux.

Vue d’ensemble : configuration du WLAN

Configuration du WLAN sur le bureau
Configuration du WLAN sur la ligne de commande avec “raspi-config”.
Configuration du WLAN en ligne de commande avec “wpa_supplicant”.
Configuration du WLAN avec “systemd
Configuration du WLAN dans le fichier “/etc/network/interfaces” (déprécié)
Vérification de la connexion WLAN en ligne de commande

Il existe différentes façons de configurer un WLAN dans le système d’exploitation Raspberry Pi. Le choix de l’une ou l’autre dépend de vos besoins et de vos préférences personnelles. Si vous exécutez le système d’exploitation Raspberry Pi avec le bureau, vous y configurerez généralement le WLAN également. C’est on ne peut plus simple.
Si vous utilisez “raspi-config” pour la configuration, alors il n’y a rien de mal à utiliser cet outil. En définitive, “wpa_supplicant” est toujours configuré dans toutes les variantes, et vous pouvez également modifier son fichier de configuration manuellement. Dans ce cas, vous devez vous familiariser avec la syntaxe afin d’éviter les erreurs.
Vous pouvez également configurer le WLAN avec “systemd” ou dans le fichier “/etc/network/interfaces”. Toutefois, cela exige que la configuration complète du réseau soit effectuée sur place. Cette dernière est une procédure dépassée qu’il convient d’éviter.

1-. Configuration du WLAN sur le bureau

La configuration du WLAN sur le bureau du Raspberry Pi OS est très simple. À cette fin, il y a un symbole d’onde radio dans le coin supérieur droit qui commence par un point. Si vous cliquez dessus avec le bouton gauche de la souris, un menu s’ouvre avec les WLAN qui sont actuellement à portée. Si vous sélectionnez l’une d’entre elles, il vous suffit de saisir le mot de passe. La connexion à ce WLAN est alors établie. Le mot de passe est également enregistré. Cela signifie que le Raspberry Pi se connectera automatiquement à ce WLAN la prochaine fois.

2-. Configuration du WLAN sur la ligne de commande avec “raspi-config”

La manière la plus simple de configurer la connexion à un WLAN sur la ligne de commande est d’utiliser “raspi-config”. Là, sous “Options réseau / Wi-Fi”, vous trouverez l’option pour entrer un WLAN avec le nom et le mot de passe auquel le Raspberry Pi se connecte.

Il faut mentionner que seuls les réseaux WLAN normaux peuvent être entrés avec cette authentification PSK. Vous ne pouvez pas l’utiliser pour établir une connexion à un WLAN d’entreprise.
Pour les environnements WLAN plus complexes, la configuration manuelle du “wpa_supplicant” est inévitable.

3-. Configuration du WLAN sur la ligne de commande avec “wpa_supplicant”

Configuring “wpa_supplicant” takes a look into the engine room of the WLAN configuration, so to speak. With a few exceptions, “wpa_supplicant” will always be responsible for the WLAN configuration. The “wpa_supplicant” software is responsible for detecting wireless networks and automatically establishing connections. To do this, the access data must be written to a configuration file. This file is also used by the network managers of the desktop interface.

Open and edit the configuration file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf 

The following lines should be located or added here.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=EN 

Next are the individual WLAN networks:

network={
ssid="WLAN-SSID
psk="WLAN-PASSWORD"
}

Then save and close: Ctrl + O, Back, Ctrl + X.

However, it is not necessary to open and edit the file. You can also create the entries automatically.

sudo wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf

The following error message appears: “-bash: /etc/wpa_supplicant/wpa_supplicant.conf: No permission”.

What is the problem? With “sudo”, the command is executed with root rights, but the configuration file cannot be described this way. You really have to be “root” for it to work.

sudo -i
wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf
quit

Let’s see what we have done:

sudo cat /etc/wpa_supplicant/wpa_supplicant.conf

This command added a new WLAN network with the correct syntax at the end of the configuration file. At the same time, it converted the WLAN password to a hash value so that the WLAN connection is established faster. If you wish, you can now remove the plaintext password from the file (optional).

You can now connect to the WLAN as a test.

sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

Alternatively:

sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &

The final “&” sends the process to the background.

As long as the file “/etc/network/interfaces” matches the original, the connection to the WLAN should be established automatically after a reboot.

4-. WLAN configuration with “systemd”

Note: before choosing this solution for WLAN configuration, you should pass the network configuration to systemd and do without any network manager. Otherwise, this solution does not make sense.

First, we create a WLAN configuration for “wpa_supplicant”. This variant requires the use of “root”, which we need to change first.

sudo -i

Then we introduce the WLAN configuration into an existing configuration file with a command.

wpa_passphrase "WLAN-NAME" "WLAN-PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf

Replace the placeholders for the WLAN name and WLAN password with your own values.

At this point, it is worth taking a look at this file and checking if the entry has been created.

cat /etc/wpa_supplicant/wpa_supplicant.conf

The file should look like this

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=FR

network={
ssid="WLAN-NAME"
#psk="WLAN-PASSWORD"
psk=WLAN-PASSWORD_CODIERT
}

Note that the WLAN password is written in plain text in the file. For security reasons, you should remove the line containing the plaintext password (optional).

Then we return to the usual user:

quit

Then we create a systemd unit. Note that this solution only makes sense if you don’t need any network manager.

sudo nano /lib/systemd/system/[email protected]

 


[Unit]
Description=WPA-Supplicant-Daemon (wlan0)
Requires=sys-subsystem-net-devices-wlan0.device
BindsTo=sys-subsystem-net-devices-wlan0.device
After=sys-subsystem-net-devices-wlan0.device
Before=target-network
Wants=target-network

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/sbin/wpa_supplicant -qq -c/etc/wpa_supplicant/wpa_supplicant.conf -Dnl80211 -iwlan0
Restart=over failure

[Install]
Alias=multi-user.target.wants/[email protected]

Save and close with Ctrl + O, Back, Ctrl + X.

Then we activate and start the new systemd unit.

sudo systemctl enable [email protected]
sudo systemctl start [email protected]

What exactly happens now depends on several factors. Assuming the configuration was done correctly, the connection to the WLAN should now be established. Depending on the WLAN adapter, we have a different reaction here. For example, the status light on the WLAN adapter will start to flash and become steady when the connection is successfully established.

In any case, you should check the WLAN connection:

sudo systemctl status [email protected]

The “Loaded:” line should say “enabled” at the end. A log entry further down should say “Started WPA-Supplicant-Daemon (wlan0)”. Then the connection to the WLAN has been successfully established.

5-. WLAN configuration in the “/etc/network/interfaces” file (deprecated)

To establish a connection to a WLAN, you need to change the network configuration. To do this, open the following file:

sudo nano /etc/network/interfaces

Enter the following lines:

# WLAN
allow-hotplug wlan0
iface wlan0 inet manual
wpa-ssid "WLAN-NAME
wpa-psk "WLAN-PASSWORD"

You still need to enter the WLAN name (SSID) and the WLAN password (pre-shared key, PSK). Both values must be enclosed in quotation marks (“”). This is not required, but recommended if the name and password contain characters that are evaluated differently in this file. For example, a space.

Then save and close: Ctrl + O, Return, Ctrl + X.

Then we restart the interface. Only then the network settings are applied and the connection to the WLAN is established.

sudo ifdown wlan0
sudo ifup wlan0

If the WLAN configuration in the network settings is correct, the Raspberry Pi has received an IP address from the DHCP server. You can recognize this by the “DHCPACK” indication. The IPv4 address is also displayed on the next line. If this is not the case, the WLAN name, the WLAN password or both are incorrect.

Use “ifconfig” to display the network interfaces again. If an IP address has been assigned to the “wlan0” interface, then the Raspberry Pi is connected to the WLAN. 6.

6-. Check the WLAN connection on the command line

Check if the WLAN interface is present:

ip l

Check if there is a connection to a WLAN:

wpa_cli status

If there is a known access point in range, connection information is displayed here.
Solution: Check the network configuration

Check if the network configuration has been done as follows.

ip a

This shows whether the interface “wlan0” has been assigned an IP address.

Troubleshooting

First of all, we recommend that you update the system, and then install a tool that facilitates the configuration of wireless LANs.

sudo apt-get update
sudo apt-get install iw

Then plug the WLAN adapter into a free USB port. Next, you need to check if the WLAN adapter has been recognized. To do this, display all USB devices.

lsusb

If it is “wireless” or “802.11”, you have almost won. Now there should also be a network interface with the designation “wlan0” or “wlan1”. To do this, display all network interfaces.

ip l

If a network interface with the designation “wlan0” or “wlan1” is listed, everything went well with the hardware detection and driver installation. Now you can start configuring the WLAN client and the IP settings.

Next, we check if the WLAN we want to connect the Raspberry Pi to is in range.

iwlist wlan0 scan | grep -i ssid

The output shows all the WLANs that are in range. The one that the Raspberry Pi needs to be connected to must also be in the list. The prerequisite is that the Raspberry Pi is within its range.

Problem: Connection interruptions with SSH connection via the WLAN adapter

Normally, SSH connections are extremely stable. If no timeout is set on the client or server side, the connection theoretically stays open indefinitely.
However, connections can always be interrupted. For example, with connections via WLAN. Typically, two problems are responsible here. Either the power-saving function of the WLAN adapter, or sloppy development or cheap components. Especially cheap, so-called “Chinese” WLAN adapters can cause problems. The only remedy is to replace it with a better WLAN adapter.

Problem: No automatic reconnection

If you have configured the WLAN manually and the access point is switched off once, the WLAN connection is interrupted. This makes sense. However, when the access point is turned back on, the Raspberry Pi does not automatically connect to the WLAN.

Extension: Disable the power saving mode of the WLAN adapter

Some WLAN adapters have the “Power Save” option enabled by default. This is a “sleep mode” or rather a power saving function. It means that the adapter shuts down the network connection when it is inactive. This is not a problem if you use the Raspberry Pi as a client. Then the WLAN connection is simply reactivated. But if you want to access the Raspberry Pi remotely via WLAN, for example via SSH, it may not be accessible.