27

How do I change the UUID of a vfat partition?

For ext2 / ext3 / ext4, this is done with a simple:

tune2fs -U <new-uuid> /dev/<partition>

Is there a similar command for vfat partitions?

4 Answers 4

16

It might be helpful to know why you want to do this. I don't know of a tool to do what you want, but there may be other options....

Technically, FAT partitions don't have UUIDs; they have serial numbers, which several Linux tools and files (such as mount and /etc/fstab) can use in place of UUIDs. On GPT disks, the partitions that hold FAT filesystems have GUIDs, which (on recent enough Linux distributions) can be referred to as PARTUUIDs in mount and /etc/fstab. Both FAT filesystems and (on GPT disks) the partitions that contain them can also have names, which can also be used by mount or in /etc/fstab. Any of these features might be usable in place of the FAT serial number.

Although I don't know of a tool to change FAT partitions' serial numbers, they can be set when the filesystem is created via the -i option to mkdosfs, as in:

mkdosfs -i ABCD1234 /dev/sdc1

This example sets the serial number of /dev/sdc1 to ABCD-1234. If the filesystem is small enough, you could back it up, create a fresh filesystem with the desired serial number, and restore it. If you're trying to set the serial number as part of a disk or installation setup script, setting it via mkdosfs might be all you need.

You can change the partition's GUID with gdisk or sgdisk. Using the latter, you'd do the job via the -u option, as in:

sgdisk -u 1:BC87D91A-02F3-4C52-87CD-536A3DF2A074 /dev/sdc

This example sets the GUID of /dev/sdc1 to BC87D91A-02F3-4C52-87CD-536A3DF2A074. (Note that -u takes a colon-delimited set of options, the first of which is the partition number and the second of which is the GUID value. The device filename for the whole-disk device [not the partition] is the final option.)

Depending on your needs, one of these approaches may work. Moving broader afield, you can set the filesystem's label with the dosfslabel tool; or you can change a GPT partition's name with sgdisk's -c option, which works much like -u, but takes a name rather than a GUID.

3
  • 9
    The servial number of a vfat device can be changed via the mlabel command of the mtools.
    – liquidat
    Apr 24, 2018 at 18:22
  • 3
    Here's an example with the mlabel command : sudo mlabel -i /dev/mmcblk0p1 -N 12345678
    – SebMa
    Nov 22, 2019 at 0:35
  • For the why, you could argue I'm overthinking this but how about I restored a usb stick from an blank image of another of the same brand/model, now I want to give it unique dos and vfat identifiers (because I probably still have that other usb key around...). Dec 1, 2020 at 1:40
35

The volume ID of FAT32 is stored in the first sector at offset 67 (0x43), for FAT16 it's at 39 (0x27). One can use the dd command to read it (replace /dev/sdc1 with your real partition):

dd bs=1 skip=67 count=4 if=/dev/sdc1 2>/dev/null \
| xxd -plain -u \
| sed -r 's/(..)(..)(..)(..)/\4\3-\2\1/'

And, of course, one can also store a new UUID (replace 1234-ABCD with your desired value):

UUID="1234-ABCD"
printf "\x${UUID:7:2}\x${UUID:5:2}\x${UUID:2:2}\x${UUID:0:2}" \
| dd bs=1 seek=67 count=4 conv=notrunc of=/dev/sdc1

Reference: FAT physical structure (Volume Serial Number offset): https://technet.microsoft.com/en-us/library/cc776720(v=ws.10).aspx#w2k3tr_fat_how_gkxz

4
  • 5
    that's amazing! This is especially handy if you want to exchange a external micro sd-card on an Android device and want the new one match the same path as the old one. Jun 17, 2018 at 10:31
  • 2
    Note that for FAT12/FAT16 volumes the offset is 39, not 67! This may happen if your partition is really small (mine was a 256M microSD). You can do a sanity check if you compare the readout by dd with the output of blkid before trying to write to your partition.
    – Ruslan
    Aug 12, 2018 at 17:22
  • dash(1) (the "/binsh" for ubuntu) cannot interpret ${UUID:7:2} and the built-in printf does not interpolate \xHH. So it is better to use printf "%s" "${UUID}" | sed -r 's/(..)(..)-(..)(..)/\4\3\2\1/' | xxd -r -plain
    – wolfmanx
    May 2, 2019 at 20:35
  • Don't forget to sudo, I didn't do it for the reading part and no error was showing.
    – Drarig29
    Jun 11, 2021 at 14:42
9

Often one may want to set a new random UUID for a vfat partition (rather than set a specific one), for example to avoid conflicts after duplicating a partition with dd. In this case, a simple solution is to use gparted: right click on the partition, then "New UUID".

@tarabyte suggests that most people come to this page looking for a GUI-less (e.g. command line) way to do this on headless server. If that's the case, gparted shows that the command it's running under the hood when doing the above is something like:

    sudo mlabel -s -n :: -i /dev/nvme0n1p6
2
  • 1
    I think most of us are interested in a GUI-less (e.g. command line) way to do this on headless servers.
    – tarabyte
    May 18, 2020 at 22:26
  • If you have the ability to use gparted you'll need to install mtools using sudo apt-get install mtools or your distro's equivalent command. Otherwise the option to change the UUID will be grayed out.
    – aklingam
    Jun 8, 2021 at 2:29
3

Following on tommy's answer above, the offset for exFAT drives is 100. Other than that, the commands to change the ID for exFAT drives proceed almost verbatim to those for FAT16/32 drives, save a small fsck at the end. Remember, commands must be elevated with sudo or run in a root shell, and the /dev/sdc1 used in the example should be changed to match your device's block id.

You can check the volume ID with:

dd bs=1 skip=100 count=4 if=/dev/sdc1 2>/dev/null \
| xxd -plain -u \
| sed -r 's/(..)(..)(..)(..)/\4\3-\2\1/'

And the command to write a new ID becomes:

UUID="1234-ABCD"
printf "\x${UUID:7:2}\x${UUID:5:2}\x${UUID:2:2}\x${UUID:0:2}" \
| dd bs=1 seek=100 count=4 conv=notrunc of=/dev/sdc1

Finally, I had to recalculate the checksum, which fsck -a accomplished for me

fsck -a /dev/sdc1

I am very much obliged, tommy, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.