2

I want to set a specific ethernet MAC address for an interface using UDEV rules. I have the following rule: SUBSYSTEM=="net", ACTION=="add", ATTRS{serial}=="50B123", ATTR{address}="00:22:33:44:55:AA", NAME="yolo0"

The rule matches and the interface is renamed to "yolo0" however the hw ether address remains unchanged from the one pre-set by the manufacturer. I can change the mac address manually of course using:

ifconfig yolo0 hw ether ..00:22:33:44:55:AA

Konrads
  • 870
  • 2
  • 20
  • 40

2 Answers2

3

You can use udev PROGRAM rules for that, by making the appropriate call to ip link set … address …. Like this:

/etc/udev/rules.d/10-network-persistent-custom-mac-address.rules

SUBSYSTEM=="net", ACTION=="add", ATTRS{serial}=="50B123", PROGRAM="/sbin/ip link set %k address 00:22:33:44:55:AA"
datenwolf
  • 289
  • 1
  • 11
1

IMHO udev is the wrong place to do that, the rule is for setting the interface name.

There are two things to consider when you add a network card:

  1. The name with which you'll refer to that interface

  2. The network properties you'll give to an interface with a given name, the name you chosen in point 1.

This said, you can set your MAC address, IP, subnet, autoneg et cetera when you setup / bring up your network interface, for example, on RH /SuSE you can do this for (let's say) eth0 in:

/etc/sysconfig/network-scripts/ifcfg-eth0

For RedHat add in there:

MACADDR=YOUR_DESIRED_MAC_ADDRESS

For SuSE:

LLADDR=YOUR_DESIRED_MAC_ADDRESS

And you're good to go.

Fredi
  • 2,257
  • 10
  • 13
  • Thank you, however this is problematic for hotplug devices and where the ifcfg collection of scripts isn't used. – Konrads Nov 09 '16 at 14:07
  • Why so? The udev gives you warranty that each plugged interface will have the same name. Then you choose what MAC to give it later, using ifcfg scripts or NetworkManager settings, based on the interface name – Fredi Nov 09 '16 at 14:17
  • @Fredi: Unfortunately it doesn't always can be done like that. Consider that with the arrival of USB-C many "docks" these days actually use a USB NIC with a MAC that's not tied to the host computer. In order to have a consistent MAC between different docks, or have different computers have unique MACs on the same dock, you must change the MAC based on a per-computer base, and can't use the dock's NIC MAC for persistent configuration purposes. – datenwolf Aug 13 '20 at 13:10
  • @datenwolf, I'm shooting in the dark as I'm not that much used to docker. But for what i know, it does NAT with the host, using a tun/tap interface. Well, nobody blocks you to change the MAC address of such a virtual iface, no? – Fredi Aug 17 '20 at 02:28
  • @datenwolf, Ah, BTW, the question wasn't related neither to docker nor to USB-C or such drivers – Fredi Aug 17 '20 at 02:34
  • 1
    @Fredi: Not *Docker* the containerization framework. I'm talking about *docks*, the things you can attach to a laptop. https://en.wikipedia.org/wiki/Docking_station – And the question is highly relevant to this use case, because a) I was confronted with the very problem i.e. *"how can I make my laptop use the same MAC address on every USB-C based docking station I connect it to"* and b) Googling it your answer was the first to show up. Unfortunately your answer doesn't help, hence I had to figure it out myself. – datenwolf Aug 17 '20 at 13:32