Code Ghar

Set MAC Address

Posted in configuration by hs on June 9, 2008

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

Tagged with: ,

3 Responses

Subscribe to comments with RSS.

  1. FractalizeR said, on September 25, 2008 at 7:20 pm

    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 said, on September 25, 2008 at 7:21 pm

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

  3. David Savinkoff said, on December 5, 2008 at 1:15 pm

    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.


Leave a Reply