• 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
    
    • 1

    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]:~$:
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    /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"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其中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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

  • 相关阅读:
    【MySQL】超详细-基础操作
    力扣:129. 求根节点到叶节点数字之和(Python3)
    go演示GRPC的用法
    windows安装npm教程
    PyTorch - Sequential和ModuleList
    Vue--1.6计算属性
    公众号迁移操作流程是怎样的?
    vue实现浏览器关闭或刷新弹窗提示(beforeunload/unload)
    网易产品面试题:马路上有个红绿灯和斑马线,但是90%的人都不走这个斑马线,怎么解决?
    数据库连接池
  • 原文地址:https://blog.csdn.net/bigfanofloT/article/details/127975837