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


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.

Also: Open source and Linux skills are still in demand in a dark economy

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.

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. If you’re using an NTFS drive (a Windows drive), you’ll need to install the ntfs-3g software with a command like sudo apt-get install ntfs-3g.

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

sda             8:0    0 931.5G  0 disk  
└─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  
└─sdb1          8:17   0 931.5G  0 part

If you can’t easily unplug the secondary disk, just run the lsblk command. 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.

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:

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.

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:

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. If this is an NTFS drive, substitute with ntfs-3g
  • 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:

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.

Source link


Please enter your comment!
Please enter your name here