Kirsle.net logo Kirsle.net

Get FAT Drive Serial Numbers in Unix

December 31, 2009 by Noah
There's not a lot of information about this on The Google, so hopefully this blog will help anyone who wants to know how to get the serial number of a FAT partition from within a Unix-like operating system (including Linux and Mac OS X).

First, this is what I mean about serial numbers. Suppose you're using a Windows system, have a floppy disk at drive A:/ and a regular USB flash drive at E:/, and you run these commands in the command prompt:

C:\>vol E:
Volume in drive E is CRUZER
Volume Serial Number is 955C-59BF

C:\>vol A:
Volume in drive A has no label.
Volume Serial Number is EC2B-36AF
These serial numbers are assigned when the drive is formatted; reformatting a floppy disk or flash drive will give it a different serial number.

According to The Wikipedia, the serial number (ID) is kept in two different places on the partition depending on the version of FAT being used.

In FAT12 and FAT16 (used with floppy disks), the ID begins at byte offset 0x27 (39 in decimal); in FAT32 (used with flash drives and external hard drives), the ID begins at 0x43 (67 in decimal).

So, with the handy dd utility that comes standard on pretty much any Unix-like system, you can extract this information and display it. Here are a couple of one-liners you can run in a Unix terminal. I'll explain how they work afterward.

# For FAT32 filesystems (modern flash drives)
dd if=/dev/sdb1 skip=67 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'

# For FAT12/16 filesystems (old floppy drives)
dd if=testfloppy.img skip=39 bs=1 count=4 | hexdump -v -e '1/1 "%02X" " "' | xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
I underlined the input file (if) and byte offset (skip) in both of these commands. In the first one, I ran the command on a real, physical, flash drive, that had a device node at /dev/sdb1 for its one and only partition. In the second one, I ran it on a floppy disk image file (who has a computer with a real floppy drive these days?)

If you're going to be using a physical device like in my first command, you need to run the command with root privileges (regular users can't read directly from the device node). My second example (using an image file) can be run as a regular user, however.

These commands printed in the terminal for me:

(for the flash drive)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.3445e-05 s, 120 kB/s
Serial Number: 955C-59BF

(for the floppy image)
4+0 records in
4+0 records out
4 bytes (4 B) copied, 3.1551e-05 s, 127 kB/s
Serial Number: EC2B-36AF
And now, how the commands work. I'll use the flash drive command as the example. In this one-liner, three commands are being executed at once:
dd if=/dev/sdb1 skip=67 bs=1 count=4
hexdump -v -e '1/1 "%02X" " "'
xargs perl -e '@_=@ARGV; print "Serial Number: $_[3]$_[2]-$_[1]$_[0]\n"'
The dd command gets the operating system to read raw data from the flash drive at /dev/sdb1, skipping the first 67 bytes, reading only 1 byte at a time, and reading a total of 4 bytes. This gets the 4 byte serial number; now we need to display it in hexadecimal like Windows and DOS.

The hexdump command takes the 4 binary bytes and displays them in hexadecimal. On my flash drive, it looks like this: BF 59 5C 95. Note that the hex codes are out of order; Windows shows them as 955C-59BF - basically, the reverse of what hexdump shows. Hexdump is showing the correct order; Windows and DOS reverse them when they show you the serial number.

So, we run it through xargs (which turns the four hex numbers into four separate parameters) and sends them to a quick Perl script, which prints out "Serial Number:" and puts the hex codes in the correct order, to give the same result as Windows and DOS.

One could use this information to make a vol command for Unix. If the command checks other places in the filesystem headers to determine the version of FAT, it could automatically use the correct byte offset and get the serial number from both floppy disks and flash drives.

Tags:

Comments

There are 12 comments on this page. Add yours.

Avatar image
mike posted on April 5, 2010 @ 05:40 UTC

have you ever heard about "/sbin/blkid"? It's in the core Linux package "util-linux", I am sure you have it :). The correct name for the "Serial Number:" is "UUID".

Avatar image
Christophe posted on June 30, 2010 @ 11:16 UTC

Hello, Very nice . I was looking for more then a week on such solution. Since I need the UUID from 16 or 32 fats on usb sticks On a mac pc. For synchronizing php script this is a security build in to avoid persons to synchronize on another stick then their own. Thank's

Avatar image
Kyle posted on August 11, 2010 @ 15:07 UTC

Tried it on Mac OS X, got the same serial number for 2 different flash drives.

Avatar image
rarerade posted on November 30, 2010 @ 07:47 UTC

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this .pl but can't see my serial number...

use Win32::OLE qw(in); my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//."); my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive"); my @DV = in($DriveCollection); print $DV[0]->{SerialNumber};

Avatar image
rarerade posted on November 30, 2010 @ 07:48 UTC

Plz tell me how can i know my harddisk serial number on windowsXP packet 3 > i use this .pl but can't see my serial number...

use Win32::OLE qw(in); my $WMIServices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//."); my $DriveCollection = $WMIServices->InstancesOf("Win32_DiskDrive"); my @DV = in($DriveCollection); print $DV[0]->{SerialNumber};

Avatar image
Noah (@kirsle) posted on November 30, 2010 @ 08:33 UTC

my ($serial) = vol E:\\ =~ m/Volume Serial Number is ([A-Fa-f0-9-]{9})/;

;)

Avatar image
rarerade posted on December 1, 2010 @ 02:56 UTC

Volume Serial Number and Harddisk Number is the same ?..

Sir.kirsle I want to ask you some thing. How can i make .pl to use just 1 computer. About .pl can check the computer key or serial number or Volume Serial Number.

EX. I give .pl program to my friend, I want .pl can use just his computer. If my friend save .pl to another computer he can't use it. ( How can I do like this Sir.)

ํํYou are very very important person,because no one help me like you. Thank You For Helpping.

Avatar image
MANAS PRADHAN posted on March 13, 2011 @ 21:46 UTC

TELL ME HOW I CAN CHECK THE SERIAL NO GIVEN TO MY PEN DRIVE AT THE TIME OF PURCHASE , I NEED 2 KNOW AND MATCH THE ORIGINAL SERIAL NO WITH THE PEN DRIVE?? KINDLY REPLY ME SOON

Avatar image
Noah (@kirsle) posted on March 13, 2011 @ 21:48 UTC

This only gets the DOS serial number given when the drive was formatted. I don't know how to get the hardware serial number given by the manufacturer though, sorry. :(

Avatar image
Niko posted on December 28, 2011 @ 11:53 UTC

I follow your step, but on Macintosh HD my serial number is 0000-0000 and on DVDs\CDs it response: "Resource busy". If I search this SN with Windows on Mac HD or optical support I read my real SN why?

Thanks for your blog that is really important.

Avatar image
Adam posted on April 22, 2012 @ 07:25 UTC

Who still has floppy drives? I do lol!

Thanks for the one-liner for FAT12/16 serial numbers. It even adds that "-" in the right place!

I really appreciate people who put the time into these little things that add to a wealth of knowledge! Keep up the good work.

(If you need a floppy drive, I've got about 20 extras...)

Avatar image
sanket posted on January 22, 2013 @ 06:58 UTC

I have a usb pen drive. I am developing an application which stores the serial number of the pen drive, so that it can check it the next time the pen drive is plugged in. Please provide me with all the steps required to find the serial number on a linux system. Thanks !

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.