VMware Workstation中Linux虛擬機器克隆後的網路配置

stonebox1122發表於2015-03-01

 

1 現象與原因

VMware WorkstationLinux模版虛擬機器網路連線採用NAT模式,克隆後檢視網路設定與對應的配置檔案,發現不一致,具體如下:

[root@localhost ~]# ifconfig -a

eth1      Link encap:Ethernet  HWaddr 00:0C:29:B5:A8:81 

          inet addr:192.168.230.139  Bcast:192.168.230.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:feb5:a881/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:134 errors:0 dropped:0 overruns:0 frame:0

          TX packets:70 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:34006 (33.2 KiB)  TX bytes:7979 (7.7 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:16436  Metric:1

          RX packets:8 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

 

[root@localhost ~]# ls /etc/sysconfig/network-scripts/ifcfg*

/etc/sysconfig/network-scripts/ifcfg-eth0

/etc/sysconfig/network-scripts/ifcfg-lo

原因如下:redhat使用udev動態管理裝置檔案,並根據裝置的資訊對其進行持久化命名。udev會在系統引導的過程中識別網路卡,將mac地址和網路卡名稱對應起來記錄在udev的規則指令碼中。而對於新的虛擬機器,VMware會自動為虛擬機器的網路卡生成MAC地址,當你克隆或者重灌虛擬機器軟體時,由於你使用的是以前系統虛擬硬碟的資訊,而該系統中已經有eth0的資訊,對於新增的網路卡,udev會自動將其命名為eth1(累加的原則),所以在你的系統啟動後,你使用ifconfig看到的網路卡名為eth1。這時候在/etc/sysconfig/network-script/下依然是eth0的配置檔案,重啟網路就會出現如下問題:

[root@localhost ~]# /etc/init.d/network restart

Shutting down loopback interface:                          [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:  Device eth0 does not seem to be present, delaying initialization.

                                                           [FAILED]

 

2 解決方法

1)備份配置檔案

[root@localhost ~]# cp /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.bak

 

2)修改/etc/udev/rules.d/70-persistent-net.rules,刪除eth0有關內容,修改eth1eth0, 記錄下這個新eth0mac地址 

修改前/etc/udev/rules.d/70-persistent-net.rules的內容如下:

[root@localhost ~]# cat /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:f6:7c:14", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

 

# PCI device 0x8086:0x100f (e1000)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:b5:a8:81", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

使用vim修改/etc/udev/rules.d/70-persistent-net.rules

[root@localhost ~]# vim /etc/udev/rules.d/70-persistent-net.rules

修改後/etc/udev/rules.d/70-persistent-net.rules內容如下:

[root@localhost ~]# cat /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)

 

# PCI device 0x8086:0x100f (e1000)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:b5:a8:81", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

 

3)修改/etc/sysconfig/network-scripts/ifcfg-eth0,修改其中的mac地址為步驟(2)中記錄的地址。

修改前/etc/sysconfig/network-scripts/ifcfg-eth0內容如下:

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"

HWADDR="00:0C:29:F6:7C:14"

NM_CONTROLLED="yes"

ONBOOT="no"

使用vim修改/etc/sysconfig/network-scripts/ifcfg-eth0

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

修改後/etc/sysconfig/network-scripts/ifcfg-eth0內容如下:

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE="eth0"

HWADDR="00:0c:29:b5:a8:81"

NM_CONTROLLED="yes"

ONBOOT="yes"

 

4)重啟後恢復正常。

[root@localhost ~]# init 6

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-1444892/,如需轉載,請註明出處,否則將追究法律責任。

相關文章