How to label a partition or volume on Linux with e2label

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. The tool we can use for this task is the e2label command.

In this tutorial, you will see how to use the e2label command to label 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 command to label hard drive partitions
  • How to use blkid to read partition labels
  • How to mount hard drive partitions by label
How to label a partition or volume on Linux with e2label
How to label a partition or volume on Linux with e2label
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software e2label
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
NOTE
e2label will only work for hard drive partitions that have been formatted with the ext4, ext3, or ext2 file systems. It will not work for other file system types, such as NTFS.

How to label a partition or volume on Linux with e2label




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/sda2.

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 or volume with e2label examples

  1. Use the e2label command and the syntax below to add a label to any disk partition of your choosing. Just substitute your own partition in place of /dev/sdX below, and your own label in place of MY_BACKUP.
    $ sudo e2label /dev/sdX "MY_BACKUP"
    

    Please note that the maximum label length is 16 bytes, in other words 16 characters.

  2. Let’s check the partition label name again:


    $ sudo e2label /dev/sdX
    MY_BACKUP
    
  3. To list label name for all partitions or volumes you may try to use blkid command:
    $ sudo blkid
    /dev/sda5: UUID="f2756986-3749-4bd3-a6e5-f6a867cb4ebb" TYPE="swap" 
    /dev/sda1: UUID="60254c19-67c0-404b-9743-1b8b7f0b11cb" TYPE="ext4" LABEL="Boot"
    
  4. Lastly, you can remove a partition label name by supplying an empty string to your e2label command:
    $ sudo e2label /dev/sdX ""
    

Mount hard drive partitions by label

Now we are able to refer to /dev/sdX 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 label a partition or volume with the e2label command on a Linux system. This is a handy tool for system administrators that have multiple hard disks installed and need to keep them straight. Rather than remembering the block device names, we can assign helpful and informative labels to each partition, so we are sure to find the right one when we need it.



Comments and Discussions
Linux Forum