Change mac address under linux - the brutal way

Last days I run into a problem that I needed to change the mac address of a network card directly under linux. In the last years VxWorks was used for this purpose – oh and yes I know that you should not change a mac address 🙂 But I am working in a company which produces devices with network ports and they need an initial mac address.

The first thing you would try is to use ifconfig to change the mac address.

ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx

[email protected]:~# strace ifconfig eth0 hw ether 00:10:7e:02:52:b6 execve("/sbin/ifconfig", ["ifconfig", "eth0", "hw", "ether", "00:10:7e:02:52:b6"], [/* 11 vars */]) = 0 brk(0) = 0x943b000 access("/etc/ld.so.nohwcap", FOK) = -1 ENOENT (No such file or directory) mmap2(NULL, 4096, PROTREAD|PROTWRITE, MAPPRIVATE|MAPANONYMOUS, -1, 0) = 0xb77d0000 access("/etc/ld.so.preload", ROK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", ORDONLY) = 3 fstat64(3, {stmode=SIFREG|0644, stsize=20075, ...}) = 0 mmap2(NULL, 20075, PROTREAD, MAPPRIVATE, 3, 0) = 0xb77cb000 close(3) = 0 access("/etc/ld.so.nohwcap", FOK) = -1 ENOENT (No such file or directory) open("/lib/libc.so.6", ORDONLY) = 3 read(3, "177ELF111\0\0\0\0\0\0\0\0\03\03\01\0\0\0360m1\0004\0\0\0"..., 512) = 512 fstat64(3, {stmode=SIFREG|0755, stsize=1319176, ...}) = 0 mmap2(NULL, 4096, PROTREAD|PROTWRITE, MAPPRIVATE|MAPANONYMOUS, -1, 0) = 0xb77ca000 mmap2(NULL, 1329480, PROTREAD|PROTEXEC, MAPPRIVATE|MAPDENYWRITE, 3, 0) = 0xb7685000 mprotect(0xb77c3000, 4096, PROTNONE) = 0 mmap2(0xb77c4000, 12288, PROTREAD|PROTWRITE, MAPPRIVATE|MAPFIXED|MAPDENYWRITE, 3, 0x13e) = 0xb77c4000 mmap2(0xb77c7000, 10568, PROTREAD|PROTWRITE, MAPPRIVATE|MAPFIXED|MAPANONYMOUS, -1, 0) = 0xb77c7000 close(3) = 0 mmap2(NULL, 4096, PROTREAD|PROTWRITE, MAPPRIVATE|MAPANONYMOUS, -1, 0) = 0xb7684000 setthreadarea({entrynumber:-1 -> 6, baseaddr:0xb76846c0, limit:1048575, seg32bit:1, contents:0, readexeconly:0, limitinpages:1, segnotpresent:0, useable:1}) = 0 mprotect(0xb77c4000, 8192, PROTREAD) = 0 mprotect(0xb77ed000, 4096, PROTREAD) = 0 munmap(0xb77cb000, 20075) = 0 brk(0) = 0x943b000 brk(0x945c000) = 0x945c000 uname({sys="Linux", node="OT", ...}) = 0 access("/proc/net", ROK) = 0 access("/proc/net/unix", ROK) = 0 socket(PFFILE, SOCKDGRAM, 0) = 3 socket(PFINET, SOCKDGRAM, IPPROTOIP) = 4 access("/proc/net/ifinet6", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/ax25", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/nr", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/rose", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/ipx", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/appletalk", ROK) = -1 ENOENT (No such file or directory) access("/proc/sys/net/econet", ROK) = -1 ENOENT (No such file or directory) access("/proc/sys/net/ash", ROK) = -1 ENOENT (No such file or directory) access("/proc/net/x25", ROK) = -1 ENOENT (No such file or directory) ioctl(4, SIOCSIFHWADDR, {ifrname="eth0", ifrhwaddr=00:10:7e:02:52:b6}) = 0 exit_group(0) = ?

What this does is to change the mac address, but after a reboot this change is gone. So where is the mac address stored, you my ask. Normaly each NIC has an eeprom where some specific informations are stored like the mac address. And for linux there exists a nice tool to dump and change the contents of this eeproms – ethtool.

[email protected]:~# ethtool -e eth0 Offset Values ------ ------ 0x0000 00 10 7e 01 fc 5b 00 00 00 00 01 03 01 47 00 00 0x0010 00 00 00 00 c0 49 01 00 00 00 70 00 00 00 00 00 0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0060 20 00 01 40 0a 41 00 00 00 00 00 00 00 00 00 00 0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 e2 37

[email protected]:~# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:10:7e:01:fc:5b inet addr:10.204.120.12 Bcast:10.204.127.255 Mask:255.255.192.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:812 errors:0 dropped:0 overruns:0 frame:0 TX packets:37 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:125002 (122.0 KiB) TX bytes:3195 (3.1 KiB)

Did you see it? We found the place where our mac address is stored 🙂
Lets try to change the mac address directly in the eeprom.

[email protected]:~# ethtool -E eth0 magic 0x1234 offset 0x5 value 0x5c [email protected]:~# ethtool -e eth0 Offset Values ------ ------ 0x0000 00 10 7e 01 fc 5c 00 00 00 00 01 03 01 47 00 00 0x0010 00 00 00 00 c0 49 01 00 00 00 70 00 00 00 00 00 0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0060 20 00 01 40 0a 41 00 00 00 00 00 00 00 00 00 00 0x0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 e2 36

In theory our new mac address should be available after an reboot.

[email protected]:~# reboot [email protected]:~# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:10:7e:01:fc:5c inet addr:10.204.120.12 Bcast:10.204.127.255 Mask:255.255.192.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:482 errors:0 dropped:0 overruns:0 frame:0 TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:32327 (31.5 KiB) TX bytes:986 (986.0 B)

Success – now its time for the big fat warning:

Every NIC stores the mac address at different offsets etc. so its a good idea
to first look at the datasheet and then make experiments. Also the magic value
needed for ethtool needs to be kown – look at the corresponding linux driver.

Open Hub profile for Christian Gmeiner