42

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?

techraf
  • 4,319
  • 10
  • 31
  • 42
user2926577
  • 523
  • 1
  • 4
  • 5

3 Answers3

49

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 commands:

wpa_cli -i wlan0 list_networks

to get a list of your configured networks. And to connect to first network:

wpa_cli -i wlan0 select_network 0

To shift from wifi_A to wifi_B use:

wpa_cli -i wlan0 select_network 1

Note: Using select_network disables until next boot all other Wifis. So if this Wifi goes away your device will remain offline until you enable or select another network, see https://nxmnpg.lemoda.net/8/wpa_cli You can use wpa_cli -i wlan0 enable_network all to re-enable them after switching to the other network to avoid beeing stuck on one wifi.

MatsK
  • 2,791
  • 3
  • 16
  • 20
Dishant
  • 606
  • 1
  • 6
  • 3
  • 5
    to check which number to use in wpa_cli select_network #, run wpa_cli list_networks first. – Z-WolF Feb 01 '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
  • 13
    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
  • 2
    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 '20 at 23:28
  • Thank you very much for giving a description for priority, which is a very essential thing. – itsraghz Sep 26 '20 at 07:28
  • Also to check the PI is actually on the selected network use: iw wlan0 info – Pierz Jan 16 '21 at 18:36
18

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
techraf
  • 4,319
  • 10
  • 31
  • 42
  • 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 02: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 01 '18 at 19:42
  • 1
    It's worth noting explicitly that higher-numbered priorities get used first. (So, the colloquial idea of "1st priority" is not what priority=1 means, in the presence of a priority=2 (or priority=800, or whatever). Your answer implies this, given a careful read of both question and answer, just... worth pointing out, since it's potentially counter-intuitive. – lindes Jan 11 '22 at 12:07
12

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 
jshep321
  • 231
  • 2
  • 4
  • 6
    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 08 '20 at 14:09