Changing Network Interface Names to eth0 and eth1 in Linux Systems

Warm Reminder

Reading on WeChat public account may result in poor code formatting, incorrect formatting after copying code, outdated content, and advertisement issues. It is strongly recommended to click the Read the Original link at the bottom left of the article for viewing.

Tips

Changing Network Interface Names to eth0 and eth1 in Linux Systems

In Linux, you can change the network interface names to traditional naming conventions such as <span>eth0</span> and <span>eth1</span> by following these steps:

Method 1: Modify GRUB Configuration and Create Udev Rules

1.Modify the GRUB Configuration File: Open the <span>/etc/default/grub</span> file and add the following content to the <span>GRUB_CMDLINE_LINUX</span> line:

net.ifnames=0 biosdevname=0

Execute the following commands:

# For Rocky, Almalinux, CentOS, openEuler, AnolisOS, OpenCloudOS, Kylin Server, Uos Server
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0 biosdevname=0"@' /etc/default/grub

# For Ubuntu, Debian
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"[email protected]=0 biosdevname=0"@' /etc/default/grub

Then update the GRUB configuration

# For Rocky, Almalinux, CentOS, openEuler, AnolisOS, OpenCloudOS, Kylin Server, Uos Server
grub2-mkconfig -o /boot/grub2/grub.cfg
   
# For Ubuntu, Debian
grub-mkconfig -o /boot/grub/grub.cfg

If it is a UEFI boot system, the update command is:

# For Rocky
grub2-mkconfig -o /boot/efi/EFI/rocky/grub.cfg

# For Almalinux
grub2-mkconfig -o /boot/efi/EFI/almalinux/grub.cfg

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

# For openEuler
grub2-mkconfig -o /boot/efi/EFI/openEuler/grub.cfg

# For AnolisOS
grub2-mkconfig -o /boot/efi/EFI/anolis/grub.cfg

# For OpenCloudOS
grub2-mkconfig -o /boot/efi/EFI/opencloudos/grub.cfg

# For Kylin Server
grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg

# For Uos Server
grub2-mkconfig -o /boot/efi/EFI/uos/grub.cfg

# For Ubuntu
grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfg

# For Debian
grub-mkconfig -o /boot/efi/EFI/debian/grub.cfg

For UEFI boot systems, you can directly execute the following command:

EFI_DIR=`find /boot/efi/ -name "grub.cfg" | awk -F"/" '{print $5}'`

# For Rocky, Almalinux, CentOS, openEuler, AnolisOS, OpenCloudOS, Kylin Server, Uos Server
grub2-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg

# For Ubuntu, Debian
grub-mkconfig -o /boot/efi/EFI/${EFI_DIR}/grub.cfg

2.Create Udev Rule File: Create the <span>/etc/udev/rules.d/10-network.rules</span> file and add the following content:

SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="<Network Interface MAC Address>", NAME="eth0"

Replace <span><Network Interface MAC Address></span> with the actual MAC address of the network interface.

[root@anolis8 ~]# ip addr
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:c5:38:3b brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 172.31.7.15/21 brd 172.31.7.255 scope global dynamic noprefixroute ens33
       valid_lft 1590sec preferred_lft 1590sec
    inet6 fe80::20c:29ff:fec5:383b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

# Execute the following on Rocky 9, Almalinux 9, CentOS 9 and 10, AnolisOS 8 and 23, OpenCloudOS 9 systems
[root@anolis8 ~]# cat >> /etc/udev/rules.d/10-network.rules << EOF
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="00:0c:29:c5:38:3b", NAME="eth0"
EOF

# You can also execute the following command
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
ETHMAC=`ip addr show ${ETHNAME} | awk -F' ' '/ether/{print $2}'`
cat >> /etc/udev/rules.d/10-network.rules << EOF
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${ETHMAC}", NAME="eth0"
EOF

3.Modify Network Interface Configuration File: Create the <span>/etc/sysconfig/network-scripts/ifcfg-eth0</span> file and configure the network interface parameters, for example:

# Execute the following on Rocky 9, Almalinux 9, CentOS 9 and 10, AnolisOS 23, OpenCloudOS 9 systems
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<-EOF
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=172.31.0.9
NETMASK=255.255.248.0
GATEWAY=172.31.0.2
DNS1=223.5.5.5
DNS2=114.114.114.114
EOF

