Home >System Tutorial >LINUX >How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide

How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide

Lisa Kudrow
Lisa KudrowOriginal
2025-03-05 11:20:09302browse

If you’ve just plugged in an external USB drive to your Linux system and are wondering how to make it accessible, you’re in the right place! Mounting a drive in Linux might sound technical, but it’s actually quite simple once you understand the basics. In this guide, we’ll walk you through the steps to permanently mount an External Drive in Linux using fstab file.

We will also cover best practices like using UUIDs, enabling TRIM, and setting a more secure umask value.

Table of Contents

What Does Mounting Mean?

In Linux, "mounting" a drive means making it accessible to the system. When you plug in an external drive, Linux detects it, but you need to tell the system where to “attach” it in the file system. This is done by creating a mount point (a directory) and linking the drive to it.

What is Fstab?

The /etc/fstab (short for file systems table) is a configuration file in Linux that defines how and where storage devices and partitions are mounted. It tells the system which drives to mount, where to mount them, and what options to use during the mounting process.

Why is it Important?

  • Automates Mounting: Drives listed in /etc/fstab are automatically mounted at boot, so you don’t have to manually mount them every time.
  • Customizable Options: You can specify mount options like read/write permissions, file system type, and error-handling behavior.
  • Centralized Configuration: All mount points and their settings are stored in one place, making it easy to manage.

Structure of /etc/fstab:

Each line in the file represents a file system or partition and has six fields:

  1. Device: The partition or drive to mount (e.g., /dev/sdb1, UUID=1234-5678, or LABEL=MyDrive).
  2. Mount Point: The directory where the drive will be accessible (e.g., /mnt/mydrive).
  3. File System Type: The type of file system (e.g., ext4, ntfs, exfat).
  4. Mount Options: A comma-separated list of options (e.g., defaults, noatime, nofail).
  5. Dump: Used by the dump utility for backups (0 means no backup).
  6. Fsck Order: Specifies the order for file system checks (0 means no check, 1 is for root, 2 is for other drives).

Example Entry:

Here’s an example of an /etc/fstab entry:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

Here,

  • UUID=1234-5678: The drive to mount (identified by its UUID).
  • /mnt/mydrive: The directory where the drive will be mounted.
  • ext4: The file system type.
  • defaults,noatime: Mount options (default settings no access time updates).
  • 0: Disables backups with the dump utility.
  • 2: Specifies the order for file system checks.

In summary, the /etc/fstab file is a powerful tool for managing how and where drives are mounted in Linux. By understanding its structure and options, you can automate mounting and customize your system’s storage setup

For more details about Fstab, please check the following guide:

  • An Introduction to Linux /etc/fstab file

Let us now see how to permanently mount a drive in Linux.

Steps to Permanently Mount External Drives in Linux with fstab

Step 1: Backup Fstab File

Before editing the /etc/fstab file, it is strongly recommended to take a backup. If something goes wrong (e.g., a typo or incorrect configuration), your system might fail to boot properly. Having a backup allows you to easily revert to the original file and fix the issue.

Backup /etc/fstab:

From the Terminal, run the following command to create a backup:

sudo cp /etc/fstab /etc/fstab.backup

This creates a copy of the fstab file named fstab.backup in the same directory.

Restore from Backup:

If you make a mistake and need to restore the original fstab file:

Boot into a recovery mode or use a live USB if your system fails to boot.

Open a terminal and restore the backup using command:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

Reboot your system:

sudo cp /etc/fstab /etc/fstab.backup

Always Test Before Rebooting

After editing /etc/fstab, always test the configuration with:

sudo cp /etc/fstab.backup /etc/fstab

This command attempts to mount all file systems listed in fstab. If there are no errors, your changes are likely safe. If there are errors, fix them before rebooting.

Always follow this workflow:

  1. Backup /etc/fstab.
  2. Edit /etc/fstab.
  3. Test with sudo mount -a.
  4. Reboot only if there are no errors.

By following these steps, you’ll avoid most common issues when mounting external drives in Linux.

Step 2: Identify Your Drive

Before mounting, you need to know the name of your drive. Linux assigns names like /dev/sdb1 or /dev/nvme0n1p1 to drives.

To find your drive, open a terminal and run:

sudo reboot

This will list all the drives and partitions on your system. Look for your external drive in the output. It will usually have a size that matches your drive’s capacity.

sudo mount -a

As you see in the above output, there are two drives are connected in my system. The internal drive is /dev/nvme0n1 and the external drive is /dev/sda.

In this example, sda1 is the device that we are going to mount.

Step 3: Create a Mount Point

A mount point is simply a directory where the drive will be accessible. You can create one anywhere, but it’s common to use /mnt or /media. For example:

lsblk

Replace /media/ostechnix/SK_WD_SSD with a name that makes sense for your drive.

Step 4: Mount the Drive Temporarily

To test the drive, you can mount it temporarily. Use the mount command:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 931.5G  0 disk 
├─sda1        8:1    0 931.5G  0 part 
└─sda2        8:2    0    32M  0 part 
nvme0n1     259:0    0 465.8G  0 disk 
├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
├─nvme0n1p2 259:2    0 464.3G  0 part /
└─nvme0n1p3 259:3    0   976M  0 part [SWAP]

