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:
- Device: The partition or drive to mount (e.g.,
/dev/sdb1
,UUID=1234-5678
, orLABEL=MyDrive
). - Mount Point: The directory where the drive will be accessible (e.g.,
/mnt/mydrive
). - File System Type: The type of file system (e.g.,
ext4
,ntfs
,exfat
). - Mount Options: A comma-separated list of options (e.g.,
defaults
,noatime
,nofail
). - Dump: Used by the
dump
utility for backups (0
means no backup). - 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:
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:
sudo cp /etc/fstab.backup /etc/fstab
Reboot your system:
sudo reboot
Always Test Before Rebooting
After editing /etc/fstab
, always test the configuration with:
sudo mount -a
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:
- Backup
/etc/fstab
. - Edit
/etc/fstab
. - Test with
sudo mount -a
. - 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:
lsblk
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.
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]
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:
sudo mkdir /media/ostechnix/SK_WD_SSD
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:
sudo mount /dev/sda1 /media/ostechnix/SK_WD_SSD
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:
LABEL=SK_WD_SSD /media/ostechnix/SK_WD_SSD exfat errors=remount-ro,defaults,users,noatime,nodiratime,umask=0 0 2
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 blkid
Sample Output:
/dev/nvme0n1p3: UUID="1ff239fc-674c-4749-9c73-fe5d0668071a" TYPE="swap" PARTUUID="95db6d7b-903b-4f8c-8583-bd00070aa45a"
/dev/nvme0n1p1: UUID="CF87-3143" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="9dc59fb7-f900-484d-b4c5-c9090b901727"
/dev/nvme0n1p2: UUID="99406049-9ff5-47d1-a1ce-d5e27cd859c0" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="f2d515b2-a3c4-4c8c-a609-91d5b755b001"
/dev/sda2: SEC_TYPE="msdos" LABEL_FATBOOT="VTOYEFI" LABEL="VTOYEFI" UUID="5A89-BA75" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="6251cb36-02"
/dev/sda1: LABEL="SK_WD_SSD" UUID="2A81-C276" BLOCK_SIZE="512" TYPE="exfat" PTTYPE="dos" PARTUUID="6251cb36-01"
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:
UUID=2A81-C276 /media/ostechnix/SK_WD_SSD exfat errors=remount-ro,defaults,users,noatime,nodiratime,umask=022,nofail 0 2
This /etc/fstab
entry:
- Mounts the drive with UUID
2A81-C276
to/media/ostechnix/SK_WD_SSD
. - Uses the
exfat
file system. - Applies mount options for safety (
errors=remount-ro
), performance (noatime
,nodiratime
), and user access (users
,umask=022
). - Prevents boot errors if the drive is disconnected (
nofail
). - 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 byusers
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:
sudo nano /etc/fstab
Add the line at the end of the file.
Here are my system's fstab entries for reference:
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# systemd generates mount units based on this file, see systemd.mount(5).
# Please run 'systemctl daemon-reload' after making changes here.
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/nvme0n1p2 during installation
UUID=99406049-9ff5-47d1-a1ce-d5e27cd859c0 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/nvme0n1p1 during installation
UUID=CF87-3143 /boot/efi vfat umask=0077 0 1
# swap was on /dev/nvme0n1p3 during installation
UUID=1ff239fc-674c-4749-9c73-fe5d0668071a none swap sw 0 0
# Fstab Entry to Permanently Mount External Drive
UUID=2A81-C276 /media/ostechnix/SK_WD_SSD exfat errors=remount-ro,defaults,users,noatime,nodiratime,umask=022,nofail 0 2
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 mount -a
If there are no errors, your drive is ready to use!
You can verify this using the df
command:
df -h
This command will will show you the list of mounted drives along with their mount points on your system:
Filesystem Size Used Avail Use% Mounted on udev 16G 0 16G 0% /dev tmpfs 3.1G 2.3M 3.1G 1% /run /dev/nvme0n1p2 456G 264G 170G 61% / tmpfs 16G 104M 16G 1% /dev/shm tmpfs 5.0M 8.0K 5.0M 1% /run/lock efivarfs 192K 109K 79K 59% /sys/firmware/efi/efivars /dev/nvme0n1p1 511M 9.3M 502M 2% /boot/efi /dev/sda1 932G 799G 133G 86% /media/ostechnix/SK_WD_SSD /dev/fuse 128M 16K 128M 1% /etc/pve tmpfs 3.1G 2.6M 3.1G 1% /run/user/1000
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:
sudo fstrim -v /media/ostechnix/SK_WD_SSD
You can also set up a cron job to run this automatically. For example, to run TRIM weekly, add this to your crontab:
0 2 * * 0 sudo fstrim -v /media/ostechnix/SK_WD_SSD
Step 9: Unmounting the Drive (Optional)
When you’re done using the drive, unmount it safely:
sudo umount /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=your-uuid-here /media/ostechnix/SK_WD_SSD exfat errors=remount-ro,defaults,users,noatime,nodiratime,umask=022,nofail,discard 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 apt install util-linux
Run fstrim
Manually:
sudo fstrim -v /media/ostechnix/SK_WD_SSD
Set Up a Cron Job (e.g., weekly):
Open the crontab editor:
crontab -e
Add this line to run fstrim
every Sunday at 2 AM:
0 2 * * 0 sudo fstrim -v /media/ostechnix/SK_WD_SSD
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 systemctl enable --now fstrim.timer
This runs TRIM weekly on supported SSDs.
If you want to run TRIM manually anytime, you can do:
sudo fstrim -av
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 apt install smartmontools sudo smartctl -a /dev/sdb1 ## This trims all mounted filesystems that support it.
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
lsblk
?A: Make sure the drive is properly connected. If it’s still not detected, check your system logs with dmesg
for errors.
/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:
Featured Image by Hans from Pixabay.