Extract the Linux serial number without sudo
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.
c program for linux, to get a serial number of the device, for a non-root user
Why don't you use the MAC address as unique computer id. Here is the code to get MAC address of one of the interface. The code below basically reads name of all the available interfaces and then loops through them to get the MAC address of the first one excluding the loopback interface.
int main()
{
struct ifreq ifr;
struct ifconf ifc;
char buf[1024];
int success = 0;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) { /* handle error*/ };
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { /* handle error */ }
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
for (; it != end; ++it) {
strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
success = 1;
break;
}
}
}
else { /* handle error */ }
}
unsigned char mac_address[6];
if (success) memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6);
}
How to read HD serial in Linux without root privileges?
By just listing /dev/disk/by-id/
directory, you get IDs of all block devices on your system. The output is not exactly the same as with the commands, but it is close enough.
For this, you do not need root privileges.
On my PC :
$ /sbin/udevadm info --query=property --name=sda | grep "ID_SERIAL"
ID_SERIAL=WDC_WD5000AAKX-60U6AA0_WD-WCC2EHJ93860
ID_SERIAL_SHORT=WD-WCC2EHJ93860
and
$ ll /dev/disk/by-id/ | grep sda
lrwxrwxrwx 1 root root 9 Nov 11 07:47 ata-WDC_WD5000AAKX-60U6AA0_WD-WCC2EHJ93860 -> ../../sda
lrwxrwxrwx 1 root root 10 Nov 11 07:47 ata-WDC_WD5000AAKX-60U6AA0_WD-WCC2EHJ93860-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Nov 11 07:47 ata-WDC_WD5000AAKX-60U6AA0_WD-WCC2EHJ93860-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Nov 11 07:47 ata-WDC_WD5000AAKX-60U6AA0_WD-WCC2EHJ93860-part3 -> ../../sda3
On Linux get disk space used per user when you are not root
You can't determine the size of directories you have no read/execute permissions for, so this is only possible when being root.
Related Topics
User Time Larger Than Real Time
How to Find List of Valid Locales in My Linux Using Perl
Copying Files with Wildcard (*) to a Folder in a Bash Script - Why Isn't It Working
Why Can't Bash Recognize The Existence of a Socket File
How to Send Email from The Command Line
Raspberry-Pi Docker Error: Standard_Init_Linux.Go:178: Exec User Process Caused "Exec Format Error"
Running Docker Without Sudo on Ubuntu 14.04
How to Find All Immediate Sub-Directories of The Current Directory on Linux
Bluetooth Low-Energy on Linux API
What Special Meaning Does an Equal-Sign Have in Zsh
-Bash: /Usr/Bin/Virtualenvwrapper.Sh: No Such File or Directory
How to Add a Directory to The Perl Library Path at The System Level
Error: Nvidia-Smi Has Failed Because It Couldn't Communicate with The Nvidia Driver
How to Change Qmake Prefix Location
Will Java Compiled in Windows Work in Linux