How to change a network interface name on CentOS 7

Question: On CentOS 7, I would like to change the assigned name of a network interface to something else. What is a proper way to rename a network interface on CentOS or RHEL 7?

Traditionally, network interfaces in Linux are enumerated as eth[0123...], but these names do not necessarily correspond to actual hardware slots, PCI geography, USB port number, etc. This introduces a unpredictable naming problem (e.g., due to undeterministic device probing behavior) which can cause various network misconfigurations (e.g., disabled interface or firewall bypass resulting from unintentional interface renaming). MAC address based udev rules are not so much helpful in a virtualized environment where MAC addresses are as euphemeral as port numbers.

CentOS/RHEL 6 has introduced a method for consistent and predictable network device naming for network interfaces. These features uniquely determine the name of network interfaces in order to make locating and differentiating the interfaces easier and in such a way that it is persistent across later boots, time, and hardware changes. However, this naming rule is not turned on by default on CentOS/RHEL 6.

Starting with CentOS/RHEL 7, the predictable naming rule is adopted by default. Under this rule, interface names are automatically determined based on firmware, topology, and location information. Now interface names stay fixed even if NIC hardware is added or removed without re-enumeration, and broken hardware can be replaced seamlessly.

 * Two character prefixes based on the type of interface:
 *   en -- ethernet
 *   sl -- serial line IP (slip)
 *   wl -- wlan
 *   ww -- wwan
 *
 * Type of names:
 *   b<number>                             -- BCMA bus core number
 *   ccw<name>                             -- CCW bus group name
 *   o<index>                              -- on-board device index number
 *   s<slot>[f<function>][d<dev_port>]     -- hotplug slot index number
 *   x<MAC>                                -- MAC address
 *   [P<domain>]p<bus>s<slot>[f<function>][d<dev_port>]
 *                                         -- PCI geographical location
 *   [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][i<interface>]
 *                                         -- USB port number chain

A minor disadvantage of this new naming scheme is that the interface names are somewhat harder to read than the traditional names. For example, you may find names like enp0s3. Besides, you no longer have any control over such interface names.

If, for some reason, you prefer the old way, and want to be able to assign any arbitrary name of your choice to an interface on CentOS/RHEL 7, you need to override the default predictable naming rule, and define a MAC address based udev rule.

Here is how to rename a network interface on CentOS or RHEL 7.

First, let's disable the predictable naming rule. For that, you can pass "net.ifnames=0" kernel parameter during boot. This is achieved by editing /etc/default/grub and adding "net.ifnames=0" to GRUB_CMDLINE_LINUX variable.

Then run this command to regenerate GRUB configuration with updated kernel parameters.

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg

Next, edit (or create) a udev network naming rule file (/etc/udev/rules.d/70-persistent-net.rules), and add the following line. Replace MAC address and interface with your own.

$ sudo vi /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:a9:7a:e1", ATTR{type}=="1", KERNEL=="eth*", NAME="sushi"

Finally, reboot the machine, and verify the new interface name.

Note that it is still your responsibility to configure the renamed interface. If the network configuration (e.g., IPv4 settings, firewall rules) is based on the old name (before change), you need to update network configuration to reflect the name change.

Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

9 thoughts on “How to change a network interface name on CentOS 7

  1. You probably shouldn't edit grub.cfg directly but instead edit /etc/default/grub and add the parameter there:

    GRUB_CMDLINE_LINUX="vconsole.keymap=us crashkernel=auto vconsole.font=latarcyrheb-sun16 net.ifnames=0"

    And then update grub.cfg with:

    # grub2-mkconfig -o /boot/grub2/grub.cfg

    This way, when you install a new kernel you won't have to remember to update its entry in grub.cfg.

    (Though, in my experience that parameter is not strictly necessary, the udev rule is usually enough).

    Regards.

  2. Rebooting the machine may not be required for the new interface names to be in use. All you have to do is:

    1. rename the interface scripts in /etc/sysconfig/network-scripts
    2. reload udev rules (udevadm control --reload-rules)
    3. stop the network
    4. reload the network interface kernel module
    5. start the network up

    anyway, very useful post.

    Thanks

  3. I removed an interface from a server and wanted eth2 to become eth1; to do this I used "udevadm trigger --sysname eth2" and rebooted. Voila! That was with a virtio kvm guest.

  4. Is there any other way which would skip updating the persistent-net.rules file? I'm asking because this will be an issue when we need to use the machine as a template. If this file is present the interface's MAC address conflict will be an issue and the machine will not get IPv4 address.

  5. The grub.cfg file is not in the /boot/grub folder for EFI systems. Make sure to use the correct path for grub2-mkconfig. For Centos 7 with EFI boot, the command is:

    grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

  6. Be sure to keep your name somewhat short. Linux has a 15 character network name limit on the length of the namespace. So if you use a name like 'net-interface0' (14 characters) then you won't be able to set VLANs or aliases (or both) on the interface without it trunking the name.

    For example:

    net-interface0.1234:300 would be an alias interface on VLAN 1234 but it would just trunk it all.

Leave a comment

Your email address will not be published. Required fields are marked *