Linux 中修改网卡名称【ubuntu + Centos7】
有时候安装好Linux系统之后,网卡的名称不是ethX, 这时候可以通过以下方式修改网卡的名称为ethX(以eth0为例)。
ubuntu 14.04
1. 查看是否存在/etc/udev/rules.d/70-persistent-net.rules 文件,如不存在,可以通过以下方式生成(或者从其它机器上拷贝一个)
# ifconfig em1 Link encap:以太网 硬件地址 18:66:da:7e:64:10 inet 地址:192.168.2.38 广播:192.168.2.255 掩码:255.255.255.0 inet6 地址: fe80::1a66:daff:fe7e:6410/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:328617 错误:0 丢弃:7944 过载:0 帧数:0 发送数据包:80911 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:35848013 (35.8 MB) 发送字节:11969732 (11.9 MB) 中断:74 Memory:95000000-957fffff # export INTERFACE="em1" # export MATCHADDR="18:66:da:7e:64:10" # /lib/udev/write_net_rules
这时,会生成/etc/udev/rules.d/70-persistent-net.rules文件,文件内容如下:
# 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. SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="18:66:da:7e:64:10", KERNEL=="em*", NAME="em1"
注: 如果没有 /lib/udev/write_net_rules 这个命令,直接创建这个文件也行,文件内容直接使用上述的内容,修改以下网卡MAC地址即可。
2. 修改/etc/udev/rules.d/70-persistent-net.rules 配置文件
# vim /etc/udev/rules.d/70-persistent-net.rules 将 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="18:66:da:7e:64:10", KERNEL=="em*", NAME="em1" 修改为 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="18:66:da:7e:64:10", NAME="eth0"
其实也就是改了一下名称。
3. 修改网卡文件 /etc/network/inerface
# vim /etc/network/inerface auto lo iface lo inet loopback auto em1 iface em1 inet static .... 修改为 auto lo iface lo inet loopback auto eth0 iface eth0 inet static ....
4. reboot 重启服务器
Centos 7.X
环境描述如下:
# ifconfig ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet xxxx netmask 255.255.255.0 broadcast 172.25.20.255 inet6 fe80::250:56ff:fea2:ad1e prefixlen 64 scopeid 0x20<link> ether 00:50:56:a2:ad:1e txqueuelen 1000 (Ethernet) RX packets 88179004 bytes 10363590861 (9.6 GiB) RX errors 0 dropped 4 overruns 0 frame 0 TX packets 49127112 bytes 4003428543 (3.7 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
目标: 将ens160网卡名称修改为 eth0
操作步骤如下:
1. 修改/usr/lib/udev/rules.d/60-net.rules 配置文件
# cat /usr/lib/udev/rules.d/60-net.rules ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", PROGRAM="/lib/udev/rename_device", RESULT=="?*", NAME="$result" 修改为 ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", PROGRAM="/lib/udev/rename_device", ATTR{address}=="00:50:56:a2:ad:1e", NAME="eth0"
【网卡MAC为上述ifconfig看到的MAC地址】
2. 修改网卡配置文件
# cd /etc/sysconfig/network-scripts/ # mv ifcfg-ens160 ifcfg-eth0 # vim ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 DEVICE=eth0 ONBOOT=yes .....
3. 重启系统
# reboot