3

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

2 Answers 2

4

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"
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.

6
  • Thank you, however this is problematic for hotplug devices and where the ifcfg collection of scripts isn't used.
    – Konrads
    Commented Nov 9, 2016 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
    Commented Nov 9, 2016 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
    Commented Aug 13, 2020 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
    Commented Aug 17, 2020 at 2:28
  • 1
    @Fredi: Not Docker the containerization framework. I'm talking about docks, the things you can attach to a laptop. 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
    Commented Aug 17, 2020 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .