How to get MAC address in CentOS 7 using terminal and not using NIC?

General support questions
Post Reply
Posts: 1
Joined: 2020/07/14 15:47:53

How to get MAC address in CentOS 7 using terminal and not using NIC?

Post by Riley18 » 2020/07/14 15:50:44

In one of the project, client has upgraded their server from CentOS6 to CentOS7. In CentOS 6, we used to get MAC address from "ifconfig -a" command and fetching out MAC address by regex (.HWaddr.:(.*)). Now in CentOS 7, ifconfig is not working. We are using java and suggested to use below code which is working fine, but we have to provide solution using command only and not using Java APIs:

Code: Select all

String getLocalServerMAC(){
    String macAddress = null;
    String macAddressToBeValidated = null;
    try {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface networkInterface : Collections.list(networkInterfaces)) {
            if (networkInterface != null && networkInterface.isUp()) {
                macAddressToBeValidated = getNotValidatedMACAddress(networkInterface);
                if (macAddressToBeValidated != null && !macAddressToBeValidated.isEmpty()) {
                    macAddress = macAddressToBeValidated; break;
    }catch (SocketException|RuntimeException e) {
        LOGGER.error(e.getMessage(), e);
    return macAddress;

private static String getNotValidatedMACAddress(NetworkInterface networkInterface) throws SocketException {
    StringBuilder macAddressToBeValidated = new StringBuilder();
    byte[] macAddressAsStream = networkInterface.getHardwareAddress();
    if(macAddressAsStream != null) {
        for (int i = 0; i < macAddressAsStream.length; i++){
            macAddressToBeValidated.append(String.format("%02X%s", macAddressAsStream[i], (i < macAddressAsStream.length - 1) ? "-" : ""));
    return macAddressToBeValidated.toString();

User avatar
Forum Moderator
Posts: 29069
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: How to get MAC address in CentOS 7 using terminal and not using NIC?

Post by TrevorH » 2020/07/14 16:21:29

Well you could install ifconfig.

dnf provides '*/*bin/ifconfig'

That will tell you which package it's in. The next problem you will have is that the output from ifconfig may have changed in 7. Perhaps just dump it and move to the more modern replacement: the ip command. `ip a` has both ip address and MAC. `ip l` has much the same but minus the ip.
CentOS 6 will die in November 2020 - migrate sooner rather than later!
Info for USB installs on
CentOS 5 is dead, do not use it.
Full time Geek, part time moderator. Use the FAQ Luke

Post Reply

Return to “CentOS 7 - General Support”