原文鏈接:http://blog.csdn.net/nnuljl/article/details/38680107
1.前言
Ubuntu安裝完畢之后,如果發生異常宕機,可能導致網卡的邏輯名出現異常,或者和之前的不一致。如果你的應用處理是依據邏輯名,那么有可能數據會傳輸到非預期的網卡接口上去,筆者今天要整理的是Ubuntu14.04的網卡邏輯名修改,筆者自己把這叫做”網卡固話“---網卡邏輯名固定化。
2.網卡信息查詢
查看已啟用的網卡
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地址
查看已有邏輯名
[python] view plain copy
root@ubuntu:~# ls /sys/class/net/
eth0 lo
查看指定網卡MAC地址
[python] view plain copy
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)
[python] view plain copy
ifconfig <network-logicalname>
3.生成配置文件
[python] view plain copy
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值。
[python] view plain copy
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"
修改后如下
[python] view plain copy
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="fa:16:3e:40:11:12", NAME="new-logicalname"
4.禁用源網卡邏輯名規則文件
[python] view plain copy
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
5.修改網卡配置
原網卡配置
[python] view plain copy
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
修改為如下
[python] view plain copy
The primary network interface
auto new-logicalname
iface new-logicalname inet dhcp
不需要重啟網卡,直接重啟系統
6.重啟主機,查看新的網卡邏輯名
[python] view plain copy
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)
這樣以后所有的配置都可以使用新的網卡邏輯名。