This article is part of the 2010 Advent calendar series “24 Short Linux Hints”. This series focuses on little (and sometimes longer) tricks, tips and hints to solve common problems and to really improve your workflow.

On Linux devices and their partitions are named automatically when being plugged in. They have a base name (for SCSI devices it's sd), a device identifier (a lowercase letter) and a number for every partition. So normal SCSI devices could have entries like this

/dev/sda
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sdb
/dev/sdb1

for two disks: one with three partitions, another with just a single partition. These names depend on the order in which the devices are plugged in, so they are not static at all.

To identify disks, you can either plug them in and watch the changes in /dev/ or have a look at /dev/disk/by-id/ or /dev/disk/by-uuid/ where Udev creates symlinks for all disks currently plugged in. Each disk has a UUID (linked in /dev/disk/by-uuid/) and a type specific (not unique) ID (linked in /dev/disk/by-id/), which consists of things like the manufacturer's name, the serial number etc.

Neither the ID nor the UUID are always clear and you might still find yourself struggling with finding out the name of the device currently attached to your computer. One way to avoid this is to name your partitions. You can do this by using either e2label or tune2fs (for Ext2/3/4 file systems). To give the partition /dev/sdb1 the name documents, run either of these commands:

e2label /dev/sdb1 documents
tune2fs -L documents /dev/sdb1

You can also make use of mke2fs to label your partitions, but that would reformat your disk. Thus, this is only useful when naming fresh partitions directly without running a second command. For instance

Of course, labeling with e2*-like tools only works for Ext file systems, but there are similar tools for other file systems. For example ntfslabel for NTFS (from package ntfsprogs), dosfslabel for FAT (package dosfstools), xfs_admin -L (package xfsprogs) for XFS and many more.

Actually, you can also reference your disks by UUIDs instead of labels but not all file systems support per-partition UUIDs, for some you have to edit them manually in a hexeditor (e.g. FAT and NTFS). A UUID consists of a series of hexadecimal digits separated by hyphens, e.g., 345ae2f-56235de244-a52fc-52395. You can set the UUID for an Ext file system with tune2fs -U <uuid> where <uuid> is either a custom UUID, clear to clear/remove the UUID, random to generate a random UUID or time to generate a UUID based on the current time.

tune2fs -U 123-456 /dev/sdc1

creates a new UUID for our multimedia partition. You can now mount this partition either in your /etc/fstab

But there's also another less known way to name partitions. This method “names” partitions without naming them. The command-line tool devlabel creates a symlink to a partition by referencing its UUID (and if not available the SCSI or IDE identifier of the disk). devlabel is not a standard tool on most Linux distributions and must be installed before. As an example, let us create a symlink to our documents partition by executing

devlabel -d /dev/sdb1 -s /dev/documents

Now we can mount /dev/documents as usual. That's it, hope you learned a thing or two.