RHEL7/centos7修改網卡名稱爲eth0

        由於RHEL7中採用新的網卡名稱命名方法,導致網卡名變得難以理解和記憶,本文介紹怎樣將網卡名稱修改爲傳統的eth0的命名方式,可以實現定義任意網卡爲eth0.

網卡重命名過程介紹

要修改網卡名稱,首先我們要了解linux網卡設備重命名的過程,根據紅帽官方文檔介紹,設備重命名的過程如下:

  1. A rule in /usr/lib/udev/rules.d/60-net.rules instructs the udev helper utility,/lib/udev/rename_device,to look into all /etc/sysconfig/networkscripts/ifcfg-suffix files.If it finds an ifcfg file with a HWADDR entry matching the MAC address of an interface it renames the interface to the name given in the ifcfg file by the DEVICE directive.

  2. A rule in /usr/lib/udev/rules.d/71-biosdevname. rules instructs biosdevname to rename the interface according to its naming policy, provided that it was not renamed in a previous step, biosdevname is installed, and biosdevname=0 was not given as a kernel command on the boot command line.

  3. A rule in /lib/udev/rules.d/75-net-description.rules instructs udev to fill in the internal udev device property values ID_NET_NAME_ONBOARD, ID_NET_NAME_SLOT, ID_NET_NAME_PATH, ID_NET_NAME_MAC by examining the network interface device.Note,that some device properties might be undefined.

  4. A rule in /usr/lib/udev/rules.d/80-net-name-slot.rules instructs udev to rename the interface, provided that it was not renamed in step 1 or 2, and the kernel parameter net.ifnames=0 was not given, according to the following priority: ID_NET_NAME_ONBOARD,ID_NET_NAME_SLOT,ID _NET_NAME_PATH. It falls through to the next in the list, if one is unset. If none of these are set, then the interface will not be renamed.

簡單翻譯如下:

  1. /usr/lib/udev/rules.d/60-net.rules文件中的規則會讓udev幫助工具/lib/udev/renamedevice查看所有/etc/sysconfig/networkscripts/ifcfg-suffix文件。如果發現包含HWADDR條目的ifcfg文件與某個接口的MAC地址匹配,它會將該接口重命名爲ifcfg文件中由DEVICE指令給出的名稱。

  2. /usr/lib/udev/rules.d/71-biosdevname.rules中的規則讓biosdevname根據其命名策略重命名該接口,該命名方法執行的前提是在上一步中沒有重命名該接口,但是已安裝biosdevname、且在boot命令行中將biosdevname=0作爲內核命令給出。

  3. /lib/udev/rules.d/75-net-description.rules中的規則讓udev通過檢查網絡接口設備,填寫內部udev設備屬性值ID_NET_NAME_ONBOARD、ID_NET_NAME_SLOT、ID_NET_NAME_PATH。注:有些設備屬性可能處於未定義狀態。

  4. /usr/lib/udev/rules.d/80-net-name-slot.rules中的規則讓udev重命名該接口,優先順序如下:ID_NET_NAME_ONBOARD、ID_NET_NAME_SLOT、ID_NET_NAME_PATH。也就是說,沒有在步驟1或2中重命名該接口,同時未給出內核參數net.ifnames=0,則會按照該規則進行重命名。按照該規則重命名時,若一個參數未設定,則會按列表的順序設定下一個。如果沒有設定任何參數,則不會重命名該接口。

網卡重命名方法

根據以上介紹,這裏可以按照步驟1中的介紹來進行網卡重命名:

  1. ifconfig 命令查看各網卡MAC地址

[root@test01 ~]# ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 08:00:27:43:c1:1d  txqueuelen 1000  (Ethernet)
        RX packets 233  bytes 43354 (42.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.102  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe56:9530  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:56:95:30  txqueuelen 1000  (Ethernet)
        RX packets 259  bytes 48117 (46.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 8735 (8.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fea5:4fd  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:a5:04:fd  txqueuelen 1000  (Ethernet)
        RX packets 1213  bytes 131145 (128.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 771  bytes 86683 (84.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1220  bytes 105112 (102.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1220  bytes 105112 (102.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

由此可知,網卡MAC地址分別爲:

enp0s3: 08:00:27:43:c1:1d
enp0s8: 08:00:27:56:95:30
enp0s9: 08:00:27:a5:04:fd

這裏將要修改網卡名稱的對應關係如下:

enp0s3: 08:00:27:43:c1:1d --eth0
enp0s8: 08:00:27:56:95:30 --eth1
enp0s9: 08:00:27:a5:04:fd --eth2
# 備註:可以自定義各網卡名稱的對應關係,如將enp0s8修改爲eth2,enp0s9修改爲eth2.
  1. 重命名網卡配置文件

[root@test01 ~]# cd /etc/sysconfig/network-scripts/
[root@test01 network-scripts]# ls ifcfg-*
ifcfg-enp0s3 ifcfg-lo      
[root@test01 network-scripts]# 
# 如果沒有網卡配置文件,可以使用以下命令添加       
[root@test01 network-scripts]# nmcli connection add type ethernet ifname enp0s8 con-name enp0s8 autoconnect yes 
成功添加的連接 'enp0s8'(71306055-0869-4125-955a-4b7959cc9226)。
[root@test01 network-scripts]# nmcli connection add type ethernet ifname enp0s9 con-name enp0s9 autoconnect yes 
成功添加的連接 'enp0s9'(9d5a3033-b478-4076-95b0-83e62aa62cbe)。
[root@test01 network-scripts]# ls ifcfg-*
ifcfg-enp0s3  ifcfg-enp0s8  ifcfg-enp0s9  ifcfg-lo

[root@test01 network-scripts]# mv ifcfg-enp0s3 ifcfg-eth0
[root@test01 network-scripts]# mv ifcfg-enp0s8 ifcfg-eth1
[root@test01 network-scripts]# mv ifcfg-enp0s9 ifcfg-eth2
[root@test01 network-scripts]# 
[root@test01 network-scripts]# ls ifcfg-*
ifcfg-eth0  ifcfg-eth1  ifcfg-eth2  ifcfg-lo
  1. 修改網卡配置文件

修改好配置文件名稱後,修改或向配置文件中添加HWADDR,DEVICE,NAME參數。

[root@test01 network-scripts]# cat ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0          --這裏修改NAME=eth0
UUID=77c7f1b8-ddef-4d96-bf74-68ecedc99a1a
DEVICE=eth0 --這裏修改DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.56.10
PREFIX=24
HWADDR=08:00:27:43:c1:1d  --這裏添加HWADDR

按照以上方法修改所有網卡配置文件。

  1. 拷貝配置文件60-net.rules

[root@test01 network-scripts]# cp /usr/lib/udev/rules.d/60-net.rules /etc/udev/rules.d/
  1. 重啓系統查看網卡名稱

shutdown -r 0

按照以上方法修改,可以自由定義不同MAC地址的網卡對應的網卡名稱,在配置文件中添加完HWADDR的前提下,只需要修改配置文件名稱ifcfg-ethx和其中的DEVICE=ethx,NAME=ethx.

[root@test01 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 08:00:27:43:c1:1d  txqueuelen 1000  (Ethernet)
        RX packets 9  bytes 3788 (3.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.102  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe56:9530  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:56:95:30  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 5825 (5.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21  bytes 4149 (4.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fea5:4fd  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:a5:04:fd  txqueuelen 1000  (Ethernet)
        RX packets 66  bytes 11712 (11.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 63  bytes 10460 (10.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章