sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

[HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Dec 01, 2017 1:11 pm

If there are several WLAN NICs in use at the same time in your raspberry you must find a way to uniquely identify them.

Common practice for this appears to be activation of that so called systemd-predictable mechanism. At the expense of adapting configuration files each time you use another WLAN adapter. Since on a raspberry the NICs are identified by their MACs on default.

This is particularly disgusting if you need to run network interface dependent scripts on several identical systems but of course all with different MAC addresses. It's a no go.

Identifying the NICs by their MACs is the worst possible approach. I don't know why this is the default.

Solution:
1. switch off systemd-predictable mechanism stuff (unless done already):

Code: Select all

ln -nfs /dev/null /etc/systemd/network/99-default.link
2. setup udev rules that uniquely identify your interfaces based on their USB connector positions:

Simply create a file:

Code: Select all

vi /etc/udev/rules.d/72-wlan-geo-dependent.rules
with following content:

Code: Select all

#
# +---------------+
# | wlan1 | wlan2 |
# +-------+-------+
# | wlan3 | wlan4 |
# +---------------+ (RPI USB ports with position dependent device names for up to 4 optional wifi dongles)
# 
# | wlan0 | (onboard wifi)
#
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan2"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan3"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan4"
and you're done! No more hassle with clumsy MAC addresses.

The rule for 'wlan0' must not necessarily appear in the list above since 'wlan0' is assigned automatically for the first interface found without a special rule.

It is just an example. Of course you can alter the device names arbitrarily as long as they all stay different.

A special case exists if there is a maximum of 1 additional USB dongle in use. Since then a naming conflict among several dongles is impossible. You are allowed to name all ports the same. Easing up things even further.

Code: Select all

#
# +---------------+
# | wlan1 | wlan1 |
# +-------+-------+
# | wlan1 | wlan1 |
# +---------------+ (RPI USB ports with position independent device names for a maximum of 1 optional wifi dongle)
# 
# | wlan0 | (onboard wifi)
#
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan1"
in this example your dongle always gets name 'wlan1' no matter what USB port is used.

last but not least there even exists a more simple setup. BUT this is applicable only if
1.) you use only 1 wifi dongle maximum in addition to the onboard wifi
2.) you are satisfied to rename the onboard wifi interface to 'wlan1' (you can't rename it to 'wlan0' in this case)

a one-liner is sufficient for this:

Code: Select all

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="brcmfmac", NAME="wlan1"
allowing your wifi dongle to allocate 'wlan0' since the onboard wifi name is forced to 'wlan1'
Last edited by sparkie777 on Thu Dec 14, 2017 7:12 pm, edited 34 times in total.

User avatar
DougieLawson
Posts: 35519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Dec 01, 2017 1:13 pm

How is that any better than the built in, automatic support for predictable names?

Predictable names doesn't need any interaction, it "just happens" if you enable it.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Dec 01, 2017 1:20 pm

because the MAC-derived 'predictable' names are all different. And you even don't know them unless you have booted your system. Or find another way to get at the MACs. In that sense the device names are exactly not predictable. Perverting the chosen method.

That means you even can't setup an generic SD image in advance with identical interface configuration for a bunch of raspberries.

@DougieLawson:
What about scripts that want to access specific interfaces?

The name of each interface is different on each raspberry (because it's hardcoded to the MAC).

How do you handle that in your script which should run on different raspberries?

Could you please tell me what there "just happens" without manual intervention?
Last edited by sparkie777 on Wed Dec 20, 2017 3:26 pm, edited 12 times in total.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Dec 01, 2017 1:22 pm

I see how wlan0 is predictable here, but how about the rest? How does this determine which order they are assigned an interface name? It should work fine if only one usb wifi device is used.

Code: Select all

ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan2"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan3"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan4"

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Dec 01, 2017 1:25 pm

SurferTim wrote:
Fri Dec 01, 2017 1:22 pm
How does this determine which order they are assigned an interface
the 'KERNELS=="..."' part identifies the USB port your dongle is plugged in

paravz
Posts: 1
Joined: Tue Dec 05, 2017 5:56 pm

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Tue Dec 05, 2017 6:13 pm

Try per "driver" interface names, knowing that onboard wifi is "brcmfmac". This makes sense if you want your single usb device to retain it's name regardless of the USB port it's plugged in. This is not helping if you have 2 or more identical usb dongles.

Code: Select all

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="brcmfmac", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="8192cu", NAME="wlan1"
You can learn which driver your usb wifi is using by plugging it and learning it's name and driver:

Code: Select all

lsmod;  ifconfig; iwconfig; ip a l
Once you know the interface name, examine it with udevadm:

Code: Select all

udevadm test /sys/class/net/wifi1
Of course changing interfaces names might break your networking, do it from RPI console or have a ethernet cable connection as backup.

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Wed Dec 06, 2017 8:13 am

thanks for the advice.

another useful command is

Code: Select all

udevadm info -a /sys/class/net/wlan0
since it lists the udev database entries in a way suitable for direct inclusion in your rules

chispitas
Posts: 1
Joined: Thu Nov 09, 2017 6:32 pm

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sun Dec 10, 2017 5:10 pm

sparkie777 wrote:
Fri Dec 01, 2017 1:11 pm
If there are several WLAN NICs in use at the same time in your raspberry you must find a way to uniquely identify them.

Common practice for this appears to be activation of that so called systemd-predictable mechanism. At the expense of adapting configuration files each time you use another WLAN adapter. Since on a raspberry the NICs are identified by their MACs on default.

This is particularly disgusting if you need to run network interface dependent scripts on several identical systems but of course all with different MAC addresses. It's a no go.

Identifying the NICs by their MACs is the worst possible approach. I don't know why this is the default.

Solution:
1. switch off systemd-predictable mechanism stuff (unless done already):

Code: Select all

ln -nfs /dev/null /etc/systemd/network/99-default.link
2. setup udev rules that uniquely identify your interfaces based on their USB connector positions:

Simply create a file:

Code: Select all

vi /etc/udev/rules.d/72-wlan-geo-dependent.rules
with following content:

Code: Select all

#
# +---------------+
# | wlan1 | wlan2 |
# +-------+-------+
# | wlan3 | wlan4 |
# +---------------+ (RPI USB ports with position dependent device names for up to 4 optional wifi dongles)
# 
# | wlan0 | (onboard wifi)
#
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan2"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan3"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan4"
and you're done! No more hassle with clumsy MAC addresses.

The rule for wlan0 must not necessarily appear in the list above since wlan0 is assigned automatically for the first interface found without a special rule.

It is just an example. Of course you can alter the device names arbitrarily as long as they all stay different.

A special case exists if there is a maximum of 1 additional USB dongle in use. Since then a naming conflict among several dongles is impossible. You are allowed to name all ports the same. Easing up things even further.

Code: Select all

#
# +---------------+
# | wlan1 | wlan1 |
# +-------+-------+
# | wlan1 | wlan1 |
# +---------------+ (RPI USB ports with position independent device names for a maximum of 1 optional wifi dongle)
# 
# | wlan0 | (onboard wifi)
#
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan1"
in this example your dongle always gets name wlan1 no matter what USB port is used.
GREAT!!!

Llevaba 2 dias intentando conseguir esto. Gracias

karlsiegert
Posts: 1
Joined: Sat Jan 19, 2019 5:38 pm

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sat Jan 19, 2019 5:44 pm

Using the above content for /etc/udev/rules.d/72-wlan-geo-dependent.rules
worked great on a raspberry pi 3, but the configuration looks like it might be different on a 3+

So, my need is to have the same raspbian image and it loaded on various sd cards that will go into raspberry pi 3 boards and raspberry pi 3+ boards.
My goal is that the onboard wifi is ALWAYS wlan0 and the usb wifi dongle is always wlan1 (regardless if its a pi3 or pi3+). And I'd rather not change the .rules file for all pi3+ boards.... and I'd definitely prefer to not use MAC addresses as those would be different for every device.

Is there another way to ensure the onboard wifi is always wlan0 and the usb dongle is always wlan1?

Thanks.

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sun Jan 20, 2019 4:09 am

the examples given in the first post still are valid for RPi 3B+. I just verified.

As outlined above in your case you simply need a file '72-wlan-geo-dependent.rules' with content:

Code: Select all

ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.4",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.5",       NAME="wlan1"
for restrictions of that variant plse see first post.

GizmoEV
Posts: 4
Joined: Fri Jan 25, 2019 3:14 am
Location: Kelso, WA USA

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Jan 25, 2019 3:31 am

I just setup a RPi 3B+ as an access point for a personal weather station with a WiFi uplink. When I had it setup on a borrowed RPi 3B, wlan0 always was assigned to the internal WiFi. On the RPi 3B+ this was not the case. More often than not, wlan0 was assigned to the WiFi USB dongle and wlan1 to the internal WiFi. This is with the 2018-11-13 release of Raspbian Stretch with desktop. I needed to be able to change the USB WiFi dongle or move to a different RPi without editing the config files every time there was a change. Trying the code the OP listed it didn't work. Using info in this thread, however, I was able to determine the proper KERNELS=="nnn" values and also find the driver used with my dongle. I expanded the diagram of the USB ports showing the address of each. Maybe this will help someone else.

Code: Select all

#
#           +-----------------+
#           | 1-1.1.2 | 1-1.3 |
# +------+  +---------+-------+
# | eth0 |  | 1-1.1.3 | 1-1.2 |
# +------+  +-----------------+ (RPI USB ports with position dependent device names for up to 4 optional wifi dongles)
#
#
# | wlan0 | (onboard wifi)
#
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="sdio", KERNELS=="mmc1:0001:1", NAME="wlan0"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.1.2",       NAME="wlan3"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.1.3",       NAME="wlan4"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.3",       NAME="wlan1"
ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb",  KERNELS=="1-1.2",       NAME="wlan2"

# when using the lines below, only one WiFi device type can be used at a time
#ACTION=="add", SUBSYSTEM=="net", DRIVERS=="brcmfmac", NAME="wlan0"
#ACTION=="add", SUBSYSTEM=="net", DRIVERS=="rtl8192cu", NAME="wlan1"
David

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Fri Jan 25, 2019 10:32 am

thanks for sharing this. Maybe there is a special dependency on external hubs and other devices in use in your system?
Last edited by sparkie777 on Sat Jan 26, 2019 5:22 am, edited 1 time in total.

GizmoEV
Posts: 4
Joined: Fri Jan 25, 2019 3:14 am
Location: Kelso, WA USA

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sat Jan 26, 2019 1:05 am

I don't know. I have the RPi3B+ with 7" Raspberry touch screen. The only USB item I plugged in was one of two different USB WiFi dongles. This is a mobile unit but when I'm at home I want it to connect with a given IP address handed out by DHCP and it wouldn't always get the same IP, I wanted the internal WiFi to be used for the uplink since it has 5GHz as an option. I thought at first something was wrong with my cable modem's DHCP server, then I realized that each IP had a different MAC address. It was very frustrating to figure it out. I'm glad this thread was there to guide me. :D

disaacs
Posts: 4
Joined: Tue Apr 09, 2019 10:13 pm

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Thu Apr 11, 2019 4:41 pm

I have encountered exactly the same issue as GizmoEV, using a RPi3B+ purchased in March. I hobbled through debugging the issue myself, until I finally decided to read further down in this thread. GizmoEV solution works perfectly :)

matt995
Posts: 2
Joined: Sat Apr 27, 2019 7:18 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sat Apr 27, 2019 7:27 am

I was trying to use this method but, in my case, I need to connect multiple identical dongles resulting in having the same MAC addresses (but with different gateway addresses). Even if I try to change their MAC addresses, my dongles are not recognized properly and they are always configured with addresses 169.254.0.0/16. Is there a way to solve this problem?

sparkie777
Posts: 121
Joined: Tue Nov 27, 2012 4:37 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sat Apr 27, 2019 12:02 pm

matt995 wrote:
Sat Apr 27, 2019 7:27 am
Even if I try to change their MAC addresses, my dongles are not recognized properly and they are always configured with addresses 169.254.0.0/16. Is there a way to solve this problem?
- how do you change their MAC addresses?
- the IP addresses your dongles receive is a matter of your DHCP servers
- this article is about how to access various dongles thru the device file system. As it seems this already works for you. Correct?

matt995
Posts: 2
Joined: Sat Apr 27, 2019 7:18 am

Re: [HOWTO] assign predictable WLAN device names without fiddling with MAC addrs

Sun Apr 28, 2019 8:26 am

sparkie777 wrote:
Sat Apr 27, 2019 12:02 pm
- how do you change their MAC addresses?
I used the following code in /etc/systemd/network/01-huawei-e8372h.link

Code: Select all

[Match]
MACAddress=0c:5b:8f:27:9a:64
Driver=cdc_ether
[Link]
Description=Huawei E8372h
NamePolicy=path
MACAddressPolicy=random
sparkie777 wrote:
Sat Apr 27, 2019 12:02 pm
- the IP addresses your dongles receive is a matter of your DHCP servers
- this article is about how to access various dongles thru the device file system. As it seems this already works for you. Correct?
My raspberry should receive an IP form each dongle but, if more than one is connected, this doesn't happen. I'm not sure I'm able to access the dongles because, even if I change /etc/network/interfaces assigning a static IP, I'm not able to ping the dongles themselves.

Return to “Networking and servers”