How to setup multiple WiFi networks?

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

    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?

  • 11chubby11

    11chubby11 Correct answer

    7 years ago

    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>
    

    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?

    I don't believe this to be the case.

    This approach works, however, is it possible to switch WiFi without having to restart the Pi?

    If no `id_str` given, the value will be `default`.

    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

    @MetalGodwin You can use `wpa_cli` as suggested here: https://www.raspberrypi.org/forums/viewtopic.php?t=179387

  • With Raspbian Jessie release, you don't have to edit the interface file. Just updating the wpa_supplicant 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.

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

    this type of tool is exactly what I was looking for. all of the flexibility offered by the GUI, but terminal instead. Thank you!

    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.

    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.

  • 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).

    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 He tried to bold the code, I'll edit it.

    Is there a way to make it automatically rescan and see if a higher priority network is available and switch?

    @Michal Yes, have a background process running that does just that.

    @les How would that work, which command?

    @Michal just set up a script that checks the strength every desired length of time

    Combine this with WPA supplicant on startup, ssh, and the ".local" addresses and you have a nice recipe for connecting the pi.

    Does "higher priority" mean a larger or a smaller integer?

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM