35

What command can I use to retrieve the system's serial number from the unix command line? As uname will output some information about the software and hardware, I would like to retrieve the serial number from a command to use in a script.

49

The system_profiler command provides a direct answer that’s easily human readable (assuming you are on 10.3 or newer), but you can also use ioreg for the task as it generally completes faster.

system_profiler SPHardwareDataType is the data type that contains the core hardware information, and you can use grep or awk to pare things down further as needed:

system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'

or

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'

Both of those commands take between 0.5 and 0.2 seconds to run on modern SSD Macs, so if you want to optimize the command and remove the " you can have your answer in 0.005s or so:

ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}'
  • 2
    The ioreg one is near instant on my MBPR. system_profiler although faster on this mac than my older ones is much slower. – markhunte Feb 17 '15 at 16:52
  • system_profile is so slow, ioreg is super fast. – user1735921 Nov 15 '18 at 6:59
14

This also works…

ioreg -l | grep IOPlatformSerialNumber
  • It's certainly easier to type than the awk version to strip out the the 600 lines of ioreg output we don't want to see. (also - don't mind the down voters - some people really don't like short answers or forget that not everyone knows how grep and awk work.) – bmike Feb 13 '14 at 19:16
  • Ok, thanks bmike, it seems simpler to me, but didn't expect down votes for a correct answer. Good to know. – MrPool Feb 13 '14 at 19:24
  • 1
    @MrPool when it does repeat what is in an earlier answer and does not improve on the previous one – Mark Feb 13 '14 at 19:28
  • 1
    They are not wrong votes per se. Even down votes are good as they indicate someone cares enough to take action. Also, I try to comment or post to chat when I see votes that don't strike me as totally fair in sum. The good news is up votes are +10 and down votes only -2 so if you post something controversial (like this zero balance with +2 and -2 is still +16 rep.) welcome to the internet ;-) – bmike Feb 13 '14 at 19:28
  • 1
    And mark is correct, if all a user does is post exclusively late, short, minor changes without explaining (if I guessed your point - typing grep is both more clear, shorter, and good enough for many) I might ping that user and ask they knock it off. Most high reputation users would comment on my answer, but we can't expect a new user with 1 rep to do that, now can we :-) – bmike Feb 13 '14 at 19:29
2

To make it easier to remember, use a case insensitive search:

ioreg -l | grep -i serialnumber | grep -i platform
0

C++ example:

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

std::string 

getSerialNumber()

{

   CFStringRef serial;
    char buffer[32] = {0};
    std::string seriaNumber;

io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
                                                          IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert)
{
    CFTypeRef serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert,
                                                                       CFSTR(kIOPlatformSerialNumberKey),
                                                                       kCFAllocatorDefault, 0);
    if (serialNumberAsCFString) {
        serial = (CFStringRef)serialNumberAsCFString;
    }
    if (CFStringGetCString(serial, buffer, 32, kCFStringEncodingUTF8)) {
        seriaNumber = buffer;
    }

    IOObjectRelease(platformExpert);
}
return seriaNumber;
}
  • Welcome to Ask Different. Please understand that posting an answer that essentially contains only code with no directions whatsoever on what to do with it or how to use it doesn't make for a quality answer. Please take the time to read How do I write a good answer? and then edit your answer to include instruction on compiling the code and how to use it. Also take the Tour. – user3439894 Feb 14 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .