修改Ubuntu14.10网卡逻辑名实践
Ubuntu安装完毕之后,如果发生异常宕机,可能导致网卡的逻辑名出现异常,或者和之前的不一致。如果你的应用处理是依据逻辑名,那么有可能数据会传输到非预期的网卡接口上去,今天我们看看在Ubuntu14.10 下网卡固话的过程。
网卡信息查询
eth0 Link encap:以太网 硬件地址 52:54:00:09:e2:11 inet 地址:10.0.3.94 广播:10.0.3.255 掩码:255.255.255.0 inet6 地址: fe80::5054:ff:fe09:e211/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:4541 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:1908 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:288707 (288.7 KB) 发送字节:691213 (691.2 KB) lo Link encap:本地环回 inet 地址:127.0.0.1 掩码:255.0.0.0 inet6 地址: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 跃点数:1 接收数据包:2503 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:2503 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:0 接收字节:836481 (836.4 KB) 发送字节:836481 (836.4 KB)上述HWaddr后面为eth0接口的MAC地址: 52:54:00:09:e2:11 查看已有网卡逻辑名:
root@ubuntu1410:~# ls /sys/class/net/ eth0 lo查看指定网卡MAC地址:
eth0 Link encap:以太网 硬件地址 52:54:00:09:e2:11 inet 地址:10.0.3.94 广播:10.0.3.255 掩码:255.255.255.0 inet6 地址: fe80::5054:ff:fe09:e211/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:5512 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:2332 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:351337 (351.3 KB) 发送字节:855606 (855.6 KB)
生成配置文件
root@ubuntu1410:~# export INTERFACE="eth0" root@ubuntu1410:~# export MATCHADDR="52:54:00:09:e2:11" root@ubuntu1410:~# /lib/udev/write_net_rules # 生成命令 root@ubuntu1410:~# ls /etc/udev/rules.d/ 70-persistent-net.rules首先引入两个变量INTERFACE,MATCHADDR,然后执行write_net_rules,查看生成的文件70-persistent-net.rules 文件内容如下,删除KERNEL项,并修改NAME值。
# 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}=="52:54:00:09:e2:11", KERNEL=="eth*", NAME="eth0"修改后:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:09:e2:11", NAME="em0"
禁用源网卡逻辑名规则文件
root@ubuntu1410:/etc/udev/rules.d# cd /lib/udev/rules.d/ root@ubuntu1410:/lib/udev/rules.d# mv 75-persistent-net-generator.rules 75-persistent-net-generator.rules.disabled
修改网卡配置
root@ubuntu1410:~# cat /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 10.0.3.94 gateway 10.0.3.1 netmask 255.255.255.0 dns-nameservers 222.222.222.222修改后:
# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto em0 iface em0 inet static address 10.0.3.94 gateway 10.0.3.1 netmask 255.255.255.0 dns-nameservers 222.222.222.222不需要重启网卡,直接重启系统。
重启后查看新的网卡逻辑名
root@ubuntu1410:~# ls /sys/class/net/ em0 lo root@ubuntu1410:~# ifconfig em0 em0 Link encap:以太网 硬件地址 52:54:00:09:e2:11 inet 地址:10.0.3.94 广播:10.0.3.255 掩码:255.255.255.0 inet6 地址: fe80::5054:ff:fe09:e211/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1 接收数据包:8413 错误:0 丢弃:0 过载:0 帧数:0 发送数据包:3456 错误:0 丢弃:0 过载:0 载波:0 碰撞:0 发送队列长度:1000 接收字节:525779 (525.7 KB) 发送字节:1092548 (1.0 MB) root@ubuntu1410:~#到这里,网卡逻辑名称就修改完成了。