Register now for better personalized quote!

How to permanently mount a drive in Linux (and why you should)

May, 19, 2023 Hi-network.com
Morsa Images/Getty Images

I currently have four hard drives inside of my System76 Thelio desktop. The primary drive serves as my operating system and the others are strictly for housing different types of files. I have one drive for virtual machines, one for music, and one for miscellaneous files. By laying out my system this way, even if the operating system fails, my data is still accessible.

I have those secondary drives set up such that they are automatically available at all times. In the Linux-verse, this is called "automounting" and it's an important task you'll want to understand.

Automounting is a thing because when you have secondary drives attached to a machine, they aren't automatically available to you upon boot of the machine. Yes, you can open your desktop file manager, navigate to the drive, and mount it by clicking the entry. However, this can be problematic if you forget to do that and you either have a backup configured to automatically save files to that drive or you simply go to save a file to that drive from an application. If the drive isn't mounted, the app (or backup) won't be able to access the drive.

Also: The best Linux laptops right now

And that's why we always want to configure these drives for automounting.

Let me show you how it's done.

How to automount a drive in Linux

What you'll need: To make this work, you'll need a running instance of Linux, a secondary drive plugged into your machine, and a user with sudo privileges. I'll demonstrate with Pop!_OS Linux but the process should be the same, regardless of which distribution you use. I will also assume the drive has been formatted. I always format my secondary Linux drives with the ext4 format.

1. Locate the name of your drive

The first thing to be done is to locate the name of the drive to be mounted. Here's a simple way to find the name.

Also: Can't remember the Linux command you ran earlier? Let history repeat itself

Without the drive plugged in, issue the following command:

lsblk

In the output of that command, you should see entries like this:

sda             8:0    0 931.5G  0 disk  a??a??sda1          8:1    0 931.5G  0 part

Plug the drive in and run the command again and you'll see a new entry like:

sdb             8:16   0 931.5G  0 disk  a??a??sdb1          8:17   0 931.5G  0 part

If you can't easily unplug the secondary disk, just run thelsblkcommand. If you see two drives, sda and sdb, chances are very good your secondary drive is sdb. For the purpose of showing this process, we'll assume the name of your drive is /dev/sdb.

2. Create a mount point

The mount point will be the directory on your primary drive that will serve as a location you will access the secondary drive from. 

Also: The most important reason you should be using Linux at home

This doesn't copy or move the files from one to the other but, rather, creates a place for the operating system to "mount" the secondary drive. Let's create a mount point called /data with the command:

sudo mkdir /data

Next, change the ownership of the new directory to your user with the command:

sudo chown -R$USER:$USER /data

The -R option makes sure all child folders have the same ownership.

3. Create an entry in /etc/fstab

The /etc/fstab is the file responsible for mapping the secondary drive to the mount point. 

Also: 8 things you can do with Linux that you can't do with MacOS or Windows 

Assuming the name of your secondary drive is /dev/sdb, we'll tack on a 1 to the end (because /dev/sdb1 is the first usable partition). Open the fstab file for editing with the command:

sudo nano /etc/fstab

At the bottom of that file, add an entry like this:

/dev/sdb1 /data    ext4    defaults        0       0

Here's an explanation:

  • /dev/sdb1 -- the secondary drive
  • /data -- the mount point
  • ext4 -- the secondary drive file system type.
  • defaults -- uses the default options
  • 0 0 -- these fields are for dump and fsck. Just leave them both as zeros

Save and close the file with Ctrl-X.

Testing the mount

All you need to do to test the mount is issue the command:

mount -a

If you receive no feedback, everything is good. You can now reboot your machine and the secondary drive will be automatically mounted so you can access the files from /data.

Also: How to choose the right Linux desktop distribution for you

Congratulations, you've just successfully set up a secondary drive automount on Linux. Next time around, we'll do the same thing with a GUI.

See also

How to install Ubuntu Linux (It's easy!)How to run a Windows app on Linux with WineHow to get started with Git on LinuxHow to kill a process in Linux
  • How to install Ubuntu Linux (It's easy!)
  • How to run a Windows app on Linux with Wine
  • How to get started with Git on Linux
  • How to kill a process in Linux

tag-icon Hot Tags : Services & Software Open Source

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.