LPIC 101 – Mounting and Unmounting Filesystems on Linux

To access a filesystem on Linux it needs to be mounted. The process of adding a filesystem to the virtual directory on your Linux system is called mounting, the filesystem is attached to a specific point in your system’s directory tree, called a mount point. You can either manually mount the partition within the virtual directory structure from the command line or allow Linux to automatically mount the partition at boot time.

In this guide, we are going to learn how to mount and unmount filesystems on Linux.

Mounting and Unmounting Filesystems

Mounting Filesystems

mount command is used to manually mount a filesystem.

Syntax:

mount -t TYPE DEVICE MOUNTPOINT

Where:

TYPE: The type of the filesystem being mounted (e.g. ext4, btrfs, exfat, etc.).

DEVICE: The name of the partition containing the filesystem (e.g. /dev/sda3)

MOUNTPOINT: Where the filesystem will be mounted. The mounted-on directory need not be empty, although it must exist. Any files in it, however, will be inaccessible by name while the filesystem is mounted.

Mounting ext4 filesystem device called /dev/sda3 in ~/Mydir directory:

sudo mount -t ext4 /dev/sda3 ~/Mydir

Now list the content of mounted filesystem on ~/Mydir directory:

$ ls -lh ~/Mydir/
total 180K
drwxrwxr-x 2 frank frank 4.0K Apr 26 10:22  Ansible
drwxrwxr-x 2 frank frank 4.0K Feb 25 14:21  Apache
drwxrwxr-x 3 frank frank 4.0K Apr 26 09:49  AWS
drwxrwxr-x 2 frank frank 4.0K Apr 26 11:02 'Azure cloud'
drwxrwxr-x 2 frank frank 4.0K Apr 26 09:46 'Cassandra db'
drwxrwxr-x 2 frank frank 4.0K Feb 25 14:02  DOCKER
drwxrwxr-x 2 frank frank 4.0K Apr 26 10:24  Dynamodb
drwxrwxr-x 4 frank frank 4.0K Feb 25 14:37  Editors

Listing Mounted Filesystems

By just typing mount, you will get a list of all the filesystems currently mounted on your system. This list can be quite large, because in addition to the disks attached to your system, it also contains a number of run-time filesystems in memory that serve various purposes. Using -t parameter, the output can be filtered to list only filesystems of the corresponding type.

Example:

$ mount -t ext4
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)
/dev/sda3 on /home/frank/Mydir type ext4 (rw,relatime)

Also, you can specify multiple filesystems at once by separating them with a comma:

$ mount -t ext4,fuseblk
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)
/dev/sda3 on /home/frank/Mydir type ext4 (rw,relatime)
/dev/sda1 on /media/frank/01D5FAEEFC8E1CB0 type fuseblk (rw,nosuid,nodev,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096,uhelper=udisks2)

The output above have the following format:

SOURCE on TARGET type TYPE OPTIONS

Where;

  • SOURCE: is the partition which contains the filesystem.
  • TARGET: is the directory where it is mounted.
  • TYPE: is the filesystem type.
  • OPTIONS: are the options passed to the mount command at mount time.

Additional command line parameters used with mount command:

  • -a: This will mount all filesystems listed in the file /etc/fstab.
  • -o or --options: This will pass a list of comma-separated mount options to the mount command, which can change how the filesystem will be mounted.
  • -r or -ro: This will mount the filesystem as read-only.
  • -w or -rw: This will the mount filesystem as writable.

Unmounting Filesystems

umount command followed by the device name or the mount point is used to unmount a filesystem. i.e.

Unmounting using the device name:

sudo umount /dev/sda1

Unmounting using the mount point:

sudo umount ~/Mydir

umount command line parameters:

  • -a: This will unmount all filesystems listed in /etc/fstab.
  • -f: This will force the unmounting of a filesystem. This may be useful if you mounted a remote filesystem that has become unreachable.
  • -r: If the filesystem cannot be unmounted, this will try to make it read-only.

Sometimes when unmounting a filesystem, you encounter an error message stating that the target is busy. This will happen if any files on the filesystem are open. However, it may not be immediately obvious where an open file is located, or what is accessing the filesystem. Using lsof command, followed by the name of the device containing the filesystem, will list processes accessing it and which files are open.

Example:

$ sudo umount ~/Mydir
umount: /home/frank/Mydir: target is busy.

Using lsof command to find the open files:

$ lsof ~/Mydir
COMMAND     PID  USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
zsh      208404 frank  cwd    DIR    8,3     4096 4718610 /home/frank/Mydir/Ansible

COMMAND is the name of the executable that opened the file, and PID is the process number. NAME is the name of the file that is open. In the output above, the file Ansible is opened by the program zsh (current shell). If we close the program, then we will be able to unmount the filesystem.

Configuring User Mountable Removable Filesystems

Filesystems can be mounted anywhere in the system, but there are some good practices that should be followed to make system administration easier.

