Renaming Network Interfaces
Introduction
In an earlier OS (such as Fedora 13, Ubuntu 15, and CentOS 6), network interfaces are named eth0, eth1, and eth2. During system initialization, the Linux kernel allocates names to network interfaces by combining fixed prefixes and indexes. For example, eth0 indicates the first Ethernet interface detected during system startup. If a new network interface is added, the existing network interface names may change because they may be initialized in a different sequence after the system is restarted.
To solve this problem and make it easier to identify network devices, modern Linux distributions use a consistent network device naming rule and use udev to manage devices in a unified manner based on these rules. There are multiple naming rules available for udev. By default, udev allocates names to network interfaces based on firmware information, topology, and physical device locations. The renaming service for network interfaces is provided by systemd-udevd which can ensure the consistency and stability of network device names.
A Consistent Network Naming Rule
The naming rule is: device type + device location.
Device type
- en: Ethernet
- wl: WLAN
- ww: WWAN
Device location
|
Format |
Description |
|---|---|
|
o<on-board_index_number> |
Network interface built in BIOS of the mainboard |
|
s<hot_plug_slot_index_number>[f<function>][d<device_id>] |
PCI-E network interface built in BIOS of the mainboard |
|
x<MAC> |
MAC address |
|
p<bus>s<slot>[f<function>][d<device_id>] |
Independent PCI-E network interface |
|
P<domain_number>]p<bus>s<slot>[f<function>][u<usb_port>][.][c<config>][i<interface>] |
USB network interface |
Procedure
Rename a network interface. For example, rename ens5 to eth0.
- Modify the configuration file of the network interface.
- If the configuration file /etc/sysconfig/network-scripts/ifcfg-ens5 exists, change its name from ifcfg-ens5 to ifcfg-eth0.
mv /etc/sysconfig/network-scripts/ifcfg-ens5 /etc/sysconfig/network-scripts/ifcfg-eth0
Edit the ifcfg-eth0 file to change the values of NAME and DEVICE to the new network interface name eth0.
- If the configuration file /etc/sysconfig/network-scripts/ifcfg-ens5 does not exist, create a configuration file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following content to the new file:
DEFROUTE=yes BOOTPROTO=dhcp NAME=eth0 DEVICE=eth0 ONBOOT=yes
- If the configuration file /etc/sysconfig/network-scripts/ifcfg-ens5 exists, change its name from ifcfg-ens5 to ifcfg-eth0.
- Modify the GRUB configuration.
Modify /etc/default/grub to add net.ifnames=0 and biosdevname=0 to GRUB_CMDLINE_LINUX. Then, run the following command to reload the GRUB configuration:
grub2-mkconfig -o /boot/grub2/grub.cfg
- Create a persistent rule file.
Create a file 70-persistent-net.rules in /etc/udev/rules.d/ and add the following content to the file:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="eth0" - Restart the node.
Alternatively, you can use a script to change a network interface name. Enter the original and new network interface names as prompted when the script is executed. After the execution is complete, restart the system. The new name will be applied.
#!/bin/bash
function check_networkcard() {
while true; do
echo "Enter the original network interface name (for example, ens33):"
read interface_name
if [[ -z "$interface_name" ]]; then
echo "The input cannot be empty."
continue
fi
ifconfig -a | grep -q "$interface_name"
if [[ $? -eq 0 ]]; then
break
else
echo "Network interface $interface_name cannot be found. Enter another one."
fi
done
}
function modify_grub() {
sed -i 's/resume/net.ifnames=0 biosdevname=0 &/' /etc/sysconfig/grub
grub2-mkconfig -o /boot/grub2/grub.cf
}
function modify_adaptername() {
while true; do
echo "Enter a new network interface name (for example, eth0):"
read new_interface_name
if [[ -z "$new_interface_name" ]]; then
echo "The input cannot be empty."
continue
else
break
fi
done
cd /etc/sysconfig/network-scripts/
if [[ -e ifcfg-$interface_name ]]; then
mv ifcfg-$interface_name ifcfg-$new_interface_name
sed -i "s/$interface_name/$new_interface_name/g" ifcfg-$new_interface_name
else
sudo cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-$new_interface_name
DEFROUTE=yes
BOOTPROTO=dhcp
NAME="$new_interface_name"
DEVICE="$new_interface_name"
ONBOOT=yes
EOF
fi
}
function reload_network() {
mac=$(ip link show $interface_name | awk '/ether/{print $2}')
echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$mac\", NAME=\"$new_interface_name\"" >> /etc/udev/rules.d/70-persistent-net.rules
}
function clear_variable() {
unset interface_name
unset interface_cardname
unset mac
}
function main() {
check_networkcard
modify_adaptername
modify_grub
reload_network
clear_variable
echo "The network interface has been renamed. Reboot the node to make the new name take effect."
}
main
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot