Home >System Tutorial >LINUX >How To Mount A Drive Permanently In Linux Using Fstab: A Step-by-Step Guide
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
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.
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?
Structure of /etc/fstab:
Each line in the file represents a file system or partition and has six fields:
Example Entry:
Here’s an example of an /etc/fstab entry:
UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2
Here,
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:
Let us now see how to permanently mount a drive in Linux.
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:
By following these steps, you’ll avoid most common issues when mounting external drives in Linux.
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.
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.
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.
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:
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:
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.
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).
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.
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:
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:
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.
It controls whether the drive is backed up by the dump utility. Most users don’t use dump, so 0 is typically fine.
It specifies the order in which the file system is checked by fsck during boot.
This ensures file system integrity for non-root drives.
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
Press CTRL O followed by CTRL X to save the file and exit the editor.
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.
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]
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).
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.
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.
Add the discard option to your /etc/fstab entry:
UUID=1234-5678 /mnt/mydrive ext4 defaults,noatime 0 2
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).
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.
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.
Use smartctl to monitor your drive’s health:
sudo reboot
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.
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.
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!