How to set IP based on interface name (not MAC) on Centos 6.

Issues related to configuring your network
Post Reply
pacman128
Posts: 1
Joined: 2015/08/03 11:25:39

How to set IP based on interface name (not MAC) on Centos 6.

Post by pacman128 » 2015/08/03 11:34:21

I'm trying to automate the network interface setup that will be used on identical boxes. The boxes have 3 different NICs. Each of the 3 NICs have different MAC prefixes that I can use to differentiate them by and I have set up udev rules that map them to predictable names by using wildcard matching on the MAC address using the appropriate MAC prefix.

However, I'm having trouble mapping the interface by name to the desired IP configuration on Centos 6.5. I can't use the normal way (HWADDR) since that uses the full MAC address and they will be different on each box, so I'm trying to match on DEVICE name. The problem seems to be with the NetworkManager daemon.

What I have done, is to set up ifcfg-device files in the /etc/sysconf/network-scripts directory. For example, ifcfg-i1:
DEVICE=i1
TYPE=Ethernet
BOOTPROTO=none
IPADDR=10.102.30.158
PREFIX=24
GATEWAY=10.102.30.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
ONBOOT=yes
UUID=9ff7691b-e65b-4f9b-b6f2-e7549662403b
Everything works fine, if the interfaces all come up before the NetworkManager daemon starts. However, if this service starts before the 3 interfaces are "ready", the service sets the interface to a different IP than the one in its ifcfg file. Here are the relevant lines from /var/log/messages from a case where the IP is wrong:
Jul 31 18:19:14 centos6 kernel: ADDRCONF(NETDEV_UP): i1: link is not ready
Jul 31 18:19:14 centos6 kernel: ADDRCONF(NETDEV_UP): i2: link is not ready
Jul 31 18:19:14 centos6 kernel: e1000e: i1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: Rx/Tx
Jul 31 18:19:14 centos6 kernel: ADDRCONF(NETDEV_CHANGE): i1: link becomes ready
Jul 31 18:19:14 centos6 kernel: ADDRCONF(NETDEV_UP): i3: link is not ready
Jul 31 18:19:14 centos6 kernel: e1000e: i2 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Jul 31 18:19:14 centos6 kernel: ADDRCONF(NETDEV_CHANGE): i2: link becomes ready
Now the NetworkManager service starts, but i3 hasn't become ready yet.
Jul 31 18:19:15 centos6 NetworkManager[2350]: <info> NetworkManager (version 0.8.1-66.el6) is starting...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: parsing /etc/sysconfig/network-scripts/ifcfg-lo ...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: parsing /etc/sysconfig/network-scripts/ifcfg-i3 ...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: read connection 'System i3'
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: parsing /etc/sysconfig/network-scripts/ifcfg-i1 ...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: read connection 'System i1'
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: parsing /etc/sysconfig/network-scripts/ifcfg-i2 ...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: read connection 'System i2'
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: parsing /etc/sysconfig/network-scripts/ifcfg-em1 ...
Jul 31 18:19:15 centos6 NetworkManager[2350]: ifcfg-rh: read connection 'System em1'
Eventually i3 does become ready:
Jul 31 18:19:16 centos6 kernel: e1000e: i3 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Jul 31 18:19:16 centos6 kernel: ADDRCONF(NETDEV_CHANGE): i3: link becomes ready
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> (i1): device state change: 7 -> 8 (reason 0)
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> Policy set 'System i1' (i1) as default for IPv4 routing and DNS.
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> Activation (i1) successful, device activated.
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> Activation (i1) Stage 5 of 5 (IP Configure Commit) complete.
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> (i3): carrier now ON (device state 2)
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> (i3): device state change: 2 -> 3 (reason 40)
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> Auto-activating connection 'System i1'.
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> Activation (i3) starting connection 'System i1'
Jul 31 18:19:17 centos6 NetworkManager[2350]: <info> (i3): device state change: 3 -> 4 (reason 0)
But by this point NetworkManager has decided that i1 is the default policy and assigns the i1 IP (10.102.30.158) to i3 too.

I tried adding NM_CONTROLLED="no" in the ifcfg-xxx files, but the NetworkManager log then shows:
warning: NM_CONTROLLED was false but HWADDR or SUBCHANNELS was missing; device will be managed
for the interfaces, so that had no effect. I can't use HWADDR since I can't use exact MAC addresses.

First, is this a bug? Everything works fine if the interfaces are up before NetworkManager starts, so the results depend on a race condition.

If it's not a bug, how can I accomplish this? Disabling NetworkManager is probably not an option.

User avatar
TrevorH
Site Admin
Posts: 33218
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: How to set IP based on interface name (not MAC) on Cento

Post by TrevorH » 2015/08/03 11:54:00

HWADDR= is the way in which it assigns interfaces to cards. Perhaps you just need to write the right one to your ifcfg file.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

aks
Posts: 3073
Joined: 2014/09/20 11:22:14

Re: How to set IP based on interface name (not MAC) on Cento

Post by aks » 2015/08/03 16:36:38

Perhaps this will give you an idea/clue: http://linux.dell.com/files/biosdevname/

Post Reply