Mount file systems at boot by UUID or label in linux

Configuring your Linux system to mount file systems at boot using Universally Unique ID (UUID) or label is a good practice because it ensures that the correct filesystem are mounted even if disk device names change or if you move the storage devices to different ports. Here’s how you can do it:

Using UUID to Mount File Systems at Boot:
1.Identify the UUID:

Use the blkid command to list the UUIDs of your storage devices and partitions. Open a terminal and run:

sudo blkid

The output will show UUIDs for each partition, such as /dev/sda5: LABEL(optional)=”given label”:UUID=”your-uuid-here” TYPE=”filesystem-type”`.

2. Edit /etc/fstab:

Open the /etc/fstab file in a text editor with root privileges. You can use a command like sudo nano or sudo vi:

sudo nano /etc/fstab

or

sudo vi /etc/fstab

 

3. Update the /etc/fstab File:

In the /etc/fstab file, replace the device name (/dev/sdX) with the UUID of the partition you want to mount. Modify the entry for each partition accordingly. The typical format in /etc/fstab looks like this:

UUID=your-uuid-here /mount/point filesystem-type options 0 0

For example, to mount a partition with UUID “fdbe2371-5239-48dc-9ff1-1288ac86f776” to /opt/test using xfs, your entry might look like this:

UUID= fdbe2371-5239-48dc-9ff1-1288ac86f776 /opt/test xfs defaults 0 0

 

4. Save and Exit:

Save the changes and exit the text editor.

Using Labels to Mount File Systems at Boot:
1.Set a Label for the Partition:

If the partition doesn’t already have a label, you can set one using the e2label or tune2fs or mkfs.ext4 with option -L command for ext filesystems. For example, to set a label “mydata” for an ext4 partition, use:

sudo e2label /dev/sdX mydata

You can label the ext4 filesystem with a name for easier identification. Use the -L option followed by the label name when formatting:

sudo mkfs.ext4 -L mydata /dev/sdXn

Replace /dev/sdX with the appropriate device identifier.

2. Edit /etc/fstab:

Open the /etc/fstab file in a text editor with root privileges:

sudo nano /etc/fstab

or

sudo vi /fstab
3. Update the /etc/fstab File:

Replace the device name (/dev/sdX) with the partition label. Modify the entry for each partition accordingly. The typical format in /etc/fstab for using labels looks like this:

LABEL=label-name /mount/point filesystem-type options 0 0

For example, to mount a partition with the label “mydata” to “/opt/software” using ext4, your entry might look like this:

LABEL=mydata /opt/software ext4 defaults 0 0

4. Save and Exit:

Save the changes and exit the text editor.

After making these changes to the /etc/fstab file, the system will mount the specified file systems at boot using either the UUID or labels you have configured. This approach is more reliable than using device names and ensures that the correct partitions are mounted, even if the device order changes or disks are added or removed.