Linux df Command: Disk Space Reporting

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will explore the df command, a crucial tool for monitoring disk space usage on Linux systems. You'll take on the role of a system administrator managing a busy web server, using df to analyze disk usage, identify potential storage issues, and ensure smooth operation of the server. This lab is designed for beginners, so don't worry if you're new to Linux - we'll guide you through each step carefully.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") subgraph Lab Skills linux/df -.-> lab-219188{{"`Linux df Command: Disk Space Reporting`"}} end

Understanding the Basic df Command

As a system administrator, your first task is to get an overview of the disk space usage on your web server. The df command without any options provides a quick snapshot of all mounted filesystems.

First, let's navigate to the project directory:

cd ~/project

Now, let's run the basic df command:

df

Tips: Due to the dynamic changes in virtual machine storage, the output of the df command may be inconsistent, and this issue can be ignored.

You should see output similar to this:

Filesystem     1K-blocks     Used Available Use% Mounted on
overlay         20971520  1314600  19656920   7% /
tmpfs              65536        0     65536   0% /dev
tmpfs            4068320        0   4068320   0% /sys/fs/cgroup
shm                65536        0     65536   0% /dev/shm
/dev/vdb       104806400 17442788  87363612  17% /etc/hosts
tmpfs             102400    51200     51200  50% /mnt/ramdisk

Let's break down this output:

  • Filesystem: This column shows the name of the device or partition.
  • 1K-blocks: This shows the total size of the filesystem in 1-kilobyte blocks.
  • Used: This indicates how many 1K-blocks are used.
  • Available: This shows how many 1K-blocks are available.
  • Use%: This shows the percentage of the filesystem that is used.
  • Mounted on: This shows where in the directory tree the filesystem is mounted.

Notice the overlay filesystem, which is commonly used in containerized environments. The /dev/vdb device is likely a virtual disk, and we can see a tmpfs filesystem mounted at /mnt/ramdisk, which is a RAM-based filesystem.

Making df Output Human-Readable

The default output of df shows sizes in 1K blocks, which can be difficult to interpret quickly. Imagine trying to figure out how many gigabytes 20640796 1K-blocks is! Fortunately, we can use the -h option (which stands for "human-readable") to make the output easier to understand.

Run the following command:

df -h

You should see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  1.3G   19G   7% /
tmpfs            64M     0   64M   0% /dev
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vdb        100G   17G   84G  17% /etc/hosts
tmpfs           100M   50M   50M  50% /mnt/ramdisk

Notice how the sizes are now in GB and MB. This is much easier to understand at a glance. For example, we can quickly see that our main filesystem (overlay) is 20G in size, with only 1.3G used and 19G available.

Displaying Filesystem Types

As a system administrator, it's important to know the types of filesystems on your server. Different filesystem types have different features and performance characteristics. The -T option allows you to view this information.

Run the following command:

df -T

You should see output similar to this:

Filesystem     Type    1K-blocks     Used Available Use% Mounted on
overlay        overlay  20971520  1314600  19656920   7% /
tmpfs          tmpfs       65536        0     65536   0% /dev
tmpfs          tmpfs     4068320        0   4068320   0% /sys/fs/cgroup
shm            tmpfs       65536        0     65536   0% /dev/shm
/dev/vdb       xfs     104806400 17442728  87363672  17% /etc/hosts
tmpfs          tmpfs      102400    51200     51200  50% /mnt/ramdisk

This output includes a new column, Type, which shows the filesystem type for each mounted filesystem. Let's look at the types we see:

  • overlay: This is a type of union filesystem, often used in containerized environments.
  • tmpfs: This is a temporary filesystem that resides in memory and/or swap partition.
  • xfs: This is a high-performance journaling filesystem, used here for the /etc/hosts mount.

Understanding filesystem types can help you troubleshoot issues and optimize your system's performance.

Analyzing Inode Usage

In Linux, inodes are data structures that store important information about files, such as their permissions, ownership, and location on the disk. Each file on your system uses one inode. Interestingly, it's possible to run out of inodes even if you have plenty of disk space available, which would prevent you from creating new files. This is why monitoring inode usage is crucial.

Let's check inode usage using the -i option:

df -i

You should see output similar to this:

Filesystem       Inodes  IUsed    IFree IUse% Mounted on
overlay        52428800 539001 51889799    2% /
tmpfs           1017080    206  1016874    1% /dev
tmpfs           1017080     17  1017063    1% /sys/fs/cgroup
shm             1017080      1  1017079    1% /dev/shm
/dev/vdb       52428800 539001 51889799    2% /etc/hosts
tmpfs           1017080      2  1017078    1% /mnt/ramdisk

