How to retrieve and change partition’s UUID Universally Unique Identifier on linux

Hard drive partitions on Linux systems rely on a UUID (universally unique identifier) for unique labels. This is basically a unique string of characters that the operating system will use to identify your hard disk partitions and other storage components.

You can see this for yourself by examining the /etc/fstab file on your own system.

$ grep UUID /etc/fstab

In this guide, we’ll go over several command line methods to retrieve the UUIDs of hard disk partitions. We’ll also show you how to generate UUIDs and change a partition’s UUID.

In this tutorial you will learn:

  • How to retrieve, generate, and change the UUID of a partition

Retrieving and setting a new UUID for a partition on Linux

Retrieving and setting a new UUID for a partition on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software tune2fs, uuid
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Retrieve UUID

There are several different ways you can retrieve the UUID for a hard drive partition on Linux. Check out some of the commands below to get started.

  1. Use the blkid command to see the UUID of all partitions.
    # blkid

    or you can specify an argument to retrieve a single partition UUID:

    # blkid /dev/sda2
  2. List the contents of the /dev/disk/by-uuid/ directory.
    # ls -l /dev/disk/by-uuid/
  3. Retrieve partition UUIDs with the udevadm command.
    # udevadm info -q all -n /dev/sda2 | grep uuid
  4. The hwinfo command can also be used to retrieve the information, assuming that the program has already been installed on your system.
    # hwinfo --block

Change UUID

Now that you know how to retrieve your current UUIDs, let’s talk about how to change a partition’s UUID.

Use the following tune2fs command to change the UUID of a partition. In this example, we will change the UUID for partition /dev/sda1.

# tune2fs /dev/sda1 -U random

Then, confirm the changes with one of the commands from the previous section.

# blkid /dev/sda1

You may also use the uuid command (provided you have it installed) if you want to manually generate a UUID or be given more options. For example, you could use the following command to generate a UUID.

# uuid

And assign a particular UUID with tune2fs:

# tune2fs /dev/sda1 -U 266584be-d7b7-11eb-8c76-c3eef48c7257

Or use the uuid command to generate a UUID inside the tune2fs command:

# tune2fs /dev/sda1 -U `uuid`

Any of the three methods work fine and will allow you to set a new UUID to the desired partition.

Closing Thoughts

In this guide, we saw how to retrieve and change the UUID of a partition on Linux. We also saw how to manually generate UUIDs with the uuid command. Using the tune2fs command, we can set a new, random UUID to a hard drive partition, or a particular one that we’ve generated via the uuid command.

Comments and Discussions
Linux Forum