154

I go back and forth from home to school with my Pi. I just got the WiFi working last night at home using wpa.conf instead of wpa_supplicant.conf as the book I was using had the walk thru for that instead. I've noticed lots seem to use wpa_supplicant but I not sure why...

Anyway, I know how to make the WiFi IP static for my home network but how do I set things up for auto-connecting to my school WiFi and use a different static IP for that network?

For the network do I just add another network={} in my wpa.conf file? For the second static IP how do I bind that to the school network?

Chef Flambe
  • 1,783
  • 3
  • 12
  • 12
  • 2
    This question is quite old, and the answers are too -- about 5 years, and the most recent edits are three years old. What's the current technique for solving this problem? – MikeB Feb 05 '20 at 02:54
  • 1
    @MikeB: A technique for [actively switching between Access Points is covered here](https://raspberrypi.stackexchange.com/a/136933/83790) (posted Apr 6, 2022). The answer to this question is a simplification of the *"active switching"* answer because by default `wpa_supplicant` will choose the network with the strongest signal; i.e. the selection will be done *automatically* as long as the `wpa_supplicant.conf` file contains the specs for the networks. – Seamus Apr 06 '22 at 05:12

5 Answers5

131

This post was OK at the time for Wheezy. DO NOT USE

Edit /etc/wpa_supplicant/wpa_supplicant.conf and add id_str="school" under the schools wpa info and id_str="home" under your homes wpa info. Your file should now look similar to this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SCHOOLS NETWORK NAME"
    psk="SCHOOLS PASSWORD"
    id_str="school"
}

network={
    ssid="HOME NETWORK NAME"
    psk="HOME PASSWORD"
    id_str="home"
}

Then set up /etc/network/interfaces with iface school inet static and iface home inet static in it so it looks like the following:

This applies to Raspbian Wheezy prior to 2015-05-05 for later (and Jessie) See How do I set up networking/WiFi/Static IP

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface school inet static
address <school address>
gateway <school gateway>
netmask <school netmask>

iface home inet static
address <home address>
gateway <home gateway>
netmask <home netmask>
Milliways
  • 58,054
  • 29
  • 98
  • 195
11chubby11
  • 4,764
  • 5
  • 25
  • 32
  • It might be worth noting: If I understand the documentation correctly, in order to use this approach, you must have and id_str property for EVERY network in your wpa_supplicant.conf file. Otherwise this won't work. Can you confirm? – PICyourBrain Mar 31 '14 at 20:29
  • I don't believe this to be the case. – les Nov 21 '14 at 01:24
  • 3
    This approach works, however, is it possible to switch WiFi without having to restart the Pi? – MetalGodwin Jul 14 '16 at 17:53
  • 3
    If no `id_str` given, the value will be `default`. – Raptor Feb 07 '17 at 13:35
  • where can I read about `wpa-roam` and `wpa-config` and other wpa_supplicant statements in `/etc/network/interfaces`? Neither `man interfaces` nor `man wpa_supplicant.conf` have anything about those statements – Drew Apr 08 '18 at 18:10
  • 2
    @MetalGodwin You can use `wpa_cli` as suggested here: https://www.raspberrypi.org/forums/viewtopic.php?t=179387 – dsapalo Oct 15 '18 at 16:18
  • After I fixed my `/etc/wpa_supplicant/wpa_supplicant.conf`, I also restart services of wpa_supplicant and networking via `systemctl`. It's working now. – Chu-Siang Lai Jun 19 '23 at 03:05
49

With Raspbian Jessie release, you don't have to edit the interface file. Just updating the /etc/wpa_supplicant/wpa_supplicant.conf file with multiple networks would suffice. Here's how it looks -

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SCHOOLS NETWORK NAME"
    psk="SCHOOLS PASSWORD"
    id_str="school"
}

network={
    ssid="HOME NETWORK NAME"
    psk="HOME PASSWORD"
    id_str="home"
}

This worked for me and my wifi is always connected to the list of available networks mentioned in the above file. Hope it helps.

Madacol
  • 103
  • 3
giri-sh
  • 757
  • 10
  • 18
  • 1
    This does not appear to work if you specify `id=` but if you instead specify a `priority=` for each then it does work. – Ian Mercer Oct 21 '20 at 03:37
  • If you edit the wpa_supplicant.conf file, you need to restart the demon to get it to read the new .conf: sudo killall -HUP wpa_supplicant – Lex Lindsey Mar 29 '21 at 00:10
  • 2
    You can also run `wpa_passphrase my_SSID my_password` and it will return the config needed to put into `/etc/wpa_supplicant/wpa_supplicant.conf` – Madacol May 21 '22 at 18:36
  • 2
    One-liner: `sudo sh -c 'wpa_passphrase my_SSID my_password >> /etc/wpa_supplicant/wpa_supplicant.conf'` – Madacol May 21 '22 at 18:36
43

I recently stumbled across a console application that sorts all the wireless configuration hell out. You can also use this tool to configure the LAN interface.

sudo apt-get install wicd-curses

It will install quite a few other packages but it runs its own daemon in the background. This manages the networks and makes sure you connect to the ones you want. Run it with:

sudo wicd-curses

Screenshot of wicd-curses

If you get a message saying no networks detected press P (must be capital so use [SHIFT]p) and type in wlan0 in the wireless interface field and press F10 to save.

  1. R to refresh the list.
  2. Use the cursors on the keyboard to navigate up and down the list
  3. Press right to configure the wireless connection
  4. Press down a few times and check "Automatically connect to this network"
  5. Press down a few times again and type in your password in the key field
  6. Press F10 to save
  7. Start from 1 to do this again for any other networks

