1. 程式人生 > >Linux的網絡卡由eth0變成了eth1,如何修復?

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