在虚拟机多网卡的环境中,有时IP会随着网卡的重新启动变得不一致,这时,就需要将IP地址和MAC进行绑定,有时也需要对网卡名称进行修改,本篇将对此进行演示。
一 软件环境
- VMware® Workstation 10.0.6
- CentOS-6.5-x86_64
二 修改MAC名称
1、查看网卡信息
[root@strong ~]# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:7F:F9:AA
inet addr:10.24.32.112 Bcast:10.24.33.255 Mask:255.255.254.0
inet6 addr: fe80::20c:29ff:fe7f:f9aa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4097746 errors:0 dropped:0 overruns:0 frame:0
TX packets:2003168 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4086085351 (3.8 GiB) TX bytes:336689861 (321.0 MiB)
eth2 Link encap:Ethernet HWaddr 00:0C:29:7F:F9:B4
inet addr:192.168.226.128 Bcast:192.168.226.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7f:f9b4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:17 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:806 (806.0 b) TX bytes:1978 (1.9 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:1176037 errors:0 dropped:0 overruns:0 frame:0
TX packets:1176037 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:493455702 (470.5 MiB) TX bytes:493455702 (470.5 MiB)
[root@strong ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
IPADDR=10.24.32.112
PREFIX=23
GATEWAY=10.24.33.254
DNS1=10.24.2.33
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=eth0
UUID=6bbe4c97-0f50-4c78-9be3-3242bbab17df
ONBOOT=yes
LAST_CONNECT=1523246127
[root@strong ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.226.128
PREFIX=24
GATEWAY=192.168.226.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=eth2
UUID=18f03116-45c9-4456-a107-260702fa518d
ONBOOT=yes
LAST_CONNECT=1523246140
[root@strong ~]#
注:以上显示发现网卡名称和实际的配置不一样。
2、查看网卡名称对应的规则配置
[root@strong ~]# cat /etc/udev/rules.d/70-persistent-net.rules
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:cc:d9:44", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7f:f9:aa", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7f:f9:b4", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
[root@strong ~]#
注:通过上面的显示得知,MAC为00:0C:29:7F:F9:AA 对应的IP相对的网卡名称应为eth0,而mac为00:0c:29:cc:d9:44的这条记录没有对应的网卡(该记录是虚拟机复制产生的)。
3、修改网卡名称
[root@strong ~]# cat /etc/udev/rules.d/70-persistent-net.rules
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7f:f9:aa", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7f:f9:b4", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
[root@strong ~]#
4、重启,规则生效
[root@strong ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:7F:F9:AA
inet addr:10.24.32.112 Bcast:10.24.33.255 Mask:255.255.254.0
inet6 addr: fe80::20c:29ff:fe7f:f9aa/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15032 errors:0 dropped:0 overruns:0 frame:0
TX packets:338 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1265597 (1.2 MiB) TX bytes:17899 (17.4 KiB)
eth2 Link encap:Ethernet HWaddr 00:0C:29:7F:F9:B4
inet addr:192.168.226.128 Bcast:192.168.226.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7f:f9b4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:468 (468.0 b)
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:1865 errors:0 dropped:0 overruns:0 frame:0
TX packets:1865 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:118122 (115.3 KiB) TX bytes:118122 (115.3 KiB)
三 IP和MAC绑定
1、修改网卡信息
[root@strong ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
IPADDR=10.24.32.112
HWADDR=00:0C:29:7F:F9:AA --增加MAC
PREFIX=23
GATEWAY=10.24.33.254
DNS1=10.24.2.33
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=eth0
UUID=6bbe4c97-0f50-4c78-9be3-3242bbab17df
ONBOOT=yes
LAST_CONNECT=1523246127
2、重启网卡
[root@strong ~]# service network restart
Shutting down interface eth0: Device state: 3 (disconnected)
[ OK ]
Shutting down interface eth2: Device state: 3 (disconnected)
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6
[ OK ]
Bringing up interface eth2: Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/7
[ OK ]
[root@strong ~]#
综上,实现了网卡名称的修改和IP和MAC地址的绑定。