100

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

Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113

11 Answers11

106

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
evan.bovie
  • 103
  • 4
Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113
37

/sys/firmware/devicetree/base/serial-number seems to be more universal nowadays.
And it doesn't require any additional processing.

# SN=$(cat /sys/firmware/devicetree/base/serial-number)
# echo $SN
0000000061c8eda7

BTW, here is model as well (`/sys/firmware/devicetree/base/model`)
edo1
  • 576
  • 4
  • 7
13

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.

Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113
Dennis Williamson
  • 1,739
  • 3
  • 13
  • 12
9

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
Alex Chamberlain
  • 15,638
  • 15
  • 69
  • 113
ps-rpi
  • 91
  • 1
8

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);

}

M. Martins
  • 81
  • 1
  • 1
7
grep -i serial /proc/cpuinfo | cut -d : -f2
Gerben
  • 2,420
  • 16
  • 17
user14752
  • 81
  • 1
  • 1
6

Using awk:

cat /proc/cpuinfo | grep Serial | awk ' {print $3}'
Greenonline
  • 2,969
  • 5
  • 27
  • 38
Name
  • 77
  • 1
  • 1
4

Most-answered question on this SE?... maybe just do this:

$ grep Serial /proc/cpuinfo

And as a bonus:

  • Raspberry Pis have "randomly assigned" serial numbers that are written to non-volatile memory. Long discussion here on whether or not these serial numbers are unique, but the bottom line is that the algorithm is proprietary to the chip manufacturer. Which effectively means, "nobody knows if the serial number can be counted on to be truly unique". It's apparently considered "unique enough" to protect a £2.40 MPEG-2 license key though - if that lets you sleep better :)
Seamus
  • 23,558
  • 5
  • 42
  • 83
3

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.

scruss
  • 9,117
  • 1
  • 25
  • 36
2

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
Colateral
  • 21
  • 1
1

I had posted my own answer elsewhere but tried one of the Answers on this recently edit Question.

I had previously said this could be found using vcgencmd otp_dump | grep 28: as the serial number is programmed into the OTP register
NOTE The value in OTP register 28 should be the definitive answer as all other methods are just an alternate method of accessing the value programmed at manufacture.

These are 32 bit values represented in hex.

This DOES NOT agree with the value in /proc/cpuinfo in my Pi4 as shown by e.g. awk '/Serial/ {print $3}' /proc/cpuinfo

I had some time ago predicted the Foundation would run out of 32 bit numbers, which seems to have happened as it is now a 64 bit number with a leading 1

Milliways
  • 62,573
  • 32
  • 113
  • 225