How to label disk in Linux with blkid

The purpose of this tutorial is to show how to label a hard disk in Linux. Labeling hard drives under a Linux system gives a user a better way to organize all of the block system’s devices on their system.

In this tutorial, you will see how to use the e2label and tune2fs commands to lavel a hard drive partition in Linux. You will also see how to use blkid to read these labels later on, and how to mount drives by their label in the fstab file. Read on to learn through examples.

In this tutorial you will learn:

  • How to use e2label and tune2fs commands
  • How to use blkid to read partition labels
  • How to mount hard drive partitions by label
How to label disk in Linux with blkid
How to label disk in Linux with blkid
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software e2label, tune2fs, ntfslabel, mkswap, blkid
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

How to label disk partition in Linux




On a Linux system, disk partitions are given a device name such as /dev/sda, /dev/sdb, /dev/sda5, etc. Basically, sdX (with X being some letter), and sometimes a number on the end.

As you can imagine, it gets hard and confusing to identify a disk with this naming system alone, especially if you have more than a few hard disks and partitions on your computer. This is why labeling a hard disk partition would come in handy. A label like MY_BACKUP is infinitely more helpful than /dev/sda1.

To see the device path of all your hard disk partitions, you can use the fdisk command.

$ sudo fdisk -l
Identifying the device paths of the hard disk partitions with fdisk command
Identifying the device paths of the hard disk partitions with fdisk command

Label disk partition examples

  1. The blkid command can be used to show the current partition label (if any) and UUID of the disk partition. Simply specify the device path of the partition you wish to see.
    $ blkid /dev/sda5
    /dev/sda5: UUID="a80ad9d4-90ff-4903-b34d-ca70d82762ed" TYPE="ext4" PARTUUID="75efe5f1-05"
    
  2. One way to add a label to a disk partition is with the e2label command. Use the syntax below to add a label to any disk partition of your choosing.
    $ sudo e2label /dev/sda5 "MY_BACKUP"
    
  3. Another way to add a label is with the tune2fs command. The following syntax would be used to add a label to our /dev/sda5 partition.
    $ sudo tune2fs -L "MY_BACKUP" /dev/sda5
    
  4. The e2label and tune2fs commands will work fine for ext2, ext3, and ext4 formatted partitions. To label a partition that’s been formatted as NTFS, you will need to use the ntfslabel instead.
    $ sudo ntfslabel /dev/sda5 NTFS_DRIVE
    
  5. To label a SWAP partition, you can use the mkswap command and the following syntax.
    $ sudo mkswap -L SWAP_PARTITION /dev/sda5
    

Mount hard drive partitions by label




Now we are able to refer to /dev/sda5 as MY_BACKUP, after adding that label to the disk partition in an earlier step. To do so, we would edit the /etc/fstab file and add the following line:

LABEL=MY_BACKUP /mount/point           ext4    defaults        0       2

Closing Thoughts

In this tutorial, we saw how to add a label to a disk partition in Linux. This included use of the e2label, tune2fs, ntfslabel, and mkswap commands, depending on the type of file system that is being used on the partition that you want to label. We also learned how to use the blkid command to view the labels of our disk partitions.



Comments and Discussions
Linux Forum