35

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?

5 Answers 5

20

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.

5
  • 16
    The servial number of a vfat device can be changed via the mlabel command of the mtools.
    – liquidat
    Apr 24, 2018 at 18:22
  • 5
    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
  • I think there is a typo in the sgdisk example, it should be /dev/sdc1 I believe – the 1 is missing. May 2 at 6:45
  • @PhilippLudwig, the command as specified is correct. I even address the point you raise in my answer. The sgdisk command takes a full-disk device, like /dev/sdc, as an option, not a partition device. (It can also take a disk device image as an option.) The partition number to be modified is specified in this example as part of the -u option -- note that it begins with 1:, which is the partition number. This is also explicitly noted in my answer.
    – Rod Smith
    Jul 22 at 17:33
44

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

8
  • 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
  • 3
    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
  • @therealmarv Did not work for me for swapping external storage in Android, as my Android 11 says there is a problem and wants to reformat. With MicroSD card reader in Windows and Linux there is no problem
    – Paul
    Sep 8, 2022 at 13:28
20

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
  • 2
    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
  • 1
    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
5

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.

1
  • To recalculate the checksum, I had to do exfatfsck -p /dev/sdc1. Also I had to get exfat-utils_1.3.0 for that, as my OS had only 1.2.8 by default, with which it does not work
    – Paul
    Sep 8, 2022 at 13:17
3

I could not change it fat32/16 vfat even from gparted gui. However after installing mtools in arch linux which contains mlabel it allowed me to change it from gui.

mlabel -N aaaa1111 -i /dev/sdb1 ::

its here: https://unix.stackexchange.com/questions/261236/change-fat16-partition-uuid

1
  • :: preceeds the label. Provide one, or you'll end up with an empty volume label!
    – Wil
    Jan 17 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .