The other day I reformatted my external USB hard drive to use as a backup device for my linux systems. I was trying to come up with a single command to do the backup, but each system that I plugged the device into gave it a different device name. One would call it "/dev/sdc", while another called it "/dev/sdb". I wanted a consistent way to have it mounted at the same location each time so I could keep one backup script for all the systems. Thankfully I'm far from the first person to have encountered this program, and the Linux community has come up with a solution.
That solution is based on the UUID - Universally Unique IDentifier - a string of 32 HEX characters that are unique in most cases. For the most part, these will be unique - see the Wikipedia entry for a full stastical analysis.
In general, each filesystem has a UUID stamped into it when the filesystem is created. These UUIDs look something like this when we view them:
The UUID assigned to each partition is visible by using the"blkid" command (found in the util-linux package).
I plugged in my USB drive with the EXT4 filesystem, then ran blkid and got output like this:
root@titan:~# blkid /dev/sda1: UUID="e48416fc-f4d0-4455-8f8f-8ba0f69fbc11" TYPE="ext4" /dev/sdb1: UUID="00a5db59-7cfe-4d6a-9237-92afaa64ca19" TYPE="ext3" /dev/sdb5: UUID="40edf22c-a364-3068-84af-d2714cf98350" TYPE="ext4" /dev/sdc1: UUID="f45eb6da-ca86-4837-afed-0fc4023b43f6" TYPE="ext4"
In my case, I wanted my external USB drive (/dev/sdc1 in the output above) to always be mounted in /mnt/backup. If the system always adds this device as "/dev/sdc1", then this line in the /etc/fstab will mount it to "/mnt/backup" each time:
/dev/sdc1 /mnt/backup ext4 defaults 0 2
Or, to mount it from the command line:
mount /dev/sdc1 /mnt/backup
But, I want to automate my backup script including having it check and mount the drive at the proper location. Since each system uses a different /dev/sdXX name, I'll have to use UUIDs. The /etc/fstab entry is fairly simple:
UUID=f45eb6da-ca86-4837-afed-0fc4023b43f6 /mnt/backup ext4 defaults 0 2
And mounting from the command line is also similar:
mount /dev/disk/by-uuid/f45eb6da-ca86-4837-afed-0fc4023b43f6 /mnt/backup