Change UUID under Linux?

3

I need to create and assign a new UUID to an ordinary disk partition under Linux.

What tools are available and how do I use them?

goangit

Posted 2014-12-12T06:48:36.543

Reputation: 183

Do you need to change the UUID of a partition (by-partuuid) or a filesystem (by-uuid)? – user1686 – 2014-12-12T09:07:50.267

Answers

2

Those working with ordinary disks can do this simply with tune2fs and uuidgen.

Example: using a not currently mounted device /dev/sdb1

tune2fs /dev/sdb1 -U `uuidgen`

The new UUID will be immediately visible under

ls -l /dev/disk/by-uuid

if, say, you need to copy the value to /etc/fstab for automatic mount.

However, blkid will continue to (erroneously) report the old value until the cache is updated (on reboot, for example; though the cache may be bypassed with sudo blkid -c /dev/null).

Alternatively, the new UUID may be obtained via udev with

sudo lsblk -fo UUID /dev/sdb1

Those working with LVM disks might like to check the answer here.

goangit

Posted 2014-12-12T06:48:36.543

Reputation: 183

1So it's a filesystem specific type of label? tune2fs works for ext2/3/4, or do other filesystems even have/use block id's? – Xen2050 – 2014-12-12T07:49:46.590

It's a good question and one I hadn't considered. The filesystem I had in mind when writing is indeed ext4, but I am certainly open to answers for other filesystem contexts. – goangit – 2014-12-12T07:52:35.953

2Yes, many filesystems have unique IDs (though they're not always in UUID format). Use tune2fs -U for ext; xfs_admin -U for xfs; jfs_tune -U for JFS; ntfslabel --new-serial for NTFS, and so on. (I think btrfs might not allow changing the UUID as it is extensively used internally.) Likewise, GPT partitions and LVM volumes have their own unique IDs. (The former is called a "PARTUUID" and can be seen in by-partuuid.) – user1686 – 2014-12-12T09:03:29.027

1Regarding blkid, it works by reading directly from the disk, and because of that it needs to cache the retrieved information somewhere accessible to ordinary users. You can bypass the cache using blkid -c/dev/null as root. Though, it's better to use tools like lsblk -f, which queries udev's automatically-updated cache. – user1686 – 2014-12-12T09:07:16.640

Thanks for the insights into blkid's cache, much appreciated. Answer updated to reflect this input. – goangit – 2014-12-12T10:35:37.813