Linux指定网卡名称

udev是Linux(linux2.6内核之后)默认的设备管理工具。udev 以守护进程的形式运行,通过侦听内核发出来的 uevent 来管理 /dev目录下的设备文件。
动态管理:当设备添加 / 删除时,udev 的守护进程侦听来自内核的 uevent,以此添加或者删除 /dev下的设备文件,所以 udev 只为已经连接的设备产生设备文件,而不会在 /dev下产生大量虚无的设备文件。

自定义命名规则:通过 Linux 默认的规则文件,udev 在 /dev/ 里为所有的设备定义了内核设备名称,比如 /dev/sda、/dev/hda、/dev/fd等等。由于 udev 是在用户空间 (user space) 运行,Linux 用户可以通过自定义的规则文件,灵活地产生标识性强的设备文件名,比如 /dev/boot_disk、/dev/root_disk、/dev/color_printer等等。

设定设备的权限和所有者 / 组:udev 可以按一定的条件来设置设备文件的权限和设备文件所有者 / 组

在规则中简单的匹配网卡MAC地址是有意义的,因为它们是唯一的.
查看网卡信息

udevadm info -a -p /sys/class/net/eth0

looking at class device ‘/sys/class/net/eth0’:
KERNEL==“eth0”
ATTR{address}“00:52:8b:d5:04:48”
规则如下:
KERNEL
"eth*", ATTR{address}==“00:52:8b:d5:04:48”, NAME=“lan”

这样就重命名了eth*为lan

a40i有多个网卡,某些场景下需要将具体网卡和外部硬件接口名称(或丝印名称)对应起来,配置示例:
首先查看网卡信息:

[root@a40i]:~$:udevadm info -a -p /sys/class/net/eth0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/soc.0/1c0b000.eth/net/eth0':
    KERNEL=="eth0"
    SUBSYSTEM=="net"
    DRIVER==""
    ATTR{mtu}=="1500"
    ATTR{type}=="1"
    ATTR{netdev_group}=="0"
    ATTR{flags}=="0x1003"
    ATTR{speed}=="100"
    ATTR{dormant}=="0"
    ATTR{addr_assign_type}=="3"
    ATTR{dev_id}=="0x0"
    ATTR{duplex}=="full"
    ATTR{iflink}=="4"
    ATTR{addr_len}=="6"
    ATTR{address}=="36:c9:e3:f1:b8:04"
    ATTR{operstate}=="up"
    ATTR{broadcast}=="ff:ff:ff:ff:ff:ff"
    ATTR{tx_queue_len}=="1000"
    ATTR{ifalias}==""
    ATTR{ifindex}=="4"
    ATTR{link_mode}=="0"
    ATTR{carrier}=="1"

  looking at parent device '/devices/soc.0/1c0b000.eth':
    KERNELS=="1c0b000.eth"
    SUBSYSTEMS=="platform"
    DRIVERS=="sun4i-emac"

  looking at parent device '/devices/soc.0':
    KERNELS=="soc.0"
    SUBSYSTEMS=="platform"
    DRIVERS==""
    
[root@a40i]:~$:

/etc/ udev/rules.d中新建规则文件a.rules

KERNEL=="eth*", DRIVERS=="sun4i-emac",KERNELS=="1c0b000.eth",NAME="lan0",RUN+="/usr/bin/setemac.sh"
KERNEL=="eth*", DRIVERS=="sunxi-gmac",KERNELS=="1c50000.eth", NAME="lan1",RUN+="/usr/bin/setgmac.sh"
KERNEL=="eth*", ATTR{address}=="00:e0:86:a0:8f:a3", NAME="lan2",RUN+="/sbin/ifconfig %k 192.168.4.101"
KERNEL=="eth*", ATTR{address}=="00:e0:86:a0:8f:bd", NAME="lan3",RUN+="/sbin/ifconfig %k 192.168.5.101"
KERNEL=="eth*", ATTR{address}=="00:e0:86:a0:92:7b", NAME="lan4",RUN+="/sbin/ifconfig %k 192.168.6.101"
KERNEL=="eth*", ATTR{address}=="00:e0:86:a0:91:91", NAME="lan5",RUN+="/sbin/ifconfig %k 192.168.7.101"

其中setemac.sh内容

#!/bin/sh
ifconfig lan0 down
ifconfig lan0 hw ether 36:C9:E3:F1:B8:04
ifconfig lan0 192.168.2.101
ifconfig lan0 up

setgmac.sh内容


#!/bin/sh
ifconfig lan1 down
ifconfig lan1 hw ether 36:C9:E3:F1:B8:05
ifconfig lan1 192.168.3.101
ifconfig lan1 up

注意:修改mac地址命令必须放到修改IP命令前面,否则出现Device Busy错误。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值