It's May, 2024, and the question "How to setup multiple WiFi networks?" now has two answers, depending upon your OS version (or which network manager you're using). At least one of them is straightforward (you can decide which :) ...
- one for
bullseye (using dhcpcd as the network manager)
- one for
bookworm (using NetworkManager as the network manager)
:: for bullseye (using dhcpcd as the network mgr)
If you use multiple WiFi networks in different locations, all that is needed is to configure each of these networks in the file /etc/wpa_supplicant/wpa_supplicant.conf - configure as many as you need!
The wpa_supplicant.conf file proposed in the currently selected answer is still correct, though the id_str is optional:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="SCHOOLS NETWORK NAME"
psk="SCHOOLS PASSWORD"
}
network={
ssid="HOME NETWORK NAME"
psk="HOME PASSWORD"
}
Unless you are "Home Schooled", which is to say that two or more networks are available in a given location, it's not necessary to create a priority for the networks :) By default, all networks listed in wpa_supplicant.conf have equal priority, and when priority is equal
wpa_supplicant will choose the network with the greatest signal strength (all other things being equal). This will come as a disappointment to the knob turners, but beyond properly defining the networks in wpa_supplicant.conf, there is really nothing else to be done in the situation defined by the OP.
If you do have two or more networks in a given location, then a priority assignment to your "favored" network may be appropriate. Another exception may be that you have a need to switch between networks. If you need to dynamically switch between networks, - there's another answer for that. Otherwise, leave it alone.
:: for bookworm (using NetworkManager as the network mgr)
NetworkManager (the default network mgr. in bookworm is a bit more complicated than using a wpa_supplicant.conf file:
Multiple WiFi networks in different locations are configured using either nmcli (or nmtui if you prefer the ncurses semi-gui interface). nmcli and nmtui are two tools for configuring networks under NetworkManager - which also includes a full GUI interface if you use X or Wayland on your RPi. For this answer we'll stick with nmcli as it's a more general tool - it has all the "features", and is applicable whether you're running X/Wayland or the Lite/headless version of RPi OS.
Adding a new/different network under NetworkManager is characterized as creating a connection. A connection is simply a file container for all the information needed to connect to a network! From the CLI, a new connection named 'NewWiFi' may be added as follows:
sudo nmcli connection add con-name "NewWiFi" type wifi ifname wlan0 ssid "SSID_NAME"
Where "SSID_NAME" is the name of the SSID used on "NewWiFi".
Connecting to "NewWiFi" is accomplished with a 2nd command:
sudo nmcli dev wifi connect 'NewWiFi' password 'mypasswd'
Of course nmcli (and in general NetworkManager) have many, many more options and features. There's a different syntax for adding a fixed IP address, but I'm not going to cover that here. And ordinarily I'd recommend reading the man page (man nmcli), but I'm not going to do that here... it's far too arcane IMHO! There are numerous blog posts that offer read-able advice; here's the best one that I've found so far.
be skeptical !
Readers should carefully consider adopting networking advice in a piecemeal fashion; there are numerous fallacies and foolishness embedded in some of the answers and comments here. Some that I noticed:
And there are others, but impossible to list them all here. Be diligent - and that goes for this answer also!
NOTE: This answer works for systems through bullseye, and bookworm. In bookworm the RPi Organization replaced dhcpcd with NetworkManager, and thus the answer to the OP's question has changed considerably. NetworkManager seems (to me) to be more than just a little overkill for a small system such as Raspberry Pi. The OP's question (which contains at least one significant error) illustrates this opinion capably. Hopefully, most readers can see that nmcli complicates the answer considerably - if not, try reading man nmcli!
But OTOH, dhcpcd has had a few issues of its own. I wonder if part of the problem was that the RPi Organization ignored/overlooked five (5) major version upgrades to the upstream dhcpcd, and stubbornly stuck with dhcpcd ver 5?
For those interested, there is a thread in the RPi Forums website that describes how to revert to dhcpcd5 on bookworm I've not tried this - yet.