In old systems, /mnt was the directory under which all external devices would be mounted and a number of pre-configured “anchor points” for common devices, like CD-ROM drives (/mnt/cdrom) and floppy disks (/mnt/floppy) existed under it.

This has been superseded by /media, which is now the default mount point for any user-removable media (e.g. external disks, USB flash drives, memory card readers, etc.) connected to the system.

On most modern Linux distributions and desktop environments, removable devices are automatically mounted under /media/USER/LABEL when connected to the system,

Where;

  • USER: is the username.
  • LABEL: is the device label.

For example, a USB flash drive with the label FlashDrive connected by the user frank would be mounted under /media/frank/FlashDrive/. The way this is handled is different depending on the desktop environment.

Whenever you need to manually mount a filesystem, it is good practice to mount it under /mnt.

Mounting Filesystems on Bootup

For permanent storage devices, Linux maintains the /etc/fstab file to indicate which drive devices should be mounted to the virtual directory at boot time. The /etc/fstab file is a table that indicates the drive device file (either the raw file or one of its permanent udev filenames), the mount point location, the filesystem type, and any additional options required to mount the drive.

The /etc/fstab file;

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=51d32bf0-6c70-4edb-b8ee-92c34a190eb6 /               ext4    errors=remount-ro 0       1
/swapfile                                 none            swap    sw              0       0

This is a text file, where each line describes a filesystem to be mounted, with six fields per line in the following order;

FILESYSTEM MOUNTPOINT TYPE OPTIONS DUMP PASS

Where:

  • FILESYSTEM: The device containing the filesystem to be mounted. Instead of the device, you can specify the UUID or label of the partition, something which we will discuss later on.
  • MOUNTPOINT: Where the filesystem will be mounted.
  • TYPE: The filesystem type.
  • OPTIONS: Mount options that will be passed to mount.
  • DUMP: Indicates whether any ext2, ext3 or ext4 filesystems should be considered for backup by the dump command. Usually it is zero, meaning they should be ignored.
  • PASS: When non-zero, defines the order in which the filesystems will be checked on bootup. Usually it is zero.

The first partition on the first disk of a machine could be described as:

$ mount -t ext4
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)

The mount options on OPTIONS are a comma-separated list of parameters, which can be generic or filesystem specific. Among the generic ones we have:

  • atime and noatime: By default, every time a file is read the access time information is updated. Disabling this (with noatime) can speed up disk I/O. Do not confuse this with the modification time, which is updated every time a file is written to.
  • auto and noauto: Whether the filesystem can (or can not) be mounted automatically with mount -a.
  • defaults: This will pass the options: rwsuiddevexecautonouser and async to mount.
  • dev and nodev: Whether character or block devices in the mounted filesystem should be interpreted.
  • exec and noexec: Allow or deny permission to execute binaries on the filesystem.
  • user and nouser: Allows (or not) an ordinary user to mount the filesystem.
  • group: Allows a user to mount the filesystem if the user belongs to the same group which owns the device containing it.
  • owner: Allows a user to mount a filesystem if the user owns the device containing it.
  • suid and nosuid: Allow, or not, SETUID and SETGID bits to take effect.
  • ro and rw: Mount a filesystem as read-only or writable.
  • remount: This will attempt to remount an already mounted filesystem. This is not used on /etc/fstab, but as a parameter to mount -o. For example, to remount the already mounted partition /dev/sdb1 as read-only, you could use the command mount -o remount,ro /dev/sdb1. When remounting, you do not need to specify the filesystem type, only the device name or the mount point.
  • sync and async: Whether to do all I/O operations to the filesystem synchronously or asynchronously. async is usually the default. The manual page for mount warns that using sync on media with a limited number of write cycles (like flash drives or memory cards) may shorten the life span of the device.

Using UUIDs and Labels

Specifying the name of the device containing the filesystem to mount may introduce some problems. Sometimes the same device name may be assigned to another device depending on when, or where, it was connected to your system. For example, a USB flash drive on /dev/sdb1 may be assigned to /dev/sdc1 if plugged on another port, or after another flash drive. One way to avoid this is to specify the label or UUID (Universally Unique Identifier) of the volume. Both are specified when the filesystem is created and will not change, unless the filesystem is destroyed or manually assigned a new label or UUID.

lsblk command with -f parameter followed by the device name is used to query information about a filesystem and find out the label and UUID associated to it. i.e.

$ lsblk -f /dev/sda2
NAME FSTYPE LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda2 ext4         51d32bf0-6c70-4edb-b8ee-92c34a190eb6   53.3G    64% /

Here is the meaning of each column:

  • NAME: Name of the device containing the filesystem.
  • FSTYPE: Filesystem type.
  • LABEL: Filesystem label.
  • UUID: Universally Unique Identifier (UUID) assigned to the filesystem.
  • FSAVAIL: How much space is available in the filesystem.
  • FSUSE%: Usage percentage of the filesystem.
  • MOUNTPOINT: Where the filesystem is mounted.

