Get disk serial number of "/" from the command line

joegood
joegood used Ask the Experts™
on
hdparm -i /dev/hda seems to work but only if I am root.  Is there a way to get this as a non-root user?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Not unless you want to expose your entire system.  Even if you give execute access to a specific group to run /sbin/hdparm, HD the device is restricted. /dev/hda

so unless you want to give access to /dev/hda, NOT A GOOD IDEA, you must be root

Commented:
install, configure and use "sudo"
Here is a newsgroup posting with c source code for getting the serial number, not sure if a non-root user can use it?

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=N8gLa.31014%24pH3.12159%40news2.east.cox.net&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26q%3Dprintf(%2522Serial%2BNumber%253D%2525s%255Cn%2522%252Cid.serial_no)%253B%26sa%3DN%26tab%3Dwg

From: Gary Desrosiers (nospam@cox.net)
Subject: Re: c code that can retrieve "Drive Serial Number" from the Harddisk firmware.
 
View this article only
Newsgroups: comp.os.linux.development.apps, comp.os.linux.development.system, it.comp.os.linux.development
Date: 2003-06-28 05:48:46 PST
 

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
struct hd_driveid id;
int fd = open("/dev/hda", O_RDONLY|O_NONBLOCK);
if (fd < 0)
{
        perror("/dev/hda");
        exit(errno);
}
if(!ioctl(fd, HDIO_GET_IDENTITY, &id))
{
    printf("Serial Number=%s\n",id.serial_no);
     //printf("Model Number=%s\n",id.model);
     //printf("Firmware Revision=%.8s\n",id.fw_rev);
     //printf("Cylinders=%d\n",id.cyls);
     //printf("heads=%d\n",id.heads);
     //printf("Sectors/Track=%d\n",id.sectors);
} return EXIT_SUCCESS;
}

From what I found, you cannot get the serial number from /proc unless you want to decode /proc/ide/ide0/hda.




Mark

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial