Hello,
In a previous video, I demonstrated how to provide Wi-Fi configuration
for a headless Raspberry Pi using Imager program during the system installation.
For a functioning system, using this approach is not recommended,
considering how much time it takes and the fact that you would lose all your files in the process.
This video will show you how to change current Wi-Fi settings
and how to add a secondary connection for a Wi-Fi hotspot.
Also, I will show you how to fix Wi-Fi for a system you cannot connect to.
It is best to perform Wi-Fi configuration while connected using a wired connection,
but you can complete the work using Wi-Fi, as long as you are careful
not to break your current connection settings.
If you are a Windows PC user, you may not be aware that your PC has a hotspot feature.
I recommend configuring your Pi to use that hotspot as a secondary network.
Windows hotspot feature is pre-configured
and it takes only a few mouse clicks to find out the network name and password, and to get it started.
It is good to have a backup connection for your Pi,
especially that you are likely to have your laptop with you when you are away from your primary network.
Wi-Fi configuration on a Raspberry Pi OS system is stored in a settings file called wpa_supplicant.conf.
The file is located in a sub-directory of /etc/ system directory
and it may may be easiest to work with it if I go in there using cd command.
Here it is...
It is very important to make a backup of the configuration file before making any changes to it.
You can use the cp command to make the backup.
I suggest that you place the backup file on the /boot partition of the Pi's memory card.
Having the file there will make it accessible if you connect the card to a PC,
and I will show you later in the video how that can come in useful.
This command will copy current supplicant file to the /boot volume
under the name wpa_supplicant.conf.backup-1, but you can pick your own name.
Let's open the existing configuration file using nano text editor.
Prefixing the editor command with sudo keyword will run the editor with administrator level access,
allowing making changes to the file. The editor needs the file name as a parameter.
Nano is simple to use.
You can navigate the editor area using keyboard arrows and make changes to the document as needed.
Saving changes and exiting the editor is done my pressing Ctrl key combinations, shown on the bottom of the screen.
For example, to save the file, we press Ctrl+o and to exit the editor we press Ctrl+x.
My system has configuration for a Wi-Fi network which I use on occasion,
which I provided when the system was originally set up,
but currently I am using a wired Ethernet connection which does not require configuration.
It is likely that your system is currently using the default Wi-Fi connection, so be careful not to break it.
Mistakes here could make Wi-Fi inoperable, where you would be cut off from the system.
Although I will show you in this video how to fix Wi-Fi for an off-line system,
it's best to avoid that possibility altogether.
Basic Wi-Fi configuration has two settings:
SSID which is the network name and PSK which is the pre-shared key,
but most people refer to it as passphrase or password.
These settings can be changed here if you are in a hurry.
The important detail to note is that the pre-shared key, when changed using a text editor
must be enclosed in double-quotes, like SSID.
The original pre-shared is not in quotes, because it is encrypted.
The text editor will not encrypt your pre-shared key, and leaving it plain text is a security flaw.
Let me show you how to complete the configuration with encrypted passphrase.
We use wpa_passphrase command to produce the Wi-Fi configuration with encrypted passphrase.
This command requires a parameter - Wi-Fi network name (the SSID).
Any special characters in the SSID, including spaces, will break this command,
therefore I strongly recommend that you enclose the SSID in double quotes.
If you run the command as is, and provide provide the passphrase when prompted,
the new configuration would be shown in the terminal.
Copy and paste of the new configuration into a text editor is not an option in the terminal,
but we can structure this command to append this new configuration
to the wpa_supplicant file automatically, by adding the following to the command:
| sudo tee -a wpa_supplicant.conf
The vertical bar symbol is called a pipe and you should find it on you keyboard near the Enter key.
The pipe sends the wpa_passphrase command output to the tee command
which will append that output to the existing configuration.
It is extremely important that you type the command exactly as you see here,
including the spaces between the command pieces.
Press Enter to run this complex command only after you triple-checked what you typed.
Type your Wi-Fi password in and press Enter again to complete the operation.
The new configuration is shown in the terminal,
but most importantly, it is also added to the supplicant file.
Let's have a look at it in a text editor...
As you can see, a new "network" section has been added to the file.
I strongly suggest that you delete the comment that contains the new passphrase in plain text.
Also, delete the # reading passphrase from... line as it is not needed.
The Pi can connect to only one Wi-Fi network at a time.
With multiple networks configured, it will try to connect to them in the order listed,
but we can assign priority to them by adding the priority parameter to each network.
A network with a higher priority number will be connected to first.
Another parameter worth knowing about is scan_ssid.
Setting it to 1 for a network indicates that it is hidden.
Make sure to add that parameter to your network configuration if your network is hidden,
otherwise the Pi will not see a hidden network, even if otherwise configured for it correctly.
It may be a good idea to make a new backup of the updated configuration file at this point.
Now I have 2 backups - one with the original settings
and the new one with the dual network configuration.
The new Wi-Fi configuration will become active on next boot,
but you can also activate it without a reboot, using the following command:
sudo wpa_cli -i wlan0 reconfigure
Be careful when using this command if you are connected to your Pi using Wi-Fi at the moment,
as deleted or broken primary connection settings may make your Pi inaccessible.
I do not have that risk as I am connected to my Pi using a wired connection.
Assuming that the Wi-Fi network is available and configuration is correct,
your Pi should connect and obtain an IP within a few seconds.
You can verify whether the new Wi-Fi settings are working through using the ip address command.
My system indicates now two Internet connections:
a wired connection I have been using all along and now a Wi-Fi connection to my hotspot.
The connection is active if it shows a valid IP listed in the inet field.
All right!
So far I showed you how to modify headless Raspberry Pi's Wi-Fi settings
if you have a connection to it currently working.
What if you re-configured your Raspberry Pi for a new network
and it turns out that you cannot connect to it now?
Or, perhaps you are at a friend's place and would like to connect your Pi to their guest Wi-Fi...
No worries! You can re-configure headless Raspberry Pi's Wi-Fi connections with the Pi being off-line.
All you need is a PC with a simple text editor.
I am using Windows now, but this will work on a Mac or Linux PC as well.
Connect your Pi's memory card to a PC.
The boot volume will appear as a removable storage device.
If you made a backup of the wpa_supplicant.conf file as I suggested earlier,
you can modify the backup file using an editor such as Notepad.
If you do not have the backup, you can create the wpa_supplicant.conf file from scratch,
or use a template I provided in the video description.
Make the necessary changes, making sure to put quotes around ssid and plain-text password.
Save the new configuration file on the boot volume.
It is very important that you do not overwrite the backup, but create a new file,
making sure that the editor does not append a default name extension to the file such as .txt.
The configuration file must be named exactly wpa_supplicant.conf, all in lower-case.
It helps to know for sure if your system does not hide file name extensions.
Install the card back in the Raspberry Pi and start the Pi up.
The Pi will move the new configuration file to its system directories,
replacing the previous configuration, and assuming that you made the changes correctly,
connect to the new network if available, after you wait about 2 minutes.
If the new configuration is meant to be permanent, I suggest that you encrypt the passphrase later
by using the wpa_passphrase command which I demonstrated earlier in this video.
I hope that you found this video useful and informative.
If you did, let me know by giving it thumbs up. If you are not a subscriber yet, please subscribe!
Thanks for watching!