13

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
9

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
  • 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
16

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
  • 2
    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
  • 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
  • 7
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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