enter image description here

You might have to press C to connect to the access point. If you were connected via cable, that will most likely kill the LAN interface and bring up wireless.

It is also manages the connection so it will reconnect to any configured wireless access points if it drops out for whatever reason but it will also try to connect to any available networks, just like in Windows, Macintosh or Linux Desktops.

Hope it helps!

jncraton
  • 103
  • 2
Piotr Kula
  • 17,247
  • 6
  • 64
  • 104
  • 2
    this type of tool is exactly what I was looking for. all of the flexibility offered by the GUI, but terminal instead. Thank you! – FoamyGuy May 24 '14 at 21:16
  • Upvote: After running this I did an Advanced IP Scan of the two subnets and my Rpi3 shows up on both of them. Interestingly, when I went to install this initially, it told me I already had the latest version. – SDsolar Mar 16 '17 at 18:17
  • One minor detail: To get extra range I am using wlan1 with an external antenna, so in curses I had to go into prefs to specify that adapter. There was no need to shut off wlan0 but I did it anyway with sudo ifcongig wlan0 down. – SDsolar Mar 16 '17 at 19:03
  • Unfortunately `wicd-curses` is not included in the repositories anymore. It also depends on python2 – mashuptwice Oct 12 '22 at 08:48
24

Actually you can add the priority option. Like so:

network={
      ssid="open"
      key_mgmt=NONE
      id_str="open"
      priority=3
}

network={
        ssid="secure"
        key_mgmt=WPA-EAP
        proto=WPA2
        group=CCMP
        pairwise=CCMP
        eap=TLS
        ca_cert="/etc/certs/cacert.pem"
        client_cert="/etc/certs/client.pem"
        private_key="/etc/certs/client.key"
        private_key_passwd="somepwd"
        identity="me"
        priority=5
}

network={
        ssid="AndroidAP"
        key_mgmt=WPA-PSK
        proto=WPA2
        pairwise=CCMP
        group=CCMP
        psk="SomeP4ssw0rd"
        priority=4
}


network={
        ssid="Spooky"
        key_mgmt=NONE
        group=WEP104
        psk="A4ABC2FC27412D4D23CAEBCA23"
        priority=2
}

network={
        ssid="another"
        key_mgmt=WPA-PSK
        proto=WPA2
        pairwise=CCMP
        group=CCMP
        psk="A very long and secret passphrase here"
        priority=1
}

priority: when multiple networks are available simultaneously, the one with the highest priority value is selected.

id_str: for each network, you can give this parameter a specific value (a string). If none is provided, "default" is used as text string. This string is used in /etc/network/interfaces as a virtual interface identifier. This allow creating specific configuration blocks for each network. The only requirement is to have the physical interface using the "inet manual" method (this is a MUST).

Patrick Cook
  • 6,335
  • 6
  • 36
  • 63
les
  • 371
  • 3
  • 6
  • Just double-checking here, priority is enclosed in asterisks? From what I read, they're not supposed to be enclosed in double-asterisks, but I might be wrong here. – ericmjl Jan 13 '16 at 17:17
  • @ericmjl He tried to bold the code, I'll edit it. – Patrick Cook Mar 06 '16 at 05:37
  • Is there a way to make it automatically rescan and see if a higher priority network is available and switch? – Michal Mar 10 '17 at 10:50
  • @Michal Yes, have a background process running that does just that. – les Oct 03 '17 at 22:57
  • @les How would that work, which command? – luckydonald Dec 18 '17 at 13:32
  • @Michal just set up a script that checks the strength every desired length of time – les Feb 17 '18 at 03:55
  • Combine this with WPA supplicant on startup, ssh, and the ".local" addresses and you have a nice recipe for connecting the pi. – Danny Staple Apr 21 '18 at 20:52
  • 3
    Does "higher priority" mean a larger or a smaller integer? – zbeekman Jul 30 '18 at 23:52
  • 1
    What is the purpose of `priority` in the context of the question? The OP was clear that the networks won't be available simultaneously. – deanresin Jul 26 '21 at 02:32
  • @deanresin: Good pont :) No one seems to be aware of the fact that when `priority` is equal for all networks configured in `wpa_supplicant.conf` (which is the *default*, the one with the strongest signal will be selected. – Seamus Apr 06 '22 at 05:22
10

It's April, 2022, and this question of "How to setup multiple WiFi networks?" now has a straightforward answer:

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.

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:

  • Then set up /etc/network/interfaces with iface school inet static and iface home inet static

    Two major no-nos here:

    1. dhcpcd now handles network management for RPi - this file should not be edited unless you know what you're doing
    2. static ip assignment is almost never a good idea - esp. when you're using a network you don't manage.
  • If you edit the wpa_supplicant.conf file, you need to restart the demon to get it to read the new .conf ...

    No - not true; that may work, but a less disruptive method is to use the wpa_cli app to handle this; see the ArchWiki for details

And there are others, but impossible to list them all here. Be diligent - and that goes for this answer also!

Seamus
  • 20,552
  • 3
  • 31
  • 67
  • A small note: If you make wifi-related changes in raspi-config *after* making the change to the conf, it runs `wpa_cli save_config` which "updates" the config, wiping the changes you made. You can instead use wpa_cli directly to add the networks. I don't have the rep for this exchange to add an answer directly on this "Highly active question", so a comment will have to do. Run `wpa_cli`, the way to add the first network is: `add_network`, `set_network 0 ssid MySSID`, `set_network 0 psk MyPassword`, `set_network 0 disabled 0`, `quit`, then run `wpa_cli save_config` to finish. – zaTricky Dec 06 '22 at 15:43