zoukankan      html  css  js  c++  java
  • Linux下修改网卡的mac地址

     

    • 方法1:

      # ifconfig ethX down

      # ifconfig ethX hw ether NEW_MAC_ADDR

      # ifconfig ethX up

      但是这样重启后就失效了,需要再敲一遍。当然也可以写入rc.local来解决
      例如:
      ifconfig eth0 down
      ifconfig eth0 hw ether 00:01:02:03:04:05  ##注意这里的mac地址用冒号分开,而非是"-",mac地址字母不区分大小写
      ifconfig eth0 up
      这样修改后,下面的地址也改变了:
      [root@elastix82 ~]# more /sys/class/net/eth0/address
      00:01:02:03:04:05
      [root@elastix82 ~]#

    • 方法2:

      与方法1类似,只是用ip命令代替ifconfig:

      # ip link set ethX address NEW_MAC_ADDR

      但是依旧重启后失效
      例如;
      [root@elastix82 ~]# ip link set eth0 address 02:04:06:08:AA:AA
      [root@elastix82 ~]#
      [root@elastix82 ~]# more /sys/class/net/eth0/address         
      02:04:06:08:aa:aa
      [root@elastix82 ~]#

    • 方法3:

      对于RedHat系统,

      # vi /etc/sysconfig/network-scripts/ifcfg-ethX

      注释掉HWADDR行,如果有的话,加入活修改:

       

      MACADDR=NEW_MAC_ADDR

      保存退出即可

      [root@elastix82 ~]# more /etc/sysconfig/network-scripts/ifcfg-eth0
      # Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+
      DEVICE=eth0
      BOOTPROTO=static
      BROADCAST=192.168.1.255
      DHCPCLASS=
      #HWADDR=0A:C9:F2:31:E5:4F
      MACADDR=02:03:05:07:0A:4F
      IPADDR=192.168.1.82
      NETMASK=255.255.255.0
      NETWORK=192.168.1.0
      ONBOOT=yes
      [root@elastix82 ~]#

    关于HWADDR和MACADDR的区别(很容易把HWADDR误认为是控制MAC地址的,因为ifconfig输出时就显示HWaddr的嘛 -.-),可以参考如何修改mac地址让它一直生效?,以及redhat.com.cn上面的在一个以太网接口配置文件中,有那些可以配置的参数?这两篇,摘抄一段如下:

     

    HWADDR=, 其中 以AA:BB:CC:DD:EE:FF形式的以太网设备的硬件地址.在有多个网卡设备的机器上,这个字段是非常有用的,它保证设备接口被分配了正确的设备名,而不考虑每个网卡模块被配置的加载顺序.这个字段不能和MACADDR一起使用.

     

    MACADDR=, 其中 以AA:BB:CC:DD:EE:FF形式的以太网设备的硬件地址.在有多个网卡设备的机器上.这个字段用于给一个接口分配一个MAC地址,覆盖物理分配的MAC地址. 这个字段不能和HWADDR一起使用.

    另外,还可以参考ifup脚本中关于HWADDR和MACADDR的处理:

     

    # remap, if the device is bound with a MAC address and not the right device num

    # bail out, if the MAC does not fit

    if [ -n "${HWADDR}" ]; then

    FOUNDMACADDR=`get_hwaddr ${REALDEVICE}`

    if [ "${FOUNDMACADDR}" != "${HWADDR}" ]; then

    curdev=`ip -o link | awk -F ':' -vIGNORECASE=1 '/$HWADDR/ { print $2 }'`

    rename_device "${REALDEVICE}" "${HWADDR}" "${curdev}" || {

    echo $"Device ${DEVICE} has different MAC address than expected, ignoring."

    exit 1

    }

    fi

    fi

     

    # this isn't the same as the MAC in the configuration filename. It is

    # available as a configuration option in the config file, forcing the kernel

    # to think an ethernet card has a different MAC address than it really has.

    if [ -n "${MACADDR}" ]; then

    ip link set dev ${DEVICE} address ${MACADDR}

    fi

    再看下nameif的man文档:

     

    nameif looks for the interface with the given MAC address and renames it to the name given.

    就能知道HWADDR和MACADDR的不同之处了,前者是用来根据HWADDR绑定ethX名称的,后者才是真正用来修改MAC地址的

  • 相关阅读:
    day09
    day8
    day 7
    day 6
    PYTHON 学习
    day 5 作业
    day04作业
    Day03作业及默写
    python 2020 day4
    (copy)python操作excel
  • 原文地址:https://www.cnblogs.com/voiphudong/p/3487074.html
Copyright © 2011-2022 走看看