Linux的網卡由eth0變成了eth1,如何修復?

前言:

使用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網卡的狀態了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章