Ubuntu Server 18.04 – Mounting and unmounting volumes

How to install Ubuntu Server 19.10

Now that you’ve added a new storage volume to your server and have formatted it, you can mount the new device so that you can start using it. To do this, we use the mount command. This command allows you to attach a storage device (or even a network share) to a local directory on your server. Before mounting, the directory must be empty. The mount command, which we’ll get to practice with an example very shortly, basically just requires you to designate a place (directory) for it to be mounted. But where should you mount the volume?

Normally, there are two directories created by default in your Ubuntu Server installation that exist for the purposes of mounting volumes: /mnt and /media. While there is no hard rule as far as where media needs to be mounted, these two directories exist as part of the FHS that was mentioned earlier. The purpose of the /mnt and /media directories are defined within this specification. The FHS defines /mnt as a mount point for a temporarily mounted filesystem, and /media as a mount point for removable media.

In plain English, this means that the intended purpose of /mnt is for storage volumes you generally keep mounted most of the time, such as additional hard drives, virtual hard disks, and network attached storage. The FHS document uses the term temporary when describing /mnt, but I’m not sure why that’s the case since this is generally where things that your users and applications will use are mounted. Perhaps temporary just refers to the fact that the system doesn’t require this mount in order to boot, but who knows. In regard to /media, the FHS is basically indicating that removable media (flash drives, CD-ROM media, external hard drives, and so on) are intended to be mounted here.

However, it’s important to point out that where the FHS indicates you should mount your extra volumes is only a suggestion. No one is going to force you to follow it, and the fate of the world isn’t dependent on your choice. With the mount command, you can literally mount your extra storage anywhere that isn’t already mounted or full of files. You could even create the directory /kittens and mount your disks there and you won’t suffer any consequences other than a few chuckles from your colleagues.

Often, organizations will come up with their own scheme for where to mount extra disks. Although I personally follow the FHS designation, I’ve seen companies use a custom common mount directory such as /store. Whatever scheme you use is up to you; the only suggestion I can make is to be as consistent as you can from one server to another, if only for the sake of sanity.

The mount command generally needs to be run as root. While there is a way around that (you can allow normal users to mount volumes, but we won’t get into that just yet), it’s usually the case that only root can or should be mounting volumes. As I mentioned, you’ll need a place to mount these volumes, so when you’ve come up with such a place, you can mount a volume with a command similar to the following:

sudo mount /dev/sdb1 /mnt/vol1

In that example, I’m mounting device /dev/sdb1 to directory /mnt/vol1. I needed to have created the /mnt/vol1 directory before this command could work, so I would’ve already created that directory with the following command:

sudo mkdir /mnt/vol1

Of course, you’ll need to adjust the command to reference the device you want to mount and where you want to mount it. As a reminder, if you don’t remember which devices exist on your server, you can list them with:

sudo fdisk -l

Normally, the mount command wants you to issue the -t option with a given type. In my case, the mount command would’ve been the following had I used the -t option, considering my disk is formatted with ext4:

sudo mount /dev/sdb1 -t ext4 /mnt/vol1
A useful trick when mounting devices is to execute the following command before and after mounting:
df -h

While that command is generally used to check how much free space you have on various mounts, it does show you a list of mounted devices, so you can simply compare the results after mounting the device to confirm that it is present.

In that example, I used the -t option along with the type of filesystem I formatted the device with. In the first example, I didn’t. This is because, in most cases, the mount command is able to determine which type of filesystem the device uses and adjust itself accordingly. Thus, most of the time, you won’t need the -t option. In the past, you almost always needed it, but it’s easier nowadays. The reason I bring this up is because if you ever see an error when trying to mount a filesystem that indicates an invalid filesystem type, you may have to specify this. Feel free to check the man pages for the mount command for more information regarding the different types of options you can use.

When you are finished using a volume, you can unmount it with the umount command (the missing n in the word unmount is intentional):

sudo umount /mnt/vol1

The umount command, which also needs to be run as root or with sudo, allows you to disconnect a storage device from your filesystem. In order for this command to be successful, the volume should not be in use. If it is, you may receive a device or resource busy error message. If you execute df -h after unmounting, you should see that the filesystem is missing from the output, and thus isn’t mounted anymore.

The downside to manually mounting devices is that they will not automatically remount themselves the next time your server boots. In order to make the mount automatically come up when the system is started, you’ll need to edit the /etc/fstab file, which I’ll walk you through in the next section.

Comments are closed.