46

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,353
  • 10
  • 32
  • 43
user2926577
  • 563
  • 1
  • 4
  • 5

3 Answers3

55

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,882
  • 3
  • 17
  • 22
Dishant
  • 666
  • 1
  • 6
  • 3
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,353
  • 10
  • 32
  • 43
13

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