# Execute the following on Rocky 8, Almalinux 8, CentOS 7 and 8, AnolisOS 8, OpenCloudOS 8, Kylin Server v10 and Uos Server v20 systems
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i.bak 's/${ETHNAME}/eth0/' /etc/sysconfig/network-scripts/ifcfg-eth0

# For Ubuntu 22.04 and 24.04
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/${ETHNAME}/eth0/' /etc/netplan/50-cloud-init.yaml

# For Ubuntu 20.04
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/${ETHNAME}/eth0/' /etc/netplan/00-installer-config.yaml

# For Ubuntu 18.04
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/${ETHNAME}/eth0/' /etc/netplan/01-netcfg.yaml

# For Debian
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
sed -i.bak 's/${ETHNAME}/eth0/' /etc/network/interfaces

4.Reboot the System:

reboot

After rebooting, the network interface name will change to <span>eth0</span>.

Method 2: Use systemd Link Files

This method is only suitable for Rocky Linux 9, Almalinux 9, CentOS Stream 9 and 10, AnolisOS 23, OpenCloudOS 9 systems.

1.Create systemd Link File: Create the <span>/etc/systemd/network/70-eth0.link</span> file with the following content:

[Match]
MACAddress=<Network Interface MAC Address>

[Link]
Name=eth0

Replace <span><Network Interface MAC Address></span> with the actual MAC address of the network interface.

[root@rocky9 ~]# mkdir -p /etc/systemd/network/
[root@rocky9 ~]# touch /etc/systemd/network/70-eth0.link

[root@rocky9 ~]# ip addr
1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160:mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:f8:60:8f brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 172.31.7.16/21 brd 172.31.7.255 scope global dynamic noprefixroute ens160
       valid_lft 1791sec preferred_lft 1791sec
    inet6 fe80::20c:29ff:fef8:608f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

[root@rocky9 ~]# cat > /etc/systemd/network/70-eth0.link << EOF
[Match]
MACAddress=00:0c:29:f8:60:8f

[Link]
Name=eth0
EOF

2.Modify NetworkManager Configuration File: If using NetworkManager to manage the network, you need to modify the configuration file under <span>/etc/NetworkManager/system-connections</span>, for example:

mv /etc/NetworkManager/system-connections/OriginalNetworkInterfaceName.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection

Then edit the file to change <span>interface-name</span> to <span>eth0</span>.

[root@rocky9 ~]# ls /etc/NetworkManager/system-connections/ens160.nmconnection 
/etc/NetworkManager/system-connections/ens160.nmconnection
[root@rocky9 ~]# mv /etc/NetworkManager/system-connections/ens160.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection

[root@rocky9 ~]# sed -i.bak 's/ens160/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection

3.Reboot the System:

reboot

After rebooting, the network interface name will change to <span>eth0</span>.

You can execute the following script:

[root@rocky9 ~]# cat rename_eth.sh 
#!/bin/bash
ETHNAME=`ip addr | awk -F"[ :]" '/^2/{print $3}'`
ETHMAC=`ip addr show ${ETHNAME} | awk -F' ' '/ether/{print $2}'`

mkdir /etc/systemd/network/
touch /etc/systemd/network/70-eth0.link
cat > /etc/systemd/network/70-eth0.link << EOF
[Match]
MACAddress=${ETHMAC}

[Link]
Name=eth0
EOF

mv /etc/NetworkManager/system-connections/${ETHNAME}.nmconnection /etc/NetworkManager/system-connections/eth0.nmconnection
sed -i.bak 's/${ETHNAME}/eth0/' /etc/NetworkManager/system-connections/eth0.nmconnection

Notes

  • After changing the network interface name, you may need to delete the configuration files of the old network interface to avoid conflicts.
  • If there are multiple network interfaces in the system, you can create corresponding Udev rules or systemd link files for each network interface in a similar manner.

WeChat Group

To facilitate better communication regarding operation and maintenance and related technical issues, a WeChat group has been created. Friends who want to join the group can scan the QR code below to add me as a friend (please note: join group).

Changing Network Interface Names to eth0 and eth1 in Linux Systems

Blog

CSDN Blog: https://blog.csdn.net/qq_25599925

Changing Network Interface Names to eth0 and eth1 in Linux Systems

Juejin Blog: https://juejin.cn/user/4262187909781751

Changing Network Interface Names to eth0 and eth1 in Linux Systems

Long press to recognize the QR code to visit the blog website for more quality original content.

Leave a Comment