• 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错误。

  • 相关阅读:
    一文详解头层反绒皮和二层反绒皮的区别,别再傻傻分不清了!
    crontab 定时任务
    Qt使用FFmpeg的静态库
    流形上的预积分(中)
    几个数组相关常见算法题
    SpringBoot实现全局异常处理
    mysql八股1
    随笔感悟:Mysql悲观锁和乐观锁
    新机Word/PowerPoint新建空白文档后闪退问题
    【项目源码】一套基于springboot+Uniapp框架开发的智慧医院3D人体导诊系统源码
  • 原文地址:https://blog.csdn.net/bigfanofloT/article/details/127975837