Finding your current MAC address in CentOS

I had an issue with one of my servers the other day: its power supply died unexpectedly during a scheduled restart. The poor thing never cam back up again.

Lucky for me, the data centre could simply swap out my hard disks and put them into another server. Although my data was save, the server wouldn’t connect to the network anymore – because it had a new MAC address. CentOS stores this value in two of its files, and when it changes (which is hardly ever the case), those files need to be updated.

Here’s how I fixed the problem. I did this on a CentOS 6 server, but it looks like the procedure is the same for CentOS 7.

Checking Interfaces and Connections

Before we begin, let’s see what the system looks like in its current (mis-)configured state. The following command will give us an overview:

4: vlan3893@eth1:&lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt;mtu1500qdisc noqueue state UP

link/ether8c:89:a5:1e:38:3dbrdff:ff:ff:ff:ff:ff

inet10.31.53.8/24brd10.31.53.255scope global vlan3893

inet6fe80::8e89:a5ff:fe1e:383d/64scope link

valid_lft forever preferred_lft forever

This tells us that eth0 appears to be unavailable, probably because it could not be brought up. We know why: because our MAC address has changed, and CentOS is trying to talk to hardware that doesn’t respond. Let’s fix that.

Detecting the current MAC address

There’s a system folder called /sys/class/net. In it we’ll find sub-folders corresponding to our connections (such as eth0, lo, wlan, etc). Each of these contains a file called address in which we can see our current MAC address. Those are updated on the fly and should always be up to date with whatever hardware we’re using.

The thing is though that a folder like eth0 will only exist if eth0 is indeed a valid connection. If eth0 is down, there’s no folder called eth0.

Hence, to have a look at all our current MAC addresses, we can read all address files like so:

1

2

3

4

5

cat/sys/class/net/*/address

8c:89:a5:1e:38:3d

8c:89:a5:1e:38:3e

00:00:00:00:00:00

The first one is the MAC address for our wired LAN connection, the second one is for the WLAN address and the third one for the local loopback.

Equipped with this knowledge, let’s tweak the system file that contains a record to the previous MAC address.

Updating CentOS with the current MAC address

The folks at the data centre were nice enough to tell me that all I had to do to connect my server to the network again was to delete a single file called

/etc/udev/rules.d/XX-persistent-net.rules

This file is automatically rebuilt by CentOS and automatically updated with the current MAC address. XX stands for a priority number (70 in my case). In our case, the file looks like this:

1

2

3

4

5

6

7

8

9

10

11

# This file was automatically generated by the /lib/udev/write_net_rules

Notice that although the MAC addresses correspond to what we’ve discovered in the earlier step, all I seem to have here are eth1 and eth2 – no mention of eth0, because it’s currently down.

Deleting this file and restarting the server should have fixed my problem. But it didn’t. I also had to tweak my network scripts file.

Updating Network Scripts

There’s yet another configuration file in /etc/sysconfig/network-scripts. We’ll find several files here beginning with ifcfg-. Those are stored during the initial network setup and as it seems are not updated on the fly when a change in hardware occurs. Programme like nmtui update the settings in these files.

The one we’re interested in is ifcfg-eth0. Among many other things (depending on our hardware and setup), mine contained something like this:

1

2

3

4

5

IPV6INIT="yes"

IPV6_AUTOCONF="no"

NM_CONTROLLED="yes"

ONBOOT="yes"

HWADDR=8C:89:A5:1D:FA:DC

Note the last line beginning with HWADDR. This is the MAC address from the previous server. Let’s update this to match the current one. Now a server restart should do the trick of connecting us to the network again.

Summary

Here are the steps that brought my server back online:

detect current MAC address with cat /sys/class/net/*/address

remove /etc/udev/rules.d/70-persistent-net.rules

update /etc/sysconfig/network-scripts/ifcfg-eth0 with the current MAC address

Share this:

Related

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.