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:
This will list all the drives connected to your system along with their UUIDs. For example, the output might look like this:
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:
This will show you a tree-like view of all the drives connected to your system.
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.
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:
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:
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 runblkid
command, you can see the filesystem type of the drive.defaults
: This field is used to specify the mount options. Thedefaults
option is a good starting point. You can customize this based on your needs. You can read more about the available mount options in theman fstab
↗️ page.0
: This field is used bydump
command to determine which filesystems need to be dumped. You can set this to0
for most drives which means you don’t want to dump the filesystem.0
: This field is used byfsck
command to determine the order in which filesystem checks are done at boot time. You can set this to0
for most drives which means you don’t want to check the filesystem. Other values are1
and2
. 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:
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.
This will show you a list of all the drives mounted on your system along with their mount points.
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! 🚀