16

It is possible to extract the Linux serial number without using sudo?

I know it is possible to do in Windows: wmic bios get serialnumber and in macOS: system_profiler | grep "r (system)". Both of them do not require root privileges.

In Linux this can be used: sudo dmidecode -s system-serial-number, but it needs sudo. Is there another way?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alberto
  • 701
  • 4
  • 9
  • 25
  • "Linux serial number" probably means the BIOS serial number (from [the WMIC example](https://www.windows-commandline.com/wmic-bios-get-serial-number/)). – Peter Mortensen Mar 25 '22 at 23:23

3 Answers3

23

dmidecode reads this information from physical memory, using /dev/mem, which requires root.

The same information is also provided by the Linux kernel via sysfs in a virtual directory, /sys/devices/virtual/dmi/id.

Unfortunately, someone decided that all information in that virtual directory is open to anyone for reading, just not the serial numbers:

$ ls -l /sys/devices/virtual/dmi/id

-r--r--r-- 1 root root 4096 Nov 25 17:12 bios_date
-r--r--r-- 1 root root 4096 Nov 14 14:59 bios_vendor
-r--r--r-- 1 root root 4096 Nov 25 17:12 bios_version
-r--r--r-- 1 root root 4096 Nov 25 17:12 board_asset_tag
-r--r--r-- 1 root root 4096 Nov 25 17:12 board_name
-r-------- 1 root root 4096 Nov 25 17:12 board_serial
-r--r--r-- 1 root root 4096 Nov 14 14:59 board_vendor
-r--r--r-- 1 root root 4096 Nov 25 17:12 board_version
-r--r--r-- 1 root root 4096 Nov 25 17:12 chassis_asset_tag
-r-------- 1 root root 4096 Nov 25 17:12 chassis_serial
-r--r--r-- 1 root root 4096 Nov 25 17:12 chassis_type
-r--r--r-- 1 root root 4096 Nov 25 17:12 chassis_vendor
-r--r--r-- 1 root root 4096 Nov 25 17:12 chassis_version
-r--r--r-- 1 root root 4096 Nov 25 17:12 modalias
drwxr-xr-x 2 root root    0 Nov 25 17:12 power
-r--r--r-- 1 root root 4096 Nov 14 14:59 product_name
-r-------- 1 root root 4096 Nov 25 17:12 product_serial
-r-------- 1 root root 4096 Nov 14 14:59 product_uuid
-r--r--r-- 1 root root 4096 Nov 14 14:59 product_version
lrwxrwxrwx 1 root root    0 Nov 14 14:59 subsystem -> ../../../../class/dmi
-r--r--r-- 1 root root 4096 Nov 14 14:59 sys_vendor
-rw-r--r-- 1 root root 4096 Nov 14 14:59 uevent

If you can install package hal (not installed by default on recent Ubuntu versions), this command will work for you as non-root:

 lshal | grep system.hardware.serial

 system.hardware.serial = '<serial_number>'  (string)

This works because package hal installs the hald daemon, which runs as root and collects this data, making it possible for lshal to read it as non-root.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mvp
  • 111,019
  • 13
  • 122
  • 148
  • How can `lshal` access to this data if it is not launched as sudo?? – Alberto Nov 26 '13 at 19:23
  • 2
    This package installs `hald` daemon, which runs as root and collects this data, making it possible for `lshal` to read it. – mvp Nov 26 '13 at 19:32
  • Thank you! It is a pity that this data can not be collected without root permission – Alberto Nov 26 '13 at 19:34
  • hal-get-property --udi '/org/freedesktop/HAL/devices/computer' --key system.hardware.serial will get you the serial number without having to parse if that is what you want. – Marlon Feb 17 '14 at 19:11
  • Instead of starting an extra daemon, you can also add a start-up-script that runs chmod to give access to that data. – Morty Sep 21 '15 at 14:15
  • _`dmidecode` reads this information from sysfs_ — does it? On my system, when I run it without root, it complains that `/dev/mem` isn't accessible. – Ruslan Aug 27 '16 at 07:57
  • @Ruslan: Yes, you are right - dmidecode reads directly from physical memory (using `/dev/mem`). Access to `/dev/mem` still requires root access though :( I will change my answer to reflect accurate state of affairs – mvp Aug 29 '16 at 17:41
  • 2
    What is the equivalent of `hal` in Ubuntu 20.04 ? – Basile Starynkevitch Mar 26 '21 at 13:37
0

Another solution which does not require root privileges:

udevadm info --query=all --name=/dev/sda | grep ID_SERIAL

This is actually the library that lsblk, mentioned by don_crissti, leverages, but my version of lsblk does not include the option for serial.

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
0

Device1 name and corresponding serial number:

lsblk --nodeps -o name,serial

Output:

NAME SERIAL
sda  0000000012400917BA30
sdb  0000000012400917BA96

Add -n if you don't want to print the header line:

lsblk --nodeps -no name,serial

Output:

sda  0000000012400917BA30
sdb  0000000012400917BA96

Pass the device as an argument to get only the serial number of a specific device:

lsblk --nodeps -no serial /dev/sda

Output:

0000000012400917BA30

Keep in mind lsblk lists information about all available (or the specified) block devices. Now, for those who do not know what that last term means:

In general, block devices are devices that store or hold data. Diskette drives, hard drives and CD-ROM drives are all block devices. But that's not a problem when using lsblk as you can simply add more columns, e.g., type (device type) and/or tran (device transport type), etc.:

lsblk --nodeps -no name,serial,type,tran

.

sda  0000000012400917BA30     disk sata
sdb  0000000012400917BA96     disk sata
sr0  4B583242334C453233353320 rom  usb
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29