2014-08-19

前言
Ubuntu安装完毕之后,如果发生异常宕机,可能导致网卡的逻辑名出现异常,或者和之前的不一致。如果你的应用处理是依据逻辑名,那么有可能数据会传输到非预期的网卡接口上去,笔者今天要整理的是Ubuntu14.04的网卡逻辑名修改,笔者自己把这叫做”网卡固话“---网卡逻辑名固定化。

查看已启用的网卡

root@ubuntu:~# ifconfig
eth0      Link encap:Ethernet  HWaddr fa:16:3e:40:11:12  
          inet addr:172.18.0.9  Bcast:172.18.0.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fe40:1112/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1400  Metric:1
          RX packets:1327966 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22911 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:170759301 (170.7 MB)  TX bytes:1282555 (1.2 MB)
  
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:65536  Metric:1
          RX packets:2725810 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2725810 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:295593236 (295.5 MB)  TX bytes:295593236 (295.5 MB)
  
#上述HWaddr后面为eth0接口的MAC地址
#查看已有逻辑名
root@ubuntu:~# ls /sys/class/net/
eth0  lo
  
#查看指定网卡MAC地址
root@ubuntu:~# ifconfig eth0             
eth0      Link encap:Ethernet  HWaddr fa:16:3e:40:11:12  
          inet addr:172.18.0.9  Bcast:172.18.0.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fe40:1112/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1400  Metric:1
          RX packets:1328066 errors:0 dropped:0 overruns:0 frame:0
          TX packets:23012 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:170767973 (170.7 MB)  TX bytes:1293097 (1.2 MB)
  
ifconfig <network-logicalname>

生成配置文件

root@ubuntu:~# export INTERFACE="eth0"
root@ubuntu:~# export MATCHADDR="fa:16:3e:40:11:12"
root@ubuntu:~# /lib/udev/write_net_rules 
root@ubuntu:~# ls /etc/udev/rules.d/
70-persistent-net.rules  README

首先引入两个变量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}=="fa:16:3e:40:11:12", KERNEL=="eth*", NAME="eth0"
  
#修改后如下
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="fa:16:3e:40:11:12", NAME="new-logicalname"

禁用源网卡逻辑名规则文件

root@ubuntu:/etc/udev/rules.d# cd /lib/udev/rules.d/
root@ubuntu:/lib/udev/rules.d# mv 75-persistent-net-generator.rules 75-persistent-net-generator.rules.disabled

修改网卡配置

#原网卡配置
root@ubuntu:~# vim /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 dhcp
  
# 修改为如下
# The primary network interface
auto new-logicalname
iface new-logicalname inet dhcp
#不需要重启网卡,直接重启系统

重启主机,查看新的网卡逻辑名

root@ubuntu:~# ls /sys/class/net/
lo  new-logicalname
root@ubuntu:~# ifconfig new-logicalname
new-logicalname Link encap:Ethernet  HWaddr fa:16:3e:40:11:12  
          inet addr:172.18.0.9  Bcast:172.18.0.255  Mask:255.255.255.0
          inet6 addr: fe80::f816:3eff:fe40:1112/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1400  Metric:1
          RX packets:399 errors:0 dropped:0 overruns:0 frame:0
          TX packets:357 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:36646 (36.6 KB)  TX bytes:55027 (55.0 KB)
#这样以后所有的配置都可以使用新的网卡逻辑名。