Replace /dev/sda1 with the correct name of your drive. Now, your drive should be accessible at /media/ostechnix/SK_WD_SSD.

Step 5: Permanently Mount the Drive Using fstab

To ensure your drive mounts automatically every time you start your computer, you need to edit the /etc/fstab file. This file tells Linux which drives to mount and where.

Here’s an example of an /etc/fstab entry for an external NVMe SSD with an exFAT file system:

sudo mkdir /media/ostechnix/SK_WD_SSD

Please note that I have labelled my external drive as "SK_WD_SSD" when I format it with exFAT file system. In your case, it could be different.

This entry works, but let’s improve it by following best practices:

1. Use UUID Instead of Label:

UUIDs are more reliable because they don’t change. Find your drive’s UUID with:

sudo mount /dev/sda1 /media/ostechnix/SK_WD_SSD

Sample Output:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

In this example, we are going to mount the /dev/sda1 that has UUID "2A81-C276".

2. Set a Secure umask:

Use umask=022 to give the owner full access and others read-only access.

3. Prevent Boot Errors:

Add the nofail option to prevent boot errors if the drive is disconnected.

Here’s the final and improved /etc/fstab entry:

sudo cp /etc/fstab /etc/fstab.backup

This /etc/fstab entry:

  1. Mounts the drive with UUID 2A81-C276 to /media/ostechnix/SK_WD_SSD.
  2. Uses the exfat file system.
  3. Applies mount options for safety (errors=remount-ro), performance (noatime, nodiratime), and user access (users, umask=022).
  4. Prevents boot errors if the drive is disconnected (nofail).
  5. Disables backups (0) and schedules file system checks after the root file system (2).

In a nutshell, this line is used to permanently mount a drive in Linux.

Here's the detailed break down of the above fstab entry:

1. UUID=2A81-C276

It specifies the drive to be mounted using its UUID (Universally Unique Identifier).

UUIDs are unique and don’t change, unlike device names (e.g., /dev/sda1, /dev/sdb1), which can vary depending on the order of drive connections.

You can run sudo blkid to list all drives and their UUIDs.

2. /media/ostechnix/SK_WD_SSD

It specifies the mount point, which is the directory where the drive will be accessible. This is where you’ll access your files after the drive is mounted.

You can change this to any directory you prefer (e.g., /mnt/mydrive).

3. exfat

It specifies the file system type of the drive. Linux needs to know how to read and write to the drive. Common file systems include ext4, ntfs, vfat, and exfat.

Replace exfat with the correct file system type for your drive.

4. errors=remount-ro,defaults,users,noatime,nodiratime,umask=022,nofail

These are mount options that control how the drive is mounted. This provides a standard set of options for most use cases.

Let’s break them down:

errors=remount-ro:

If errors are detected, the drive is remounted as read-only to prevent data corruption. it protects your data in case of file system errors.

defaults:

This enables a set of default mount options, including:

  • rw: Read and write access.
  • suid: Allows SUID and SGID bits to take effect.
  • dev: Allows interpretation of device files on the file system.
  • exec: Allows execution of binaries.
  • auto: Automatically mounts the drive at boot.
  • nouser: Only root can mount the drive (overridden by users in this case).
  • async: File system operations are done asynchronously.

users

It allows non-root users to mount and unmount the drive. Useful for external drives that need to be mounted by regular users.

noatime and nodiratime

It will prevent the system from updating access times on files and directories. It improves performance and reduces wear on SSDs by minimizing write operations.

umask=022

It sets file permissions for the drive and controls who can access and modify files on the drive.

Here umask=022 means:

  • Owner: Read, write, and execute (rwx).
  • Group and Others: Read and execute (r-x)

nofail

It prevents the system from throwing errors or failing to boot if the drive is not connected. It is essential for external drives that may not always be plugged in.

5. 0

It controls whether the drive is backed up by the dump utility. Most users don’t use dump, so 0 is typically fine.

  • 0: Disables backups (recommended for most users).
  • 1: Enables backups.

6. 2

It specifies the order in which the file system is checked by fsck during boot.

  • 0: No check.
  • 1: Check first (used for the root file system).
  • 2: Check after the root file system.

This ensures file system integrity for non-root drives.

Step 6: Add Entry to Fstab

To add the above entry in Fstab, open the /etc/fstab file in a text editor:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

Add the line at the end of the file.

Here are my system's fstab entries for reference:

sudo cp /etc/fstab /etc/fstab.backup

How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide

Press CTRL O followed by CTRL X to save the file and exit the editor.

Step 7: Test the Configuration

Before rebooting, test your setup to make sure there are no errors:

sudo cp /etc/fstab.backup /etc/fstab

If there are no errors, your drive is ready to use!

You can verify this using the df command:

sudo reboot

This command will will show you the list of mounted drives along with their mount points on your system:

sudo mount -a

As you can see in the above output, the /dev/sda1 is mounted on /media/ostechnix/SK_WD_SSD.

Step 8: Enable Periodic TRIM

