How to check if a disk is mounted in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a disk is mounted in Linux using various command-line tools. You will begin by using the df -h command to list mounted file systems and understand the output columns like Filesystem, Size, Used, Avail, Use%, and Mounted on.

Next, you will explore the /proc/mounts file to inspect detailed information about currently mounted file systems. Finally, you will utilize the findmnt command, a more modern and versatile tool, to verify mount points and gain further insights into your system's disk mounts.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/FileandDirectoryManagementGroup -.-> linux/find("File Searching") linux/SystemInformationandMonitoringGroup -.-> linux/df("Disk Space Reporting") subgraph Lab Skills linux/ls -.-> lab-558707{{"How to check if a disk is mounted in Linux"}} linux/cat -.-> lab-558707{{"How to check if a disk is mounted in Linux"}} linux/find -.-> lab-558707{{"How to check if a disk is mounted in Linux"}} linux/df -.-> lab-558707{{"How to check if a disk is mounted in Linux"}} end

List mounted disks with df -h

In this step, you will learn how to list the mounted file systems on your Linux system using the df command. The df command is used to display the amount of disk space available on the file systems.

File systems are how your operating system organizes and manages files on storage devices like hard drives or SSDs. When a file system is "mounted," it means it's attached to a specific directory in the file system hierarchy, making its contents accessible.

Open the terminal if you haven't already. You can do this by clicking the Xfce Terminal icon on the left side of the desktop.

Now, type the following command and press Enter:

df -h

Let's break down the command:

  • df: This is the command itself, short for "disk free".
  • -h: This option stands for "human-readable". It displays the sizes in powers of 1024 (e.g., KiB, MiB, GiB) instead of bytes, making it much easier to read.

You will see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          <size>  <used>  <avail> <use%> /
tmpfs           <size>  <used>  <avail> <use%> /dev
tmpfs           <size>  <used>  <avail> <use%> /sys/fs/cgroup
/dev/sda1       <size>  <used>  <avail> <use%> /etc/hosts
shm             <size>  <used>  <avail> <use%> /dev/shm
tmpfs           <size>  <used>  <avail> <use%> /proc/asound
tmpfs           <size>  <used>  <avail> <use%> /proc/acpi
tmpfs           <size>  <used>  <avail> <use%> /proc/scsi
tmpfs           <size>  <used>  <avail> <use%> /sys/firmware

The output provides several columns:

  • Filesystem: The name of the file system.
  • Size: The total size of the file system.
  • Used: The amount of space used on the file system.
  • Avail: The amount of space available on the file system.
  • Use%: The percentage of space used.
  • Mounted on: The directory where the file system is mounted.

The / (root) entry is particularly important, as it represents the main file system where your operating system and files reside.

Understanding mounted file systems is crucial for managing disk space and troubleshooting storage issues.

Click Continue to proceed to the next step.

Inspect mount points in /proc/mounts

In this step, you will explore the /proc/mounts file, which provides detailed information about the currently mounted file systems.

The /proc file system is a virtual file system in Linux that provides information about processes and other system information. It doesn't contain real files on disk but rather provides a window into the kernel's data structures.

The /proc/mounts file specifically lists all the file systems that are currently mounted, along with their mount options. This file is a dynamic representation of the system's current state.

To view the contents of /proc/mounts, you can use the cat command. cat is a command-line utility that reads file contents and prints them to the standard output (your terminal).

Type the following command in your terminal and press Enter:

cat /proc/mounts

You will see output similar to this, which might be quite long:

overlay / overlay rw,relatime,lowerdir=/var/lib/docker/overlay2/l/...,upperdir=/var/lib/docker/overlay2/...,workdir=/var/lib/docker/overlay2/... 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev tmpfs rw,nosuid,size=65536k,mode=755 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,relatime,size=10240k,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
...
/dev/sda1 /etc/hosts ext4 rw,relatime 0 0
shm /dev/shm tmpfs rw,nosuid,nodev,noexec,relatime,size=65536k 0 0
...

Each line in /proc/mounts represents a mounted file system and contains the following fields (separated by spaces):

  1. File system device: The device or source of the file system (e.g., overlay, /dev/sda1).
  2. Mount point: The directory where the file system is mounted (e.g., /, /proc).
  3. File system type: The type of the file system (e.g., overlay, proc, ext4).
  4. Mount options: Options used when mounting the file system (e.g., rw for read-write, ro for read-only, relatime).
  5. dump frequency: Used by the dump command (usually 0).
  6. pass number: Used by the fsck command to check file system integrity (usually 0).

Comparing the output of df -h and cat /proc/mounts can give you different perspectives on the mounted file systems. df -h focuses on disk usage, while /proc/mounts provides detailed mounting information.

Click Continue to move on to the next step.

Verify mounts with findmnt command

In this step, you will use the findmnt command, which is another powerful tool for displaying information about mounted file systems. findmnt is often considered more user-friendly and provides a tree-like view of the mount points.

The findmnt command queries the /etc/fstab, /etc/mtab, or /proc/self/mountinfo files. It can display the mount points in a hierarchical way, making it easier to understand the relationships between different file systems.

Type the following command in your terminal and press Enter:

findmnt

You will see output similar to this:

TARGET        SOURCE     FSTYPE     OPTIONS
/             overlay    overlay    rw,relatime,...
|-/proc       proc       proc       rw,nosuid,nodev,noexec,relatime
| |-/proc/asound tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=...
| |-/proc/acpi  tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=...
| |-/proc/scsi  tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=...
| `-/proc/kcore tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=...
|-/sys        sysfs      sysfs      rw,nosuid,nodev,noexec,relatime
| `-/sys/fs/cgroup tmpfs      tmpfs      ro,nosuid,nodev,noexec,relatime,size=...
|   `-systemd cgroup     cgroup     rw,nosuid,nodev,noexec,relatime,xattr,...
|-/dev        tmpfs      tmpfs      rw,nosuid,size=...,mode=755
| |-/dev/pts  devpts     devpts     rw,nosuid,noexec,relatime,gid=...,mode=...
| `-/dev/shm  tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=...
|-/etc/hosts  /dev/sda1  ext4       rw,relatime
`-...

The output shows the mount points in a tree structure.

  • TARGET: The mount point directory.
  • SOURCE: The device or source of the file system.
  • FSTYPE: The file system type.
  • OPTIONS: The mount options.

You can also use findmnt to check if a specific file system is mounted. For example, to check if the root file system (/) is mounted, you can use:

findmnt /

This will show the details for the root mount point if it exists.

The findmnt command provides a clear and organized way to view your system's mount points, which is very helpful for system administration and troubleshooting.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if a disk is mounted in Linux using various command-line tools. You started by using the df -h command to list mounted file systems and their disk usage in a human-readable format, understanding the output columns like Filesystem, Size, Used, Avail, Use%, and Mounted on.

You also explored how to inspect mount points by examining the contents of the /proc/mounts file, which provides detailed information about currently mounted file systems. Finally, you learned to verify mounts using the findmnt command, a more modern and versatile tool for displaying file system mount information.