Let's break down this output:

  • Inodes: The total number of inodes for the filesystem
  • IUsed: The number of inodes currently in use
  • IFree: The number of free inodes
  • IUse%: The percentage of inodes used

In this example, we can see that our main filesystem (overlay) is using only 2% of its available inodes. This is good - it means we're nowhere near running out of inodes, which could prevent us from creating new files even if we had disk space available.

Combining Options for Comprehensive Analysis

Now that we've explored individual options, let's combine them for a more comprehensive view of our server's storage situation. We can use multiple options together to get all the information we need in one command.

Run the following command:

df -hT

This command combines the human-readable (-h) and filesystem type (-T) options.

You should see output similar to this:

Filesystem     Type     Size  Used Avail Use% Mounted on
overlay        overlay   20G  1.3G   19G   7% /
tmpfs          tmpfs     64M     0   64M   0% /dev
tmpfs          tmpfs    3.9G     0  3.9G   0% /sys/fs/cgroup
shm            tmpfs     64M     0   64M   0% /dev/shm
/dev/vdb       xfs      100G   17G   84G  17% /etc/hosts
tmpfs          tmpfs    100M   50M   50M  50% /mnt/ramdisk

This output provides a comprehensive overview of your filesystems, including their types and human-readable sizes. It's a great way to get a quick but detailed snapshot of your system's storage situation.

Focusing on a Specific Filesystem

As a system administrator, you might often need to check the status of a specific filesystem. For example, you might want to quickly check the space on the root filesystem. We can do this by specifying the mount point after the df command.

Let's focus on the root filesystem. Run the following command:

df -h /

You should see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  1.3G   19G   7% /

This command gives you a quick view of the space usage on the root filesystem. It's particularly useful when you're only interested in one specific filesystem and don't need information about all the others.

Checking Available Space for User Home Directories

As part of your server management duties, you need to ensure that users have enough space in their home directories. On many systems, user home directories are located in the /home directory. Let's check the available space in the /home directory.

Run the following command:

df -h /home

You might see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  1.3G   19G   7% /

This shows that the /home directory is on the same filesystem as the root directory (/). In this case, we can see that there's plenty of space available (19G) for user home directories.

If you see that the filesystem is getting full (a high Use% value), you might need to investigate which users are using the most space or consider increasing the storage capacity.

Excluding Certain Filesystem Types

Sometimes, you might want to exclude certain filesystem types from the df output. For instance, you might want to exclude temporary filesystems, which are stored in memory and don't represent physical disk usage. We can do this using the -x option.

Run the following command to exclude tmpfs filesystems:

df -h -x tmpfs

You should see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  1.3G   19G   7% /
/dev/vdb        100G   17G   84G  17% /etc/hosts

Notice that the tmpfs filesystems are no longer listed in the output. This can be useful when you're only interested in physical disk usage and want to filter out virtual filesystems.

Displaying a Total Summary

As a final overview, let's display a total summary of all filesystems. We can do this using the --total option. This is particularly useful when you want to see the overall disk usage across all filesystems.

Run the following command:

df -h --total

You should see output similar to this:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  1.3G   19G   7% /
tmpfs            64M     0   64M   0% /dev
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/vdb        100G   17G   84G  17% /etc/hosts
tmpfs           100M   50M   50M  50% /mnt/ramdisk
total           125G   18G  107G  15% -

This provides a total summary of disk usage across all filesystems. The last line shows the total across all filesystems, indicating that overall, 18G out of 125G is used, which is about 15% of the total available space.

Summary

In this lab, you've learned how to use the df command to monitor and analyze disk space usage on a Linux system. You've explored various options of the df command, including:

  • Basic usage of df
  • Making output human-readable with -h
  • Displaying filesystem types with -T
  • Analyzing inode usage with -i
  • Combining options for comprehensive analysis
  • Focusing on specific filesystems
  • Excluding certain filesystem types with -x
  • Displaying a total summary with --total

These skills are crucial for effective system administration and ensuring optimal performance of your Linux servers. As you continue your journey in system administration, you'll find the df command to be an invaluable tool in your toolkit.

Additional df options not covered in this lab include:

  • -a: Show all filesystems, including pseudo, duplicate, and inaccessible filesystems
  • -k: Display sizes in kilobytes (default)
  • -m: Display sizes in megabytes
  • -P: Use POSIX output format
  • --sync: Invoke sync before getting usage info
  • -t: Limit listing to filesystems of a specific type

Remember, while df is powerful, it's just one of many tools available for monitoring disk usage. As you progress in your Linux journey, you might want to explore other commands like du (disk usage), ncdu (NCurses Disk Usage), and various system monitoring tools.

Other Linux Tutorials you may like