Adding a Disk to /etc/fstab Using blkid in Vim

Mounting disks via UUID (Universally Unique Identifier) is the most reliable method for Linux systems. This guide demonstrates how to pull disk information directly into your configuration file to minimize typing errors.

Prerequisites

  • Root or sudo privileges.

  • The device name of your disk (e.g., /dev/sdb).

  • A created mount point (e.g., /mnt/data).


Step 1: Open the File

Open the filesystem table configuration with vim:

Bash

sudo vim /etc/fstab

Step 2: Import Disk Information

Instead of switching terminals, you can pull the blkid data directly into your current buffer.

  1. Ensure you are in NORMAL mode (press Esc).

  2. Move the cursor to the bottom of the file.

  3. Type the following command and press Enter:

    Vim Script

    :r !blkid /dev/sdb
    

    Substitute /dev/sdb with your actual device name.

Example of the inserted raw text:

/dev/sdb: UUID="a1b2c3d4-e5f6-7890-abcd-1234567890ef" TYPE="ext4" PARTUUID="..."

Step 3: Format the Entry

The raw output needs to be cleaned up to match the fstab syntax. An fstab entry follows this specific structure:

Field

Description

UUID

The unique ID (Remove quotes and the /dev/sdb: prefix)

Mount Point

The directory where the disk will live (e.g., /data)

Type

The filesystem type (e.g., ext4, xfs)

Options

Usually defaults

Dump

Set to 0 (disables backup)

Pass

Set to 2 (for non-root partitions)

Final Formatted Example:

UUID=a1b2c3d4-e5f6-7890-abcd-1234567890ef /data ext4 defaults 0 2

Step 4: Save and Exit

Once the line is cleaned up and the unnecessary PARTUUID or device tags are deleted:

  1. Press Esc.

  2. Type :wq and press Enter.


Step 5: Critical Safety Check

Never reboot without testing your fstab file. An error in this file can prevent your system from booting.

Run the following command:

Bash

sudo mount -a

Interpreting Results:

  • No Output: Success! Your disk is mounted and the configuration is valid.

  • Error Message: Do not reboot. Re-open /etc/fstab and check for typos, missing spaces, or incorrect UUIDs.

Updated on