0

The Setup:

  • Raspberry Pi 3 B (Raspbian GNU/Linux 9 (stretch)) and boots to Command Line
  • (internal Wifi NIC) [wlan0]
  • additional external Wifi NIC [wlan1]

What I want to achieve:

I Have two different wireless networks: One is always available (a), the other one not (b). I want to configure the networking in a way, that:

wlan0 -> a
wlan1 -> b

and the following happens but should not:

wlan0 -> a
wlan1 -> a
----or----
wlan0 -> b
wlan1 -> b

How do I avoid this?

What have i done so far?

Try1 (wpa_supplicant.conf & /etc/network/interfaces)

pi@raspi:~ $ cat /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=DE

network={
        ssid="HomeNetwork"
        psk="############"
        id_str="home"
}

network={
        ssid="MobileNetwork"
        psk="############"
        id_str="mobile"
}
pi@raspi:~ $ cat /etc/network/interfaces
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 mobile inet dhcp

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

iface home inet dhcp

Try2

I've booted to Desktop and connected the interfaces to the according network and rebooted to Command Line...

3
  • why did you say nothing about what actually happens? ... you said nothing about any problem and you did not ask any questions
    – jsotola
    Commented May 19, 2019 at 21:16
  • what happens is what i explained above at "what should not happen" Commented May 19, 2019 at 21:18
  • it is not clearly stated that it is what happens ... it only says that it is an undesirable condition
    – jsotola
    Commented May 19, 2019 at 22:57

2 Answers 2

1

If you like to use systemd-networkd you can create two instances of wpa_supplicant that each can be managed independent from the other. Here in short I would do it.

First switch over to systemd-networkd:
Just follow to Use systemd-networkd for general networking. You can use section "♦ Quick Step". Then come back here.

Now create a wpa_supplicant.conf file for each connection (examples):

rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1
p2p_disabled=1
country=DE

network={
    ssid="HomeNetwork"
    psk="PasswordHomeNet"
    key_mgmt=WPA-PSK
}
EOF

rpi ~# cat > /etc/wpa_supplicant/wpa_supplicant-wlan1.conf <<EOF
ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1
p2p_disabled=1
country=DE

network={
    ssid="MobileNetwork"
    psk="PasswordMobileNet"
    key_mgmt=WPA-PSK
}
EOF

rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan1.conf
rpi ~# systemctl disable wpa_supplicant.service

rpi ~# systemctl enable [email protected]
rpi ~# systemctl enable [email protected]
rpi ~# rfkill unblock wlan

We have to bring up the interfaces with these two files:

rpi ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
DHCP=yes
#Address=192.168.1.86/24
#Gateway=192.168.1.1
EOF

rpi ~# cat > /etc/systemd/network/10-wlan1.network <<EOF
[Match]
Name=wlan1
[Network]
DHCP=yes
EOF

Reboot.

As you can see you have now two independent services you can manage each on its own way. For example if you have special settings for the mobile network you can do it only for the [email protected] or maybe give one interface a static ip address and gateway instead of DHCP enabled in its .network file.

2
  • Thanks for your reply. I pretty naively followed your steps but did not succeed. (I think i might have done the switching to systemd-networkd wrong). However, i found this on here. It describes the process of switching to systemd-networkd in more detail. So I'm trying this when i have the time. Commented May 21, 2019 at 11:20
  • @KiliJenkins Sorry, I have missed to bring up the interfaces with its configuration files in /etc/systemd/network/. I have updated the answer at the and. Starting with a fresh flashed Raspbian Stretch Lite image it will work. I have tested it.
    – Ingo
    Commented May 22, 2019 at 19:07
1

Use different wpa_supplicant files in How to set up networking/WiFi describes how to setup networking to use different wpa_supplicant files for each interface.

Your etc/network/interfaces should work (only because it causes dhcpcd to disable itself) and can be configured to use multiple wpa_supplicant files, however this uses old Debian networking, which is far less robust than dhcpcd

There is no guarantee which interface will be assigned to wlan0/wlan1 due to a race condition (although the onboard WiFi will probably win). To be sure you should enable Predictable Network Interface Names (this is used by most modern Linux OS).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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