Linux step-by-step tutorial on how to change the network card name

Table of contents

1. Why do you need to modify the network card name?

2. Redhat series

三、Freedom

4. Temporarily modify the network card name

1. Why do you need to modify the network card name?

In early Linux systems, network card names were often named according to the traditional naming methods eth0, eth1, and eht2.

For example, before CentOS 6, network interfaces were named with consecutive numbers: eth0, eth1, etc. However, if new hardware devices are added, they may also be recognized as eth0, eth1, etc.

Starting from CentOS 7, the naming rules for network card devices have been changed. Network card names are generated based on hardware, such as ens33, ens160, etc. This can ensure that the network card names are stable and unique, but in a batch environment, there is no way to unify them.

For batch management and script versatility considerations. In some cases, it is necessary to change the new network card naming rules to the traditional naming method, that is, change the names of ens33, ens160, etc. to eth0, eth1, etc.

The following will introduce you to the Redhat series and how to modify the network card name in Ubuntu.

2. Redhat series

Change the network card name from ens160 to eth0 :

Step 1: Modify the /etc/default/grub configuration file and add net.ifnames=0 after the line GRUB_CMDLINE_LINUX . The purpose of adding this parameter is to disable the operating system's rules for generating network card names based on hardware and use traditional network card names for naming.

#使用vim工具编辑grub文件内容
vim /etc/default/grub

 Step 2: Modify the network card configuration file under /etc/sysconfig/network-scripts . The naming of these files generally  consists of ifcfg- + "network card name" . So we need to rename the file ifcfg-ens160 to ifcfg-eth0 . In addition, we also need to change the information related to ens160 in ifcfg-ens160 to eth0 .


#切换到network-scripts目录下
cd /etc//etc/sysconfig/network-scripts

#将ifcfg-ens160重命名为ifcfg-eth0
mv ifcfg-ens160 ifcfg-eth0

#将ifcfg-ens160中有关ens160有关的信息(name和device)改为eth0
vim ifcfg-eth0

 Step 3 : Determine whether the operating system is a system booted based on UEFI mode or a system booted based on BIOS .

#输入此命令判断系统是基于哪个模式引导的
#这个命令翻译一下就是:判断/sys/firmware/efi这个文件是否存在,若存在属于UEFI,反之输出BIOS
#基于UEFI模式引导的系统,在/sys/firmware/下会有一个efi文件
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS

Step 4 : Systems booted in different modes use different commands to re-read the configuration file.

#基于UEFI模式引导的系统 
grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg


#基于BIOS模式引导的系统 
grub2-mkconfig -o /boot/grub2/grub.cfg

Step 5 : Reboot to restart the server

After the restart is complete, you can view the name of the network card through the ip a command.

三、Freedom

Change the network card name from ens33 to eth0:

Step 1 : Modify the /etc/default/grub configuration file and add net.ifnames=0 to the GRUB_CMDLINE_LINUX line .

vim /etc/default/grub

 Step 2 : Modify the /etc/netplan/00-installer-config.yaml network card configuration file and change ens33 to eth0.

vim /etc/netplan/00-installer-config.yaml

Steps 3 and 4 refer to Redhat.

grub2-mkconfig -o /etc/grub2.cfg

Step 5 : Reboot.

4. Temporarily modify the network card name

#将网卡关闭
ip link set ens160 down

#临时更改网卡名称,服务器重启后网卡名称会还原
ip link set ens160 name eth0

#将网卡打开
ip link set eth0 up

Guess you like

Origin blog.csdn.net/qq_54381110/article/details/131096872