quota Command in Linux

Introduction to Linux Disk Quotas

In this hands-on lab, you'll delve into the world of Linux disk quotas and discover how to leverage the quota command for effective user disk space management. This tutorial covers essential disk quota concepts, including soft and hard limits, providing a comprehensive guide to monitoring and controlling disk usage. You will learn practical techniques for setting disk quotas for individual users and mastering the tools for tracking and managing quota consumption. Get ready for real-world examples that will empower you to confidently apply the quota command within any Linux environment as a systemadmin.

Understanding Disk Quota Fundamentals

Let's begin by exploring the core concepts of disk quotas in Linux. A disk quota system allows system administrators, like yourself, to impose restrictions on the amount of storage a user or group can utilize on a specific file system. This is critical for maintaining system stability and preventing resource exhaustion.

To illustrate this, let's first examine the current disk usage of your system using the following command:

df -h

Example output:

Filesystem      Size  Used Avail Use% Mounted on
overlay         39G   15G   22G  41% /
tmpfs           64M     0   64M   0% /dev
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
shm             64M     0   64M   0% /dev/shm
/dev/sda1       39G   15G   22G  41% /
tmpfs           3.9G     0  3.9G   0% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup

The output displays the total capacity (39GB in this case), the amount of disk space used (15GB), and the available space (22GB). This provides an overview of the system's overall disk utilization.

Now, imagine you want to restrict the disk space available to a particular user. This is where disk quotas become essential. They enable you to define limits on disk space consumption for both users and groups, ensuring fair resource allocation.

Here are the key elements of disk quota management:

  1. Soft Limit: The soft limit represents a threshold. When a user's disk usage exceeds this limit, they receive warnings, but they can continue to use the file system for a grace period. These warnings serve as gentle reminders to manage their data.

  2. Hard Limit: The hard limit is an absolute boundary. Once a user reaches this limit, they are prevented from allocating any further disk space. Any attempts to save new files or increase existing ones will fail, enforcing strict disk usage control.

  3. Inodes: In addition to disk space, quotas can also regulate the number of files (inodes) a user can create. This prevents the proliferation of numerous small files, which can also negatively impact disk performance and availability.

By understanding these fundamental concepts, you will be well-equipped to effectively control disk space usage on your Linux system using the quota command. We'll explore the practical application of this command in the upcoming sections.

Setting Up Disk Quotas for Linux Users

In this section, you'll learn the step-by-step process of setting disk quotas for users on a Linux system, a crucial skill for any systemadmin.

First, let's create a dedicated user account named quota_user. We'll also add this user to the sudo group, granting them administrative privileges for testing purposes:

sudo useradd -m quota_user
sudo usermod -aG sudo quota_user

Next, you must enable disk quota functionality for the file system where the user's home directory resides. Typically, this is the root file system (/). Use these commands to achieve this:

sudo quotacheck -cug /
sudo quotaon /

The quotacheck command performs a scan of the file system, building a table that reflects current disk usage and enabling quota accounting. The quotaon command then activates quota enforcement, making the limits effective.

Now, let's define the soft and hard limits for our test user, quota_user:

sudo setquota -u quota_user 500M 1G 0 0 /

This command sets a soft limit of 500MB and a hard limit of 1GB for quota_user on the root file system (/). The trailing zeros represent the inode soft and hard limits, which we're omitting in this example.

To verify that the quota settings have been applied correctly, use the repquota command:

sudo repquota /

Example output:

*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root       --   15360        0       0              1     0     0
quota_user     0     500M     1G               0     0     0

The output confirms that quota_user now has a soft limit of 500MB and a hard limit of 1GB for their disk space allocation.

Let's test the quota system by logging in as quota_user and attempting to create a file that exceeds the soft limit:

sudo -u quota_user touch ~/large_file.txt
sudo -u quota_user dd if=/dev/zero of=~/large_file.txt bs=1M count=600

The first command creates an empty file, and the second fills it with 600MB of data. Since this exceeds the 500MB soft limit, the user should receive a warning, indicating the quota is being exceeded. This demonstrates the functionality in action.

Monitoring and Managing Disk Quota Usage Effectively

In this concluding step, we'll focus on how to monitor and manage disk quota usage effectively, ensuring your Linux system runs smoothly.

Begin by checking the current disk quota usage for the quota_user:

sudo repquota /

Example output:

*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root       --   15360        0       0              1     0     0
quota_user     600M   500M     1G               0     0     0

The output shows that quota_user is currently using 600MB, exceeding the 500MB soft limit.

For real-time monitoring, use the quota command:

sudo -u quota_user quota

Example output:

Disk quotas for user quota_user (uid 1001):
  Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
  /           614400  512000  1048576 [EXCEEDED]       0       0       0

This command provides a detailed view of the user's disk usage, soft limit, and hard limit on the root file system (/).

When a user surpasses the soft limit, they'll receive periodic warnings. To manage this, you have two primary options:

  1. Increase the soft and/or hard limits using the setquota command, as demonstrated earlier.
  2. Advise the user to remove unnecessary files to reduce their disk footprint.

Let's increase the soft limit for quota_user:

sudo setquota -u quota_user 1G 2G 0 0 /

This command raises the soft limit to 1GB and the hard limit to 2GB for the user on the root file system (/).

Verify the updated quota settings:

sudo repquota /

Example output:

*** Report for user quotas on device /
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root       --   15360        0       0              1     0     0
quota_user     600M     1G     2G               0     0     0

The output confirms that the soft limit for quota_user is now 1GB, and the hard limit is 2GB.

Disk Quota Summary

This lab has provided you with a solid understanding of Linux disk quotas, empowering you to effectively manage disk space usage across your system. You've learned how to set limits on disk space for users and groups, explored the significance of soft and hard limits, and gained hands-on experience with monitoring and adjusting quota settings. With this knowledge, you are now prepared to implement and manage disk quotas effectively within your Linux environment, contributing to a stable and efficient system.

400+ Linux Commands