If you’re using an SSD, enable periodic TRIM to maintain performance.

To do so, run:

lsblk

You can also set up a cron job to run this automatically. For example, to run TRIM weekly, add this to your crontab:

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 931.5G  0 disk 
├─sda1        8:1    0 931.5G  0 part 
└─sda2        8:2    0    32M  0 part 
nvme0n1     259:0    0 465.8G  0 disk 
├─nvme0n1p1 259:1    0   512M  0 part /boot/efi
├─nvme0n1p2 259:2    0 464.3G  0 part /
└─nvme0n1p3 259:3    0   976M  0 part [SWAP]

Step 9: Unmounting the Drive (Optional)

When you’re done using the drive, unmount it safely:

sudo mkdir /media/ostechnix/SK_WD_SSD

You can also do it from your GUI File manager (Right click and choose Unmount or Safely Remove).

Choosing the Correct TRIM Method

There are generally two TRIM methods. They are Continuous and Periodic TRIMs.

Choosing between continuous TRIM and periodic TRIM depends on your specific use case, the workload on your SSD, and your preference for performance versus longevity.

What is Continuous TRIM?

Continuous TRIM (enabled by the discard mount option in /etc/fstab) sends TRIM commands to the SSD in real-time as files are deleted. This keeps the SSD’s free space immediately available for new writes.

Pros:

  • Immediate Space Reclamation: The SSD knows which blocks are free right away, which can improve write performance over time.
  • Consistent Performance: Helps maintain consistent performance by preventing the SSD from having to deal with stale data.

Cons:

  • Increased Wear: Frequent TRIM operations can increase wear on the SSD, though modern SSDs are designed to handle this.
  • Potential Latency: Real-time TRIM operations can introduce slight latency during file deletions, which might be noticeable in high-performance workloads.

When to Use Continuous TRIM?

  • If you frequently delete large amounts of data and want to maintain optimal performance.
  • If your workload involves many small, random writes and deletions.

How to Enable Continuous TRIM

Add the discard option to your /etc/fstab entry:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

What is Periodic TRIM?

Periodic TRIM (enabled by running fstrim manually or via a scheduled job) sends TRIM commands to the SSD at regular intervals (e.g., daily or weekly).

Pros:

  • Reduced Wear: Fewer TRIM operations mean less wear on the SSD, which can extend its lifespan.
  • No Latency Overhead: TRIM operations are batched and run at a convenient time, avoiding potential latency during file deletions.

Cons:

  • Delayed Space Reclamation: Free space isn’t immediately available for new writes, which could temporarily reduce performance.
  • Manual or Scheduled Setup: Requires setting up a cron job or systemd timer to run fstrim periodically.

When to Use Periodic TRIM?

  • If you want to minimize wear on the SSD and don’t need immediate space reclamation.
  • If your workload involves mostly large, sequential writes and deletions.

How to Enable Periodic TRIM

Install util-linux (if not already installed):

sudo cp /etc/fstab /etc/fstab.backup

Run fstrim Manually:

sudo cp /etc/fstab.backup /etc/fstab

Set Up a Cron Job (e.g., weekly):

Open the crontab editor:

sudo reboot

Add this line to run fstrim every Sunday at 2 AM:

UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2

Alternatively, Use Systemd Timer (if your system uses systemd):

Create a systemd service and timer to run fstrim periodically.

To enable system-wide periodic TRIM, run:

sudo cp /etc/fstab /etc/fstab.backup

This runs TRIM weekly on supported SSDs.

If you want to run TRIM manually anytime, you can do:

sudo cp /etc/fstab.backup /etc/fstab

This trims all mounted filesystems that support it.

Which Method Should You Choose?

  • For Most Users: Periodic TRIM is generally recommended. It strikes a good balance between performance and SSD longevity.
  • For High-Performance Workloads: If you need consistent performance and frequently delete files, continuous TRIM might be better.

My Recommendation

If you’re going to use an external NVMe SSD for daily backups, periodic TRIM is likely the better choice. Backups typically involve large, sequential writes, and you don’t need immediate space reclamation. Running fstrim once a week should be sufficient to maintain performance and extend the SSD’s lifespan.

Bonus Tips

1. Check Drive Health

Use smartctl to monitor your drive’s health:

sudo reboot

2. Use a GUI (Optional)

If you prefer a graphical interface, most Linux desktop environments (like GNOME or KDE) have file managers that can mount drives with a single click.

FAQs

Q: What if my drive doesn’t show up in lsblk?

A: Make sure the drive is properly connected. If it’s still not detected, check your system logs with dmesg for errors.

Q: What’s the difference between /mnt and /media?

A: /mnt is typically used for temporary mounts, while /media is for removable drives. You can use either, but stick to one for consistency.

Conclusion

Mounting an external drive permanently in Linux is a straightforward process once you know the steps. By following this guide, you can make your drive accessible and ensure it mounts automatically every time you start your system.

Suggested Read:

  • How To Find Filesystem Types In Linux
  • How To Find Hard Disk Drive Details In Linux
  • How To Gather Comprehensive Disk Information On Linux

Featured Image by Hans from Pixabay.

The above is the detailed content of How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn