71

Where can I find the serial number of the Raspberry Pi I am currently using?

  • 2
    is the serial number printed on the device or is it only available through software? I see there is a sticker on the device but I don't think that it is related to the serial number. It would be nice to know the ID of the device without having to power it on and connect it. – Scoop Oct 4 '12 at 21:54
  • 3
    @AlexisK It's only available in software. – Alex Chamberlain Oct 8 '12 at 10:21
  • 2
    Correct, there doesn't seem to be any relation with the sticker and serial number. I've read the sticker of a number of Pi's and tried to decode it. While Raspberry probably has internal lists connecting both, it doesn't seem that there is any way to calculate the serial or MAC from it. – EDP Aug 15 '15 at 9:24
80

The serial number can be found in /proc/cpuinfo; for example,

 pi@raspberrypi:~$ cat /proc/cpuinfo
 Processor       : ARMv6-compatible processor rev 7 (v6l)
 BogoMIPS        : 697.95
 Features        : swp half thumb fastmult vfp edsp java tls
 CPU implementer : 0x41
 CPU architecture: 7
 CPU variant     : 0x0
 CPU part        : 0xb76
 CPU revision    : 7

 Hardware        : BCM2708
 Revision        : 1000002
 Serial          : 000000000000000d

Bash

You can use very basic bash piping

cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2

Since tabs are used on the left side of the colon, cutting on the space character will reliably catch only the serial number.

Prior versions of this answer cut on the colon, which produced a leading space in the variable. That leading space is not removed during variable assignment as was previously suggested.

Bash/Perl

In Bash, it is very simple to extract... by using Perl. Use

cat /proc/cpuinfo | perl -n -e '/^Serial\s*:\s([0-9a-f]{16})$/ && print "$1\n"'

For example,

$ cat /proc/cpuinfo | perl -n -e '/^Serial\s*:\s([0-9a-f]{16})$/ && print "$1\n"'
000000000000000d

Python

Raspberry Spy provides a very useful Python example.

def getserial():
  # Extract serial from cpuinfo file
  cpuserial = "0000000000000000"
  try:
    f = open('/proc/cpuinfo','r')
    for line in f:
      if line[0:6]=='Serial':
        cpuserial = line[10:26]
    f.close()
  except:
    cpuserial = "ERROR000000000"

  return cpuserial

References

  1. Licence key product pages
  2. Raspberry Spy: Getting Your Raspberry Pi Serial Number Using Python
  • 6
    Perl will accept filenames as arguments. It's not necessary to use cat. – Dennis Williamson Oct 4 '12 at 21:18
  • 3
    the last 6 digits of my serial number are the same as the last 6 digits of my MAC address. Is this true for you? – Scoop Oct 4 '12 at 21:51
  • 6
    < redirection also works, so it's a useless use of cat. – XTL Oct 5 '12 at 6:21
  • 3
    Is there any way to get this information physically? I mean on the board, so I dont have to boot every pi to get this information. – Zeezer Nov 30 '16 at 14:46
  • 3
    Is it unique? seems a little short ... in my serial only 4 bytes used, all rest are zeroes – Flash Thunder Dec 14 '16 at 23:18
12

Bash/Grep

Using grep:

grep -Po '^Serial\s*:\s*\K[[:xdigit:]]{16}' /proc/cpuinfo

Bash

Using pure Bash without using any external utilities:

pattern='^Serial.*([[:xdigit:]]{16})$'
while read -r line
do
    if [[ $line =~ $pattern ]]
    then
        echo "${BASH_REMATCH[1]}"
    fi
done < /proc/cpuinfo

The output of either of the above is the same.

  • this is the only one that worked for me – cwd Mar 26 '14 at 19:36
  • This doesn't work: 'P' is an invalid option. At least in pcpAudioCore – theking2 Apr 17 '17 at 16:06
  • 1
    @theking2: I'm afraid I'm unfamiliar with pcpAudioCore. The -P (Perl regex) is an option of modern versions of GNU grep. – Dennis Williamson Apr 17 '17 at 17:26
8

Bash/Awk

Since this turned out to be some kind of "how many ways can you get the serial" here is the awk version

  awk '/^Serial\s*:\s/{print $3}' /proc/cpuinfo
7

NodeJS

For anyone interested here is a way to get the Raspberry Serial Number using NodeJS:

function getserial(){

   var fs = require('fs');

   var content = fs.readFileSync('/proc/cpuinfo', 'utf8');

   var cont_array = content.split("\n");

   var serial_line = cont_array[cont_array.length-2];

   var serial = serial_line.split(":");

   return serial[1].slice(1);

}
  • For whatever reason, content_array.length-2 doesn't always work for me. Sometimes it gives me "Raspberry Pi 4 Model B Rev 1.1" which is the "Model" line right after the "Serial" line. So I changed your function to this (see next comment) to make sure. – fivedogit Oct 5 at 21:34
  • function getserial() { var fs = require('fs'); var content = fs.readFileSync('/proc/cpuinfo', 'utf8'); var cont_array = content.split("\n"); var x = 0; var serial_line = ""; while (x < cont_array.length) { serial_line = cont_array[x]; if (serial_line.startsWith("Serial")) { return serial_line.split(":")[1].slice(1); } x++; } } – fivedogit Oct 5 at 21:34
5
grep -i serial /proc/cpuinfo | cut -d : -f2
  • 1
    programming golf is not encouraged on the web-site, because it's very difficult to follow your answer for beginners, especially if you don't write any explanation or detailed comment. – lenik May 10 '14 at 8:03
  • 1
    I'd oppose to call this answer "programming golf" comparing to even more cryptic one like "Bash/Awk". Actually, I intended to uplift this answer for sake of equality, but after running it on my PI and comparing to other version, won't do it because must admit, this answer is not giving precise output by inserting extra leading space. PS: I won't minus it too though – Van Jone Mar 25 '15 at 23:48
5

Using awk:

cat /proc/cpuinfo | grep Serial | awk ' {print $3}'
  • 6
    That's a nice example of UUOC. Could be simply written awk '/Serial/{print $3}' /proc/cpuinfo – ripat Feb 4 '16 at 17:30
2

The shortest and simplest hasn't been provided as an answer yet. This, in sed:

sed -n '/^Serial/{s/.* //;p}' /proc/cpuinfo

meaning:

  • sed -n — run the stream editor without printing every line
  • /^Serial/ — match only lines that start with the word “Serial”
  • s/.* //; — replace everything up until the last space with nothing (sed regexes are greedy, btw)
  • p — print the result.

sed sometimes gets a bad name for being hard to use and cryptic, but it is available and works the same way (as long as you stick to POSIX conventions) on many types of Linux and Unix.

  • 1
    My one-liner to even remove leading zeros: sed -n 's/^Serial\s*: 0*//p' /proc/cpuinfo – thomas May 22 '18 at 15:28
1

Yes. Indeed this the way to get it.

It is funny that my Pi0 has the same data (serial) as above example. There is no diferentiator between my PI0 serial and the one posted by Alex Chamberlain

For PI3 yo got 4 procesors

#cat /proc/cpuinfo

processor       : 0
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt                                                                                                                      vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 1
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt                                                                                                                      vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 2
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt                                                                                                                      vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

processor       : 3
model name      : ARMv7 Processor rev 4 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt                                                                                                                      vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xd03
CPU revision    : 4

Hardware        : BCM2709
Revision        : a02082
Serial          : 000000003d1d1c36

protected by goldilocks Sep 5 '18 at 13:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.