分享

Linux下修改网卡的mac地址

 richsky 2012-05-06
Linux下修改网卡的mac地址
  • 方法1:

    # ifconfig ethX down

    # ifconfig ethX hw ether NEW_MAC_ADDR

    # ifconfig ethX up

    但是这样重启后就失效了,需要再敲一遍。当然也可以写入rc.local来解决

  • 方法2:

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

    # ip link set ethX address NEW_MAC_ADDR

    但是依旧重启后失效

  • 方法3:

    对于RedHat系统,

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

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

    MACADDR {NEW_MAC_ADDR}

    保存退出即可

关于HWADDRMACADDR的区别(很容易把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脚本中关于HWADDRMACADDR的处理:

# 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.

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


From:http://liuleijsjx./blog/443820

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多