Set MAC Address

Ubuntu

In Ubuntu, you can explicitly set a MAC address for your network card. You can either do it manually (Working with MAC Addresses) or use a package called macchanger (GNU MAC Changer).

To manually change your MAC address, for example, for eth0, you do the following:

sudo vim /etc/network/interfaces

And then you add following line to end of interface being configured (eth0 in this example):

hwaddress xx:xx:xx:xx:xx:xx

where xx:xx:xx:xx:xx:xx is the MAC address.

So now your interfaces file may look like this:

auto eth0 static
iface eth0 inet static
address 10.10.1.5
netmask 255.255.255.0
gateway 10.10.1.1
broadcast 10.10.1.255
hwaddress ether 02:01:02:03:04:08

Do the following to make sure all changes are applied:
sudo ifdown eth0 (warning: you will lose connectivity on this interface when you run this command, so make sure you are able to access your computer either via console or through another interface to run the rest of the commands)
sudo ifup eth0
sudo /etc/init.d/network restart

CentOS

Open the file of the network interface you want to modify and add the following:

HWADDR=xx:xx:xx:xx:xx:xx
MACADDR=xx:xx:xx:xx:xx:xx

For example, if you want to explicitly specify a MAC address for eth0, you do the following:

vim /etc/sysconfig/network-scripts/ifcfg-eth0

And then you add following line to end of file:
HWADDR=xx:xx:xx:xx:xx:xx
MACADDR=xx:xx:xx:xx:xx:xx
where xx:xx:xx:xx:xx:xx is the MAC address.

So now your ifcfg-eth0 file may look like:

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
DHCP_HOSTNAME=localhost.localdomain
IPADDR=10.10.1.5
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
HWADDR=02:01:02:03:04:08
MACADDR=02:01:02:03:04:08

Do the following to make sure all changes are applied:
sudo ifdown eth0 (warning: you will lose connectivity on this interface when you run this command, so make sure you are able to access your computer either via console or through another interface to run the rest of the commands)
sudo ifup eth0
sudo service network restart

7 Responses to Set MAC Address

  1. FractalizeR says:

    This solution will not change MAC on CentOS. You will have “device eth0 has mac address blabla, instad of configured address blabla . ignoring .” message and MAC will not be changed.

  2. FractalizeR says:

    To really change MAC you need to replace HWADDR directive with MACADDR directive with the same argument in ifcfg-ethX

  3. David Savinkoff says:

    Replacing the HWADDR directive with the MACADDR directive is what is recommended in sysconfig.txt .
    I, however, had a problem with the following error message occurring during boot-up whenever my dhcp lease had expired while my computer was turned off:
    “$alias device ${DEVICE} does not seem to be present, delaying initialization.”
    This error comes from /etc/sysconfig/network-scripts/ifup-eth . After looking at the ifup-eth script, I noticed that I could solve my problem by placing HWADDR=00:FE:ED:00:BE:EF on the first line of ifcfg-eth0 AND MACADDR=01:FE:ED:00:BE:EF on the last line.

  4. David Savinkoff says:

    Update: Replacing the HWADDR directive with the MACADDR directive works on CentOS 5.4

  5. Jason Moores says:

    Thanks for the update, David. I was trying to figure out how to perform this and wanted to know whether it would certainly work on CentOS.

  6. David Savinkoff says:

    How-To MAC bind an Arbitrary MAC address on CentOS 5.5
    (Using both HWADDR and MACADDR directives together)

    Remember to back up the original config files before modifying them.

    STEP 1:
    See ifcfg-eth0:
    /etc/sysconfig/networking/devices/ifcfg-eth0

    Shown here:
    # FEED, Inc. Ethernet
    DEVICE=eth0
    HWADDR=00:FE:ED:00:00:00
    BOOTPROTO=dhcp
    TYPE=Ethernet
    USERCTL=no
    IPV6INIT=no
    PEERDNS=yes
    ONBOOT=yes
    MACADDR=00:BE:EF:00:00:00

    And (optionally) ifcfg-eth1:
    /etc/sysconfig/networking/devices/ifcfg-eth1

    Shown here:
    # FEED, Inc. Ethernet
    DEVICE=eth1
    HWADDR=00:FE:ED:00:00:01
    BOOTPROTO=none
    NETMASK=255.255.255.0
    IPADDR=192.168.1.1
    TYPE=Ethernet
    USERCTL=no
    IPV6INIT=no
    PEERDNS=yes
    ONBOOT=yes
    MACADDR=00:BE:EF:00:00:01

    “Replacing” the HWADDR directive with the MACADDR directive is what is
    recommended in sysconfig.txt. (This doesn’t work properly on CentOS 5.5)

    I had a problem with the following error message occurring during boot-up
    whenever my dhcp lease had expired while my computer was turned off:
    “$alias device ${DEVICE} does not seem to be present, delaying initialization.”
    This error comes from /etc/sysconfig/network-scripts/ifup-eth . After looking
    at the ifup-eth script, I noticed that I could solve my problem by placing
    HWADDR=00:FE:ED:00:00:00 on the first line of ifcfg-eth0 AND
    MACADDR=00:BE:EF:00:00:00 on the last line (Now works on any line).

    STEP 2:
    Red Hat changed ifup-eth to defeat this hack. Therefore you must comment
    lines 49 50 and 51 as shown below to get MAC address binding for spoofing.

    See ifup-eth:
    /etc/sysconfig/network-scripts/ifup-eth

    Shown partially here:
    33
    34 # load the module associated with that device
    35 # /sbin/modprobe ${REALDEVICE}
    36 is_available ${REALDEVICE}
    37
    38 # remap, if the device is bound with a MAC address and not the right device num
    39 # bail out, if the MAC does not fit
    40 if [ -n “${HWADDR}” ]; then
    41 FOUNDMACADDR=`get_hwaddr ${REALDEVICE}`
    42 if [ “${FOUNDMACADDR}” != “${HWADDR}” ]; then
    43 curdev=`get_device_by_hwaddr ${HWADDR}`
    44 if [ -n “$curdev” ]; then
    45 rename_device “${REALDEVICE}” “${HWADDR}” “${curdev}” || {
    46 echo $”Device ${DEVICE} has different MAC address than expected, ignoring.”
    47 exit 1
    48 }
    49 # else
    50 # echo $”Device ${DEVICE} has different MAC address than expected, ignoring.”
    51 # exit 1
    52 fi
    53 fi
    54 fi
    55

    STEP 3:
    # /sbin/ifconfig
    # /sbin/service network restart
    # /sbin/ifconfig

  7. Pingback: Solution for device eth0 has different mac address on vmware centOS « Oracle, Web, Script, SQLserver, Tips & Trick