3

I am running a headless Raspberry Pi and I have multiple wireless networks at home. I SSH into my Pi. Is there a command line way to switch which network the Pi is on? Yes, I realize that I will lose the connection until after I reconnect to the new network.

  • Reputation too low to add a comment, so I have to use this route. I already have my wpa_supplicant set up with four networks. And the priority. The question is if I need to jump to the priority 2 network, how do I do that via command line? – Aspen May 29 '17 at 20:06
2

Run the following command:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Which will enable you to edit the wpa_supplicant file. Add the following code to the bottom:

network={
    ssid="HomeOneSSID"
    psk="passwordOne"
    priority=1
    id_str="homeOne"
}

network={
    ssid="HomeTwoSSID"
    psk="passwordTwo"
    priority=2
    id_str="homeTwo"
}

Exit the file with saving. I suggest doing this for the different networks you intend to connect to. In the example code above the details for two different networks has been added.

Note also that the priority line determines which network is connected to in preference to another available network. This isn't necessary but might be useful in your case.

Next you need to run:

sudo nano /etc/network/interfaces

And using the example networks from above add these lines to the bottom of the file:

iface homeOne inet dhcp
iface homeTwo inet dhcp

The finished file should look something like this:

auto lo
iface lo inet loopback

iface eth0 inet manual

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

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

iface homeOne inet dhcp
iface homeTwo inet dhcp
  • 1
    This MAY work, but it will also break dhcpcd, preventing the networking icon on GUI from working (among other things). Have you actually tested it on a normal Raspbian installation? – Milliways May 30 '17 at 0:03
  • I must confess I haven't, but this seems to be the way to do it by just using a command line interface. The fact it disables the GUI icon isn't great but the OP said in their question they only SSH into their Pi, so I wouldn't have thought that would have been an issue for them. – Darth Vader May 30 '17 at 8:54

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.