User avatar
scruss
Posts: 5382
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

udev for stable MAC address: Raspberry Pi Zero gadget mode

Mon Jan 16, 2017 8:55 pm

I've got a Raspberry Pi Zero in Gadget Mode attached to a Linux laptop. It appears on the USB bus as

Code: Select all

Bus 003 Device 019: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget
It's currently appearing as interface enp0s20u1 with a MAC address of 66:e0:93:30:33:73, and I'm currently remotely logged on via ssh into Raspbian Lite. This is good.

What's not so good, though, is that the MAC address and interface on the Linux box seem to be completely random on startup: it was enp0s20u2u2 before, with the MAC set to 8a:94:1c:a1:f5:a2. Needless to say, this makes having a stable IP address to log in to rather difficult

Is there a way to have the Raspberry Pi Zero produce a consistent MAC address? Failing that, any suggestions for udev rules for the connected Linux box to identify the same Zero on the same MAC?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
elkberry
Posts: 167
Joined: Wed Dec 28, 2016 9:21 pm

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Tue Jan 17, 2017 6:16 am

Does this one here may help? http://linux-sunxi.org/USB_Gadget/Ethernet says...

/etc/modprobe.d/g_ether.conf:
options g_ether host_addr=00:11:22:33:44:55

Device name depends on the USB port where you plug in your device.
From ZX81 to Raspberry Pi, but wait ... where's the 7805 gone?

User avatar
scruss
Posts: 5382
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

[SOLVED] stable MAC address: Raspberry Pi Zero gadget mode

Tue Jan 17, 2017 6:28 pm

Thank you! That was a most useful lead.

If you do this once on your Raspberry Pi Zero:

Code: Select all

echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf
you'll end up with stable network addressing.

Update: you probably want to do the cmdline.txt thing below as well/instead, as this stopped working reliably for me. Editing /boot/cmdline.txt seems to be more reliable.
Last edited by scruss on Wed Jan 25, 2017 7:48 pm, edited 1 time in total.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

tweak42
Posts: 542
Joined: Tue Mar 22, 2016 3:16 am
Location: Los Angeles, California

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Sat Jan 21, 2017 4:22 am

You can also pass the kernel parameters for static MAC addresses to the g_ether module in the cmdline.txt
I prefer this method because it's on the FAT boot partition and easily added when enabling gadget mode.

Code: Select all

modules-load=dwc2,g_ether g_ether.host_addr=00:22:82:ff:ff:20 g_ether.dev_addr=00:22:82:ff:ff:22
g_ether.dev_addr is the Pi Zero interface.
g_ether.host_addr is the host PC interface.

viewtopic.php?t=166002&p=1069725#p1069670

User avatar
scruss
Posts: 5382
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Sat Jan 21, 2017 5:13 pm

Yup, that's another way to do it. While it does have the advantage of being on the boot partition for easy access on other systems, mucking about with cmdline.txt programmatically is not something I feel confident about doing.

It would be nice if there were a startup config for the Zero that, noticing it was in Gadget mode, would cache the MAC addresses for reuse. Stability in network addressing is a good thing.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
scruss
Posts: 5382
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: udev for stable MAC address: Raspberry Pi Zero gadget mo

Sat Feb 18, 2017 4:29 am

I meant to add that I've found that certain systems (like my Ubuntu laptop) timeout the network connection for USB networking if it's not immediately available. What I've found helpful is start the Zero with a power bank/cable into the POWER socket, then when the Zero appears to have booted, plug in the USB data cable too. It should then be immediately recognized at the MAC addresses you specified. You can - if you're feeling brave - take the power connector out once you have a data connection.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Schorschi
Posts: 362
Joined: Thu Nov 22, 2012 9:38 pm

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Sun Mar 04, 2018 9:38 pm

Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?

User avatar
HawaiianPi
Posts: 7748
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Mon Mar 05, 2018 12:35 pm

Schorschi wrote:
Sun Mar 04, 2018 9:38 pm
Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?
The Pi Zero has no Ethernet hardware, so there is no hard coded Ethernet MAC address. USB Gadget Mode has been a work-in-progress for awhile now, and there have been some improvements (it's much easier to setup now), but there is still room for further improvement.

Code: Select all

echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | sudo tee /etc/modprobe.d/g_ether.conf
Worked for me, and has continued to work.

I honestly don't understand the point of USB Gadget Mode. It's really been nothing more than a curiosity to me, so apparently I've missed something. What does connecting a far less powerful computer to a far more powerful computer accomplish? I can see some possible uses in education, which is what the Raspberry Pi was created for, but other than that I don't get it.

With Raspbian Stretch it's possible to pre-configure an SD card to boot any Pi, connect to a wireless network and SSH into it, so why would I use USB Gadget Mode over doing that?

Anyway, like I said, it's an amusing curiosity to play with, but I don't find it very useful to tether a Pi to my main PC when I have the option of running them independently.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups, and where is that annoying music coming from?

ejolson
Posts: 10972
Joined: Tue Mar 18, 2014 11:47 am

Re: udev for stable MAC address: Raspberry Pi Zero gadget mode

Wed Mar 07, 2018 2:43 am

Schorschi wrote:
Sun Mar 04, 2018 9:38 pm
Who is the idiot, yes I say idiot, that thought a dynamic MAC address by default is a good thing? This is a horrible issue for network security design and control systems. The default SHOULD BE STATIC, have total human intelligence for network device design dropped sharply recently?
Using MAC addresses for security in any situation is about as insecure as using IP numbers. In this case we are using a virtual Ethernet device over USB. I don't think it is likely someone could secretly plug a Zero into my USB hub without me noticing. At the same time, it can be inconvenient for the MAC address to change with every reboot. Another example of preventing this using rpiboot is described in my post on a cluster of Pi Zero computers.

Return to “Advanced users”