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.
5 Answers
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)}'
C++ example from https://developer.apple.com/library/archive/technotes/tn1103/_index.html:
#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;
}
To make it easier to remember, use a case insensitive search:
ioreg -l | grep -i serialnumber | grep -i platform
-
Warning: this returns a line | "IOPlatformSerialNumber" = "YourSerialNumber" That’s fine visually but if you use it in a script you'll need to pipe it to awk. Jun 25, 2019 at 4:28
If you don't want to count columns you can rely on the fact that the value is always in the last one and use
system_profiler SPHardwareDataType | awk '/Serial/{print $NF}'
-
gee, thanks mom @nohillside; we wouldn't want to subject people to anything that isn't kid gloves and kudos, you know, like reality. Jul 4, 2021 at 0:56
-
SE works a bit different than forum sites. The way to give kudos is to upvote posts you like or which helped you.– nohillside ♦Jul 4, 2021 at 6:18
-
I wasnt giving kudos @nohillside, I was criticizing the members of the thread that managed to waste time and misinform by discussing benchmarks around ioreg and system_profiler, giving weight to something that NO ONE outside of exceptional circumstances will ever concern themselves with. My beef, is that one day a junior ops engineer, will point to this or something like it, when defending a pull request that "optimizes" the serial-number-as-a-rube-goldberg-service, which is famously held together with spit and tape. Maybe balancing the carrot with the stick will help lead to less of that. Jul 4, 2021 at 23:29
-
Please have a look at apple.stackexchange.com/help/how-to-answer for what we look for in answers. Also, this is a Q&A site not a forum, answers should stand on their own and not discuss other answers. If you want to discuss or criticize other answers, please use comments beneath that answer or the chat.– nohillside ♦Jul 5, 2021 at 4:12