42

Linux修改网卡名称

 5 years ago
source link: https://www.linuxprobe.com/linux-modifying-the-network-card.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
导读 网卡,即网络接口板,又称网络适配器或NIC (网络接口控制器),是一块被设计用来允许计算机在计算机网络上进行通讯的计算机硬件。由于其拥有MAC地址,因此属于OSI模型的第1层。它使得用户可以透过电缆或无线相互连接。 每一个网卡都有一个被称为MAC地址的独一无二的48位串行号,它被写在卡上的一块ROM中。
一、环境

VirtualBox + CentOS6.5

二、问题

有时候在克隆服务器之后配置网络时,或者在维护别人建好的服务器时,会遇到这样一种情况。如下图所示:

即:在接口配置文件ifcfg-eth0中,配置的是网卡eth1的信息。

这种不一致有可能对强迫症造成一定的困扰,有时候我们更想要的是:在ifcfg-eth0文件中,配置的应该是网卡eth0的信息,而网卡eth1,则应该是在ifcfg-eth1中进行配置。

三、解决办法

要解决上面的问题,大致有两种方法:

1) 将接口配置文件的文件名由ifcfg-eth0修改为ifcfg-eth1;

2) 将网卡eth1的名称修改为eth0.

方法一:

该方法实测是可行的,如下图所示,修改完之后直接重启网络即可。

方法二:

这种方法的直观感觉是直接在ifcfg-eth0配置文件中将网卡名称由eth1修改为eth0,但是这样改的话,重启网络会出现Device eth0 does not seem to be present即:设备 eth0 不存在的错误,如下图所示,该问题的解决参考《L08-Linux解决Device eth0 does not seem to be present,delaying initialization问题》 。

所以还不能简单粗暴的这么改,因为,正如下图由ifconfig –a命令查询到的,服务器所识别的网卡依然是eth1,而不是eth0。

注:记住这里的mac地址08:00:27:74:39:02,在后面的实施步骤中可作为修改的依据,因为一台服务器的IP可以随意改变,但是mac地址是唯一的。

为了解决这个问题,我们先修改/etc/udev/rules.d/70-persistent-net.rules文件中的配置,使得服务器的网卡名称由eth1变为eth0,然后,我们再将ifcfg-eth0中的网卡名也跟着改过来,最后重启网络。

1) 修改配置文件/etc/udev/rules.d/70-persistent-net.rules。

原/etc/udev/rules.d/70-persistent-net.rules文件内容如下:

# This file was automatically generated by the /lib/udev/write_net_rules

# program, run by the persistent-net-generator.rules rules file.

#

# You can modify it, as long as you keep each rule on a single

# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100e (e1000)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:9b:bb:3c", ATTR{type}=="1

", KERNEL=="eth*", NAME="eth0"

 
# PCI device 0x8086:0x100e (e1000)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:74:39:02", ATTR{type}=="1

", KERNEL=="eth*", NAME="eth1"

修改后的内容如下:

# This file was automatically generated by the /lib/udev/write_net_rules

# program, run by the persistent-net-generator.rules rules file.

#

# You can modify it, as long as you keep each rule on a single

# line, and change only the value of the NAME= key.

  
# PCI device 0x8086:0x100e (e1000)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:74:39:02", ATTR{type}=="1

", KERNEL=="eth*", NAME="eth0"

如上面所示

a)将原文件中的eth0的配置删掉了(因为它没用了,这项配置是我本次测试时克隆本服务器时源机器的配置,配置中对应的mac地址也是源服务器的mac地址,对我们目前这台服务器而言没用);

b)将eth1网卡名称修改为了eth0(可注意到它的mac地址为08:00:27:74:39:02,恰恰就是我们要修改的条目。)

2) 修改接口配置文件ifcfg-eth0,将网卡由eth1修改为eth0

3) 重启机器,让服务器根据/etc/udev/rules.d/70-persistent-net.rules文件重新配置网卡信息

可以发现,重启机器之后一进入服务器,网络就已经配置成一致的了,如下图:

至此,修改完毕。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK