Header Shadow Image

Linux Networking: Persistent naming rules based on MAC for eth0 and wlan0


You've got a set of devices but their names, such as NIC / Network card names keep changing on each reboot or reconnect.  This is a continuation of Linux Networking: device eth0 does not seem to be present, delaying initialization.  where we resolved a similar issue due to naming but here we'll make it persistent using this simple naming system for Linux.  At the end of this very short post, we'll have a constant name for our physical device on reboots.  In particular, it is to make the line ip link set dev wlan1 name wlan0 permanent across reboots.


To solve this, we'll use UDEV rules to give a certain device a persistent name.  We'll use the 70-persistent-net.rules file to do just that so our network interfaces keep coming up with the same name on reboots or replugging.

# cat /etc/udev/rules.d/70-persistent-net.rules

# PCI device 0x10ec:0x8168 (r8169)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="1c:6f:35:3f:da:14", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# USB device 0x0cf3:0x1002 (usb) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:15:86:f1:65:d5", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"


There's several ways to get parameters for the devices you have.  Most notable is lspci that we can use here:

# lspci -s 03:00.0
03:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168B PCI Express Gigabit Ethernet controller (rev 03)

Add some -v[v….] options for verbosity to see more details.   Another way, which is probably the most common, is to use netstat or ifconfig to get your MAC address and other parameters you can use with which to uniquely define your rules:

# netstat -anevi
Kernel Interface table
bond0     Link encap:Ethernet  HWaddr 1c:6f:35:3f:da:14 
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::1e6f:65ff:fe3f:fc14/64 Scope:Link
          RX packets:53255 errors:0 dropped:0 overruns:0 frame:0
          TX packets:41224 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:28013914 (26.7 MiB)  TX bytes:4811379 (4.5 MiB)

Or ifconfig bond0 which would provide the exact same thing as above.  Another way still is to use udev itself to get more information on your device in this manner:

# udevadm info -a -p /sys/class/net/eth0 2>&1|grep -i "ATTR{address}"

If you have bonding setup, all MAC's may appear the same.  The way to find out the unique ones for each interface is with this command:

[root@mbpc bonding]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: down
Speed: 10 Mbps
Duplex: half
Link Failure Count: 0
Permanent HW addr: 1c:6f:65:3f:fc:14
Slave queue ID: 0

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 90:e2:ba:1a:08:28
Slave queue ID: 0
[root@mbpc bonding]#

Hope you found this useful.  Feel free to leave a reply!


  Copyright © 2003 - 2013 Tom Kacperski (microdevsys.com). All rights reserved.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License