In /etc/fstab a device can be specified by its UUID with the UUID= option, followed by the UUID, or with LABEL=, followed by the label. So, instead of:

/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)

Use:

UUID=51d32bf0-6c70-4edb-b8ee-92c34a190eb6  /  ext4 (rw,relatime,errors=remount-ro)

Or the disk labeled Mydisk:

LABEL=Mydisk  ~/ ext4  defaults

Also blkid command is used to display information about block devices, such as storage drives.

$ blkid /dev/sda2
/dev/sda2: UUID="51d32bf0-6c70-4edb-b8ee-92c34a190eb6" TYPE="ext4" PARTUUID="6a1007f0-02"

The same syntax can be used with the mount command. Instead of the device name, pass the UUID or label. For example, to mount an external NTFS disk with the UUID 01D5FAEEFC8E1CB0 on /mnt/Mydir, the command would be:

sudo mount -t ntfs UUID=01D5FAEEFC8E1CB0 /mnt/Mydir

Mounting Disks with Systemd

Systemd is the init process, the first process to run on many Linux distributions. It is responsible for spawning other processes, starting services and bootstraping the system. Among many other tasks, systemd can also be used to manage the mounting (and automounting) of filesystems.

To use this feature of systemd, you need to create a configuration file called a mount unit. Each volume to be mounted gets its own mount unit and they need to be placed in /etc/systemd/system/.

Mount units are simple text files with the .mount extension. The basic format is shown below:

[Unit]
Description=

[Mount]
What=
Where=
Type=
Options=

[Install]
WantedBy=

Where;

  • Description=: Short description of the mount unit, something like Mounts the backup disk.
  • What=: What should be mounted. The volume must be specified as /dev/disk/by-uuid/VOL_UUID where VOL_UUID is the UUID of the volume.
  • Where=: Should be the full path to where the volume should be mounted.
  • Type=: The filesystem type.
  • Options=: Mount options that you may wish to pass, these are the same used with the mount command or in /etc/fstab.
  • WantedBy=: Used for dependency management. In this case, we will use multi-user.target, which means that whenever the system boots into a multi-user environment (a normal boot) the unit will be mounted.

So the file must look like this;

[Unit]
Description=Real Disk         

[Mount]
What=/dev/disk/by-uuid/e3ef9af4-7136-4650-8365-c6c02d0d14f4
Where=/media/frank 
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target

In order to work correctly, the mount unit must have the name as the mount point. the mount point is /media/frank, so the file should be named media-frank.mount.

Now restart the systemd daemon with the systemctl command;

sudo systemctl daemon-reload
sudo systemctl start media-frank.mount

You can check the status of the mounting with the command systemctl status media-frank.mount;

$ systemctl status media-frank.mount
● media-frank.mount - Real Disk
     Loaded: loaded (/etc/systemd/system/media-frank.mount; disabled; vendor preset: enabled)
     Active: active (mounted) since Sun 2021-05-16 11:25:38 EAT; 3min 37s ago
      Where: /media/frank
       What: /dev/sda3
      Tasks: 0 (limit: 9361)
     Memory: 100.0K
     CGroup: /system.slice/media-frank.mount

May 16 11:25:38 frank systemd[1]: Mounting Real Disk...
May 16 11:25:38 frank systemd[1]: Mounted Real Disk.

 If you want to enable it on every boot, run the following command;

$ sudo systemctl enable media-frank.mount
Created symlink /etc/systemd/system/multi-user.target.wants/media-frank.mount → /etc/systemd/system/media-frank.mount.

Automounting a Mount Unit

Mount units can be automounted whenever the mount point is accessed. To do this, you need an .automount file, alongside the .mount file describing the unit.

The basic format is:

[Unit]
Description=

[Automount]
Where=

[Install]
WantedBy=multi-user.target

Like before;

  • Description= is a short description of the file.
  • Where= is the mountpoint.

For example, an .automount file for our previous example would be:

[Unit]
Description=Real Disk         


[Automount]
Where=/media/frank

[Install]
WantedBy=multi-user.target

Save the file with the same name as the mount point (in this case, media-frank.automount), reload systemd and start the unit:

sudo systemctl daemon-reload
sudo systemctl start media-frank.automount

Now whenever the /media/frank directory is accessed, the disk will be mounted. Like before, to enable the automount on every boot you would use:

sudo systemctl enable media-frank.automount

Conclusion

This marks the end of our guide on how to mount and unmount filesystems on Linux. Stay tuned for more LPIC – 101 Guide, hope this guide has been helpful.

Similar guides:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Welcome to our today’s tutorial on how to search text files using regular expressions like grep, egrep, fgrep, sed, regex. […]

In this guide, we show you how to manage files and permissions on Linux. The core security feature of Linux […]

Normally when backing data in a computer locally or to the cloud, there has been a struggle to get the […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.