Linux下修改网卡接口名
by:授客 QQ:1033553122
(测试环境:CentOS-6.0-x86_64-bin-DVD1.iso+Vmware)
作用
可以用于解决类似如下Device not found的问题:
# ifconfig eth1
eth1: error fetching interface information: Device not found
例1:把网卡接口名从eth0改为eth1
#修改网卡接口名之前的相关信息如下
# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:F3:8B:8A
inet addr:172.25.75.4 Bcast:172.25.75.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef3:8b8a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:472 errors:0 dropped:0 overruns:0 frame:0
TX packets:95 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:42666 (41.6 KiB) TX bytes:11536 (11.2 KiB)
# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:0C:29:F3:8B:8A
inet addr:172.25.75.4 Bcast:172.25.75.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef3:8b8a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:503 errors:0 dropped:0 overruns:0 frame:0
TX packets:106 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:46425 (45.3 KiB) TX bytes:13230 (12.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:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
# cat /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) (custom name provided by external tool)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:f3:8b:8a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
步骤1.修改网卡接口配置文件
#把eth0对应的配置文件ifcfg-eth0重命名为ifcfg-eth1--如要设置网卡接口名为ethN,则重命名为ifcfg-ethN
# cd
/etc/sysconfig/network-scripts/
# mv ifcfg-eth0
ifcfg-eth1
说明:如果网卡接口名为ethN,则对应的配置文件为ifcfg-ethN
#修改ifcfg-eth1配置文件,把DEVICE="eth0"改成DEVICE="eth1"
# vi ifcfg-eth1
DEVICE="eth1"--如要设置网卡接口名为ethN,则此处DEVICE="ethN"
BOOTPROTO=static
HADDR="00:0c:29:f3:8b:8a"
IPADDR=172.25.75.4
NETMASK=255.255.255.0
NETWORK=172.25.75.0
NM_CONTROLLED="yes"
ONBOOT="yes"
GATEWAY=172.25.75.254
步骤2.修改70-persistent-net.rules配置文件
修改70-persistent-net.rules文件,找到包含原始MAC地址的记录,把NAME="eth0"改成NAME="eth1"
# 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) (custom name provided by external tool)
SUBSYSTEM=="net",
ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:f3:8b:8a",
ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
--如要设置网卡接口名为ethN,则此处为NAME="ethN"
注意:
1.如果存在多条仅NAME的值不一样的记录,则删除多余的记录,仅保留一条记录
2.如果有对网卡MAC地址进行修改过,即网卡mac地址并非原始mac地址,则推荐步骤2之前进行如下操作:
70-persistent-net.rules文件:
# rm
/etc/udev/rules.d/70-persistent-net.rules
#
reboot
说明:重启后,系统自动重新生成70-persistent-net.rules文件,里面记录中的包含的MAC地址为对应网卡的原始MAC地址
步骤3.重启系统
# reboot
注意:此处# service network
restart
命令不起作用,必须重启
步骤4.验证是否已经修改
如下,网卡接口名已经变成eth1
[root@localhost ~]#
ifconfig eth1
eth1 Link
encap:Ethernet HWaddr
00:0C:29:F3:8B:8A
inet addr:172.25.75.4
Bcast:172.25.75.255
Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef3:8b8a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX
packets:45 errors:0 dropped:0 overruns:0 frame:0
TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4693 (4.5 KiB) TX bytes:7766 (7.5
KiB)
[root@localhost ~]#
ifconfig -a
eth1
Link
encap:Ethernet HWaddr
00:0C:29:F3:8B:8A
inet addr:172.25.75.4
Bcast:172.25.75.255
Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fef3:8b8a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX
packets:73 errors:0 dropped:0 overruns:0 frame:0
TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:7091 (6.9 KiB) TX bytes:9004 (8.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:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0
b)
# 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) (custom name provided by external tool)
SUBSYSTEM=="net",
ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:f3:8b:8a",
ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"