Introduction / Why This Is Needed
When working with disks in Linux, you encounter two primary identification methods: device names (e.g., /dev/sda1) and UUIDs (Universally Unique Identifier). Device names are convenient for manual operations but unreliable for automatic mounting—they can change when a new disk is added. A UUID, however, is a unique identifier "baked into" the filesystem when it's created. It remains unchanged regardless of how the system names the disk.
This guide explains why UUID is the standard for /etc/fstab and systemd units, and how to use it correctly to avoid boot errors or incorrect data mounting.
Requirements / Preparation
Before you begin, ensure:
- You have terminal access with
sudoprivileges. - The disk or partition is already formatted (has a filesystem, e.g., ext4, xfs, ntfs).
- You know the mount point (e.g.,
/mnt/dataor/home) where you want to attach the disk.
Step 1: How Device Names and UUIDs Work
Device Names (/dev/sdX, /dev/nvme0n1p1)
The system assigns disk names in the order they are detected. If you add a new SATA disk, existing ones may "shift":
- You had
/dev/sda(system) and/dev/sdb(data). - You plug in a USB drive → it becomes
/dev/sdc, and/dev/sdbmight stay in place or become/dev/sdcif the USB drive was detected first. - Problem: An
/etc/fstabentry like/dev/sdb1 /mnt/data ext4 defaults 0 2after a reboot might point to the wrong disk or cause a "not found" error.
UUID
A unique 128-bit identifier generated when the filesystem is created (mkfs). Example:
UUID=3e4f1a2b-5c6d-7e8f-9a0b-1c2d3e4f5a6b
This identifier is tied to the data, not the disk's "order." It stays the same even if you reconnect the disk to a different port or a different system.
💡 Tip: Always use UUID for entries in
/etc/fstab. This guarantees your data will mount correctly after any hardware reconfiguration.
Step 2: Find the Disk or Partition's UUID
There are several ways. The most reliable is blkid (requires sudo to show all disks):
sudo blkid
Example output:
/dev/sda1: UUID="a1b2c3d4-e5f6-7890-abcd-ef1234567890" TYPE="ext4" PARTUUID="..."
/dev/sdb1: UUID="f1e2d3c4-b5a6-0987-cdef-0123456789ab" TYPE="xfs"
/dev/nvme0n1p1: UUID="12345678-90ab-cdef-1234-567890abcdef" TYPE="vfat"
Alternative without sudo (will show only mounted filesystems and those with read permissions):
lsblk -f
Output will be a table: NAME, FSTYPE, LABEL, UUID, MOUNTPOINT.
Which UUID to use?
- If you are mounting an entire partition (e.g.,
/dev/sdb1), use that partition's UUID. - If you are mounting an LVM logical volume (
/dev/mapper/vg0-lvdata), that volume also has its own UUID (find it viablkid /dev/mapper/vg0-lvdata).
Note down the required UUID (copy from the output, without the quotes).
Step 3: Configure Mounting by UUID in fstab
The /etc/fstab file (file systems table) defines how the system mounts disks at boot.
- Open the file in a text editor with
sudo:
sudo nano /etc/fstab
(or sudo vim /etc/fstab, sudo vi /etc/fstab)
- Find the line corresponding to your disk. It might look like:
/dev/sdb1 /mnt/data ext4 defaults 0 2
- Replace
/dev/sdb1withUUID=<your_uuid>. For example:
UUID=f1e2d3c4-b5a6-0987-cdef-0123456789ab /mnt/data ext4 defaults 0 2
- Save the file and exit.
Explanation of fstab fields:
- UUID=... — the device (or its stable identifier).
- /mnt/data — the mount point (must exist; create it via
sudo mkdir -p /mnt/data). - ext4 — filesystem type (you can specify
auto, but explicit is better). - defaults — mount options (rw, suid, dev, exec, auto, nouser, async).
defaultsis sufficient for most cases. - 0 — dump (obsolete, set to 0).
- 2 — filesystem check order at boot (0 — don't check, 1 — only root, 2 — all others).
Step 4: Verify the Entry is Correct
Don't reboot immediately! Check the syntax and mountability:
sudo mount -a
This command attempts to mount all filesystems from /etc/fstab according to the settings.
Possible results:
- No output — no errors; everything mounted (or was already mounted).
- Error:
mount: /mnt/data: special device UUID=... does not exist.- Cause: UUID is incorrect, disk is not connected, or filesystem is corrupted.
- Solution: check the UUID via
sudo blkid, ensure the disk is visible inlsblk.
- Error:
mount: /mnt/data: mount point does not exist.- Cause: the mount point
/mnt/datadoes not exist. - Solution:
sudo mkdir -p /mnt/data.
- Cause: the mount point
After a successful mount -a, verify the disk is mounted:
df -hT /mnt/data
The output will show the filesystem, size, used space, and mount point.
Step 5: Using Symbolic Links in /dev/disk/by-uuid (Alternative Method)
The system automatically creates stable symbolic links in /dev/disk/by-uuid/. For example:
/dev/disk/by-uuid/f1e2d3c4-b5a6-0987-cdef-0123456789ab -> ../../sdb1
You can use this path directly in fstab:
/dev/disk/by-uuid/f1e2d3c4-b5a6-0987-cdef-0123456789ab /mnt/data ext4 defaults 0 2
This is equivalent to the UUID=... entry, but some system utilities (e.g., systemd) prefer UUID=.
How does it work? The kernel creates these links upon detecting block devices. They always point to the current /dev/sdX or /dev/nvme..., even if the name changes.
Verify the Result
- Reboot the system:
sudo reboot
- After booting, check if the disk mounted:
mount | grep /mnt/data
or
df -hT | grep /mnt/data
You should see a line with your filesystem and mount point.
- Ensure the data is accessible:
ls /mnt/data
If the disk did not mount after reboot — see the "Potential Issues" section.
Potential Issues
1. Boot Error: "Waiting for /dev/disk/by-uuid/..."
- Cause: The
fstabspecifies a UUID for a disk that is physically absent (e.g., an external disk is not connected). - Solution:
- If the disk should always be present — check the connection and cables.
- If the disk is optional — add the
nofailoption to thefstaboptions field:
This allows the system to continue booting even if the disk is not found.UUID=... /mnt/data ext4 defaults,nofail 0 2
2. Error "wrong fs type, bad option, bad superblock"
- Cause: Incorrect filesystem type specified (
ext4instead ofxfs), corrupted superblock, or the disk is not formatted. - Solution:
- Check the filesystem type via
sudo blkidorlsblk -f. - If the filesystem is corrupted, try
sudo fsck.<fs_type> /dev/sdX1(e.g.,fsck.ext4). - Ensure you are not trying to mount a swap partition (
TYPE="swap").
- Check the filesystem type via
3. Disk mounts to a different location after reboot
- Cause:
fstabuses a device name (/dev/sdb1) that has changed. - Solution: Replace the device name with
UUID=...or/dev/disk/by-uuid/...and runsudo mount -a.
4. No access to files after mounting
- Cause: Incorrect permissions on the mount point or the disk itself.
- Solution:
- Check permissions:
ls -ld /mnt/data. - Change the owner (if it's your home partition):
sudo chown -R $USER:$USER /mnt/data. - For NTFS partitions (via
ntfs-3g), permissions can be problematic — configure mount options infstab(e.g.,uid=1000,gid=1000,umask=022).
- Check permissions:
5. blkid does not show a UUID
- Cause: The filesystem does not support UUIDs (very old) or the disk is not formatted.
- Solution: Format the disk (data will be erased!):
sudo mkfs.ext4 /dev/sdX1 # for ext4
After formatting, the UUID will be created automatically.
Advanced Option: systemd Mount Units
Instead of fstab, you can use individual systemd mount units. This provides more control (dependencies, timeouts). Create a file /etc/systemd/system/mnt-data.mount:
[Unit]
Description=Mount data disk
After=network.target
[Mount]
What=UUID=f1e2d3c4-b5a6-0987-cdef-0123456789ab
Where=/mnt/data
Type=ext4
Options=defaults
[Install]
WantedBy=multi-user.target
Enable and start it:
sudo systemctl enable --now mnt-data.mount
This method is particularly useful for disks that should mount later in the boot process or for complex dependencies.
Conclusion
Using UUIDs for disk mounting in Linux is a simple and reliable practice that protects against issues caused by changing device names. Key steps:
- Find the UUID via
sudo blkidorlsblk -f. - Write
UUID=<value>in/etc/fstabinstead of/dev/sdX1. - Verify with
sudo mount -aand a reboot.
Now your disks will mount stably, even if you add a new hard drive or rearrange SATA ports.