Mount a drive permanently with fstab in Linux

📆 · ⏳ 5 min read · ·

Introduction

So you’ve got a new drive and you want to mount it on your Linux system? You can do this manually every time you boot up your system using mount command, but that’s not very convenient.

Instead, today we will see how to mount a drive permanently in Linux using the fstab file which will mount the drive automatically on boot.

Step 1: Find the UUID of the drive

First, you need to find the UUID of the drive you want to mount. You can do this by running the following command:

Terminal window
sudo blkid

This will list all the drives connected to your system along with their UUIDs. For example, the output might look like this:

Terminal window
/dev/sda1: UUID="b8e4a1f6-6e8f-4f6e-8b6e-6e8f4f6e8b6e" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sdb1: UUID="c8e4a1f6-6e8f-4f6e-8b6e-6e8f4f6e8b6e" BLOCK_SIZE="4096" TYPE="ext4"

In this example, /dev/sda1 is the drive we want to mount, and its UUID is b8e4a1f6-6e8f-4f6e-8b6e-6e8f4f6e8b6e.

How to check what’s the device name of the drive you want to mount? You can use the lsblk command to list all the drives connected to your system along with their device names. For example:

Terminal window
lsblk

This will show you a tree-like view of all the drives connected to your system.

Terminal window
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 94.5M 1 loop /snap/lxd/30135
loop1 7:1 0 69.2M 1 loop /snap/core22/1590
loop2 7:2 0 69.2M 1 loop /snap/core22/1614
loop3 7:3 0 94.5M 1 loop /snap/lxd/29946
loop4 7:4 0 33.7M 1 loop /snap/snapd/21761
sda 8:0 0 931.5G 0 disk
└─sda1 8:1 0 931.5G 0 part
mtdblock0 31:0 0 16M 0 disk
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 4M 0 part
└─nvme0n1p2 259:2 0 931.5G 0 part /

In this example, /dev/sda and /dev/nvme0n1 are the drives connected to the system, and from our example we want to mount /dev/sda1.

Step 2: Create a mount point

Next, you need to create a directory where you want to mount the drive. You can create a new directory using the mkdir command. Usually people mount drives under /mnt or /media directories. You can choose any directory you like. I’ll go ahead with the /mnt directory.

Terminal window
sudo mkdir /mnt/mydrive

Step 3: Edit the fstab file

Now, you need to edit the fstab file to add an entry for the drive you want to mount. You can do this by running the following command:

Terminal window
sudo vi /etc/fstab

I am choosing vi as the text editor here. You can choose any text editor you like.

The fstab file contains information about all the drives that are mounted automatically on boot. You need to add an entry for the drive you want to mount. The entry should look like this:

Terminal window
UUID=b8e4a1f6-6e8f-4f6e-8b6e-6e8f4f6e8b6e /mnt/mydrive ext4 defaults 0 0

Now you can save the file and exit the text editor.

Side Quest: What does each field in the fstab entry mean?

  • UUID=b8e4a1f6-6e8f-4f6e-8b6e-6e8f4f6e8b6e: The UUID of the drive you want to mount. So replace this with the UUID of your drive that you found in step 1.
  • /mnt/mydrive: The directory where you want to mount the drive. Pretty straightforward.
  • ext4: The filesystem type of the drive. Replace this with the filesystem type of your drive. So when you run blkid command, you can see the filesystem type of the drive.
  • defaults: This field is used to specify the mount options. The defaults option is a good starting point. You can customize this based on your needs. You can read more about the available mount options in the man fstab ↗️ page.
  • 0: This field is used by dump command to determine which filesystems need to be dumped. You can set this to 0 for most drives which means you don’t want to dump the filesystem.
  • 0: This field is used by fsck command to determine the order in which filesystem checks are done at boot time. You can set this to 0 for most drives which means you don’t want to check the filesystem. Other values are 1 and 2. The root filesystem should be specified with a value of 1. Other filesystems should have a value of 2.

Step 4: Mount the drive

Finally, you can mount the drive by running the following command:

Terminal window
sudo mount -a

If you do not see any errors, then the drive has been mounted successfully. You can verify this by running the df command which will show you all the drives mounted on your system.

Terminal window
df -h

This will show you a list of all the drives mounted on your system along with their mount points.

Terminal window
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.6G 4.4M 1.6G 1% /run
/dev/nvme0n1p2 917G 154G 726G 18% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 916G 274G 597G 32% /mnt/mydrive
tmpfs 1.6G 12K 1.6G 1% /run/user/1000

In this example, you can see that /dev/sda1 is mounted on /mnt/mydrive.

Conclusion

That’s it! You have successfully mounted a drive permanently in Linux using the fstab file. The drive will now be mounted automatically on boot. You can now start using the drive as you like.

If you have any questions or comments, please feel free to reach out to me on Twitter ↗️ / Reddit ↗️ or in the comments box below.

Happy mounting! 🚀

You may also like

  • Setup Jellyfin with Hardware Acceleration on Orange Pi 5 (Rockchip RK3558)

    Recently I moved my Jellyfin to an Orange Pi 5 Plus server. The Orange Pi 5 has a Rockchip RK3558 SoC with integrated ARM Mali-G610. This guide will show you how to set up Jellyfin with hardware acceleration on the Orange Pi 5.

  • HTTPS with self-signed certificates for your Homelab services

    In this article we will deep dive into understanding how we can setup HTTPS with self-signed certificates for our Homelab services.This is often required when you are running your own services and you want to access them over HTTPS.

  • Setup Shareable Drive with Samba in Linux

    In this article we will setup a shareable drive in Linux with SMB. We will learn how to setup the share directory using Samba on server and how to mount it on client.