How To: Use Filesystem Labels in /etc/fstab

Filesystem label in /etc/fstab

Yesterday I have shown how to manage ext2/ext3/ext4 filesystem labels using e2label command. Continuing this topic, let’s update /etc/fstab file on my Ubuntu VM.

WARNING: don’t do this on a real server! try on a non-important virtual machine first, just to learn how to use commands, update fstab and so on. I’m using root filesystem (/) just because that’s the only filesystem I have on my Ubuntu VM.

Here’s how my /etc/fstab looks right now:

root@ubuntu:~ # cat /etc/fstab
# /etc/fstab: static file system information.
# 
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#           
# / was on /dev/sda1 during installation
UUID=ef0ca1f8-28cf-4baf-ada6-f2271aaece17 /               ext4    errors=remount-ro 0       1

Check filesystem label for / filesystem

If we run df -h command, we can see what device the root (/) filesystem is using:

root@ubuntu:~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            964M     0  964M   0% /dev
tmpfs           199M  1.5M  197M   1% /run
/dev/sda1        63G  8.7G   51G  15% /
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           991M     0  991M   0% /sys/fs/cgroup

Let’s use e2label to confirm label for /dev/sda1:

root@ubuntu:~ # e2label /dev/sda1
rootdisk

Perfect! Now that we know /dev/sda1 has label of “rootdisk“, let’s use this label to mount this filesystem going forward.

Update /etc/fstab to use filesystem labels

Editing the /etc/fstab file, comment out the UUID line for / filesystem (note the # at the start of the line now):

# UUID=ef0ca1f8-28cf-4baf-ada6-f2271aaece17 /               ext4    errors=remount-ro 0       1

and replace it with this:

LABEL=rootdisk    /               ext4    errors=remount-ro 0       1

Just to be sure things work as expected, let’s remount the filesystem rather than reboot the server:

root@ubuntu:~ # mount -o remount /

If you don’t get any errors back – this is working as expected.

See Also