您的位置:首页 > 运维架构 > Linux

Linux的网卡由eth0变成了eth1,如何修复?

2017-12-17 13:46 323 查看

前言:

使用VMware安装centos虚拟机,刚开始安装之后网卡eth0是没有问

题的,一段时间没有去使用,后来去开启次虚拟机,网卡名称编程了eth1.

如下图:

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:C1:7E
inet addr:192.168.146.129  Bcast:192.168.146.255  Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe41:c19e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:90 errors:0 dropped:0 overruns:0 frame:0
TX packets:62 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:11379 (11.1 KiB)  TX bytes:7555 (7.3 KiB)

lo        Link encap:Local Loopback
inet addr:127.0.0.1  Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING  MTU:65536  Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)


然后使用service network restart指令来重新启动网络服务时,之前是遇

到没找到此eth0网卡,这次重启网络服务也没报错,重启成功。但是还是要

把默认网卡改成eth0.这里简单的描述一下解决此问题。

为什么eth0会变成eth1?

很多Linux distribution使用udev动态管理设备文件,并根据设备的信息对其进行持久化命名。udev会在系统引导的过程中识别网卡,将mac地址和网卡名称对应起来记录在udev的规则脚本中。而对于新的虚拟机,VMware会自动为虚拟机的网卡生成MAC地址,当你克隆或者重装虚拟机软件时,由于你使用的是以前系统虚拟硬盘的信息,而该系统中已经有eth0的信息,对于这个新的网卡,udev会自动将其命名为eth1(累加的原则),所以在你的系统启动后,你使用ifconfig看到的网卡名为eth1。

如何恢复到eth0?

[root@192 ~]# cd /etc/udev/rules.d/

[root@192 rules.d]# pwd
/etc/udev/rules.d

[root@192 rules.d]# ls
60-fprint-autosuspend.rules  70-persistent-net.rules    98-kexec.rules
60-pcmcia.rules              90-alsa.rules              99-fuse.rules
60-raw.rules                 90-hal.rules
70-persistent-cd.rules       97-bluetooth-serial.rules


[root@192 rules.d]# vi /etc/udev/rules.d/70-persistent-net.rules


此文件详细信息如下:

[root@192 rules.d]# vi /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:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:41:c1:7e", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:41:c1:9e", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"


打开该文件,这时你会发现,里面有eth0,eth1两个网卡的信息,但实际上你ifconfig时只能发现eth1一个网卡的信息,这时因为eth0根本就不存在。

将其中eth0的信息删掉,并将eth1信息中的设备名改为eth0,重启系统,然后输入命令:ifconfig,或者删掉其中所有的信息重启系统udev会帮你发现新的设备的。

另外还有一个启动脚本文件/etc/sysconfig/network-scripts/ifcfg-eth0,该文件中的mac地址为原来eth0网卡的物理地址,而虚拟机为eth1分配新的物理地址,故启动脚本中的信息与实际信息时不匹配的,将MAC的地址信息修改为70-persistent-net.rules中的eth1的MAC地址,再次重启网络,就完全恢复到以前eth0网卡的状态了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: