24

I understand that I can set up multiple WiFis by adding to /etc/wpa_supplicant/wpa_supplicant.conf (as prescribed in Setting WiFi Up Via The Command Line).

Let's say I have two WiFis registered: wifi_A and wifi_B

When I unplug router wifi_A, and reboot my raspberry to command line, it automatically connects to wifi_B. When I unplug router wifi_B, and reboot my raspberry to command line, it automatically connects to wifi_A. So far so good.

But, let's say both routers are available, how can I set priorities? E.g. I'd like my Raspberry to connect to wifi_B upon reboot?

My second question is: Let's say I am in the command line mode and I am currently connected to wifi_B. How can I connect to wifi_A instead?

  • Unless you list your config files, all you will get is a lot of speculation (at best). – Milliways Nov 30 '16 at 7:05
15

E.g. I'd like my raspberry to connect to wifi_B upon reboot?

Add priority=2 to the wifi_B block and priority=1 to the wifi_A block in the /etc/wpa_supplicant/wpa_supplicant.conf file.

Let's say I am in command line mode and I am currently connected to wifi_B. How can I connect to wifi_A instead?

You can create a separate config file for each of the SSIDs and specify it explicitly:

wpa_supplicant -B -Dwext -iwlan0 -c</path/to/config_for_wifi_A>
sudo dhclient wlan0
| improve this answer | |
  • Using your approach, I can switch from one WiFi network to the other only once, can't switch back (or to another WiFi_C for ex.). After using the wpa_supplicant command to do the 1st switch, using it again I get a message ctrl-iface exists and seems to be in use - cannot override it \n Delete '/var/run/wpa_supplicant/wlan0' manually if it is not used anymore etc ... And after sudo dhclient wlan0 I'm being told RTMETLINK answers: File exists. Is this what to expect? – calocedrus Jan 31 '18 at 2:09
  • you need to first shutdown the connection with wpa_cli terminate and then connect to another with wpa_supplicant, there's also a more complicated way without shutting it down here – Z-WolF Feb 1 '18 at 19:42
26

But, let's say both routers are available, how can I set priorities?

You can set priorities for network as follows:

network={
    ssid="wifi_A"
    psk="passwordOfA"
    priority=1
}
network={
   ssid="wifi_B"
   psk="passwordOfB"
   priority=2
}

By default priority of all networks is 0, set higher priority to prioritize as per your need.

Let's say I am in the command line mode and I am currently connected to wifi_B. How can I connect to wifi_A instead?

For that use the command:

wpa_cli select_network 0

To shift from wifi_A to wifi_B use:

wpa_cli select_network 1
| improve this answer | |
  • 3
    to check which number to use in wpa_cli select_network #, run wpa_cli list_networks first. – Z-WolF Feb 1 '18 at 19:47
  • 1
    This does not work for me with a Pi 3+ on Stretch. Ran wpa_cli list_networks and got the network numbers, and then wpa_cli select_network 2. System responded with OK but iwconfig shows it's still connected to the original network. – lonstar Sep 17 '18 at 15:11
  • 10
    A note for clarity: The priority value is applied as a higher value meaning higher priority. i.e. networks are not selected in numeric/counting order (zero first). – Nicolas Dec 18 '18 at 21:09
  • 1
    On Buster, there is an interface "p2p-dev-wlan0", which I found is selected by default. Therefore, the commands above need to include -i wlan0. For example: wpa_cli -i wlan0 select_network 0. This works and it's perfect for my application because it doesn't need sudo – ThatsRightJack May 11 at 23:28
  • Thank you very much for giving a description for priority, which is a very essential thing. – itsraghz Sep 26 at 7:28
3

So to elaborate on the full solution here:

network={
    ssid="wifi_A"
    psk="passwordOfA"
    priority=1 #lower priority
}
network={
   ssid="wifi_B"
   psk="passwordOfB"
   priority=2 #higher priority
}

Then after initial reboot, to actively switch networks (without a secondary reboot):

sudo wpa_cli list_networks #show them!
sudo wpa_cli -i wlan0 select_network 0 #0, 1, etc.  Note: Networks are NOT the same number as your priority in the wpa_supplicant 
| improve this answer | |
  • 1
    Thanks for the comments in the example! It's very counter-intuitive that higher priority is further in the sequence of natural numbers... I've spent quite some time trying to figure out why "first" priority was ignored in favor of "second" priority. – Ruslan Aug 8 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.