/The right way to correctly automount a drive in Ubuntu Linux

The right way to correctly automount a drive in Ubuntu Linux


Need to create a proper fstab entry to automatically mount a drive in Linux? Find out how here.

How to properly automount a drive in Ubuntu Linux

Need to create a proper fstab entry to automatically mount a drive in Linux? Find out how here.

If you have servers or desktops, running Ubuntu Linux (or a derivative), you might find yourself needing to add extra drives for storage, backups, or any number of reasons. Sure you could plug that drive in, open your file manager, and click to mount it. But what if you’re dealing with a headless server or just wanted to make sure that disk was always mounted at boot (without your interaction)? 

Fortunately that’s a fairly simple task to undertake. But to do it properly requires a few extra steps you might not know about. I’m going to show you the proper way to set up the automounting of a drive in Linux—specifically, Pop!_OS 19.04 (a derivative of Ubuntu Desktop).  

SEE: Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)

What you need

To make this work, you’ll need the following:

  • An instance of Ubuntu Server (or a derivative).
  • An attached hard-drive to mount.
  • A user account with sudo privileges.

With those things at the ready, let’s get to work.

Locate the partition to mount

The first thing to be done is to locate the partition you want to mount. In this case, we’ll be working with an entire drive. To do this, open a terminal window and issue the command:

sudo fdisk -l

You should see a complete listing of all the attached drives to the system (Figure A).

mounta.jpg

Figure A: Some of the drives attached to a Linux system.

Let’s say we figured out the disk we want to mount is on /dev/sdj. With that bit of information in hand, we’re ready to continue on.

Locate the UUID

Next we need to find the UUID (Universal Unique Identifier) of the drive. To do that, issue the command:

sudo blkid

This will display every UUID associated with every storage device attached to your machine (Figure B).

mountb.jpgmountb.jpg

Figure B: The UUID of the drive on /dev/sdj.

We now have our drive name and UUID. With this information we can create an automount entry in fstab.

Create a mount point

Before we add the entry to fstab, we must first create a mount point for the drive. The mount point is the directory where users will access the data on the drive (as they can’t access /dev/sdj itself). So let’s create a directory called data with the command:

sudo mkdir /data

You’ll want to also change the group ownership of that directory, so that users can access it. For this, you might create a group called data and then add users to the new group. This could be done with the commands:

sudo groupadd data
sudo usermod -aG data USERNAME (Where USERNAME is the name of the user to be added)

After you’ve done that, you could then change the ownership of the mountpoint with the command:

sudo chown -R :data /data

The automount entry

In order to create the automount entry, issue the command:

sudo nano /etc/fstab

At the bottom of that file, we’ll add an entry that contains the information we’ve discovered. The entry will look like this:

UUID=14D82C19D82BF81E /data    auto nosuid,nodev,nofail,x-gvfs-show 0 0

Breaking that line down, we have:

  • UUID=14D82C19D82BF81E – is the UUID of the drive. You don’t have to use the UUID here. You could just use /dev/sdj, but it’s always safer to use the UUID as that will never change (whereas the device name could).
  • /data – is the mount point for the device.
  • auto – automatically mounts the partition at boot 
  • nosuid – specifies that the filesystem cannot contain set userid files. This prevents root escalation and other security issues.
  • nodev – specifies that the filesystem cannot contain special devices (to prevent access to random device hardware).
  • nofail – removes the errorcheck.
  • x-gvfs-show – show the mount option in the file manager. If this is on a GUI-less server, this option won’t be necessary.
  • 0 – determines which filesystems need to be dumped (0 is the default).
  • 0 – determine the order in which filesystem checks are done at boot time (0 is the default).

Save and close the file.

Testing the entry

Before you reboot the machine, you need to test your new fstab entry. To do this, issue the command:

sudo mount -a

If you see no errors, the fstab entry is correct and you’re safe to reboot.

Congratulations, you’ve just created a proper fstab entry for your connected drive. Your drive will automatically mount every time the machine boots.

Also see

linuxhero.jpglinuxhero.jpg

Image: Jack Wallen

Original Source