Home Raspberry Pi How to find the serial number of the Raspberry Pi

How to find the serial number of the Raspberry Pi

Most hardware devices and software come with a unique identification number commonly referred to as a Serial number. For example, if you walked to a store today and bought 20 Raspberry Pi 4 (4 GB RAM) boards, all these boards look similar to the naked. And you are not wrong because they have the same design and the same amount of resources (memory, storage, and processing speed). However, each Raspberry Pi board has a unique serial number assigned during manufacture.

Finding the serial number of a Raspberry Pi

This post will give you a step-by-step guide on – “How to find the serial number of Raspberry Pi.” You can use many methods to do this, and we will try and look at all ways, including using a programming language like Python or NodeJS.

Requirements

  • A Raspberry Pi board
  • Reliable power supply
  • An internet connection – This will only be necessary if you plan to use SSH/ VNC or install Node.JS

Other optional requirements include:

  • Display monitor
  • Keyboard
  • Mouse

We are referring to those as optional because you can opt to access your Raspberry Pi via SSH or VNC. If you think of doing that, the following posts will help. “How to SSH into Raspberry Pi” and “How to Set up VNC Server on Raspberry Pi. “

Setup your Raspberry Pi

For this post, we will use the official Raspberry Pi OS. You can check out our post – “How to install the official Raspberry Pi OS,” which will give you a step-by-step guide, including How to format your SD card and flash the OS image using Balena Echer. Insert the SD card into the Raspberry Pi and power it up. 

Like the rule of thumb for every Linux distribution, execute the commands below to update your system.

sudo apt update
sudo apt upgrade

When done, you can now look at the various methods you can use to find the Serial Number of your Raspberry Pi.

Method 1: View the contents of /proc/cpuinfo file

That is one of the easiest ways that you can use to find the Serial Number of your Raspberry Pi. The /proc/cpuinfo file contains important system information like Processor type, CPU architecture, Number of processors, serial Number, and much more.

You can use different commands to view this file, including opening it with your favorite text editor (e.g., nano or vim). For this post, we will use the cat command as shown below.

cat /proc/cpuinfo
serial number

serial number

You will see the Serial Number at the bottom of the list.

Method 2. Use the grep command

Grep is a Linux command that you can use to search a file for particular patterns. In Method 1, we showed you how to get the Serial Number by viewing the contents of the /proc/cpuinfo file. Even though this seems pretty easier, you might prefer a more straightforward method that only displays the Serail Number alone. That’s where Grep command comes in.

In the command below, we are using the cat command to read the contents of the /proc/cpuinfo file, but we are piping this output to the grep command to only get the Serial Number information.

cat /proc/cpuinfo | grep Serial

grep serial number

grep Serial Number

If you only want the Serial Number and not any other information, you can pip the output above to the cut command. We will remove the tabs used on the left side of the colon and only return the Serial Number as shown below.

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

Grep and Cut Serial Number

Method 3. Use cat and awk command

Using the grep command discussed in Method 2, we showed you how you could only return the Serial Number line.

Serial : 100000006ca64554

We can pipe this output into the awk command and only return the third index value, the Serial Number. To better understand that statement, let’s look at it practically.

  • Serial: This is the first index value
  • :“: This is the second index value
  • 100000006ca64554: This is the third index value and the serial number we are interested in.

To get the serial number, we can use the command, awk ' {print $3}'. Let us look at the whole command.

cat /proc/cpuinfo | grep Serial | awk ' {print $3}'
awk serial number

AWK Serial Number

However, the command above can be referred to as UUOC or “the Useless Use of Cat ” because we can simplify the command further, as shown below.

awk '/Serial/{print $3}' /proc/cpuinfo

This way, you don’t even need the cat command.

awk

awk

Method 4. Use the sed command

Another quick and straightforward method you can use is the sed command below.

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

Let’s look at the parameters used in this command in detail.

  • sed -n: Here, we tell the sed command to read through the file but don’t print all lines.
  • ‘/^Serial/: We are looking for lines that start with the word “Serial.” That is similar to what we were doing with the grep command.
  • s/.* //;p: This might seem not very easy if you are new to Linux regex. Here, we tell sed to remove everything on the Serial Number line up to the last space. That enables us only to return the Serial number.
  • p: Here, we tell the sed command to print the result.
  • /proc/cpuinfo: That is our target file for this command.
sed command

Sed Command

Method 5. Use Bash Scripts

If you are writing a bash script and need to return the Serial Number of the Raspberry Pi, below is a script you can use.

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

We have only converted the command below to that script.

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

Run Script

Method 6. Use Node.JS

If you are a web developer working with Node.JS, you might find yourself looking for a way to get the Serial Number using NodeJS. Run install node on your system using the command below to get started.

sudo apt install nodejs

When done, write the script below and save it with a “.js” file extension.

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);
console.log(serial_line.split(":")[1].slice(1));
}
x++;
}
}
getserial()

To run the program, use the syntax below.

node [program-name]e.g
node test.js
nodejs

NodeJS

Conclusion

This post has looked at many methods that you can use to get the Serial Number of your Raspberry Pi. Which one worked for you? Please feel to share with our readers in the comments below. Also, if you use another way other than those mentioned, let us know below, and we might probably add it to the list.

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.