thelinuxvault guide

Mastering Linux Disk Quotas: A Step-by-Step Guide

In a multi-user Linux environment—whether a shared server, a corporate network, or a cloud instance—uncontrolled disk space usage can lead to critical issues: system slowdowns, application failures, or even complete outages when the disk fills up. **Disk quotas** are a powerful solution to this problem, enabling administrators to limit how much disk space and how many files (inodes) individual users or groups can consume. This guide will walk you through everything you need to know to implement, configure, and manage disk quotas on Linux. From understanding quota types to troubleshooting common issues, we’ll cover both foundational concepts and practical, hands-on steps for popular filesystems like ext4 and XFS.

Table of Contents

  1. Understanding Disk Quotas
    • Types of Quotas
    • Key Components: Soft Limits, Hard Limits, and Grace Periods
  2. Prerequisites
    • Supported Filesystems
    • Required Tools
  3. Enabling Quota Support
    • For ext4 Filesystems
    • For XFS Filesystems
    • Verifying Kernel Support
  4. Configuring Quotas
    • Step 1: Initialize Quota Databases with quotacheck
    • Step 2: Set Quota Limits with edquota
    • Step 3: Enable Quotas with quotaon
    • User vs. Group Quotas
    • Setting Grace Periods
  5. Monitoring and Managing Quotas
    • Checking Quota Usage with quota
    • Generating Reports with repquota
    • Automating Alerts with warnquota
  6. Advanced Topics
    • Project Quotas (XFS)
    • Integrating Quotas with LDAP/Active Directory
  7. Troubleshooting Common Issues
  8. Conclusion
  9. References

1. Understanding Disk Quotas

What Are Disk Quotas?

Disk quotas are system-wide restrictions that limit the amount of disk space (blocks) and the number of files (inodes) a user, group, or project can consume on a filesystem. They prevent a single user from monopolizing resources, ensuring fair usage in shared environments.

Types of Quotas

Linux supports two primary quota types:

  • Block Quotas: Limit disk space usage (measured in kilobytes, megabytes, or gigabytes).
  • Inode Quotas: Limit the number of files/directories a user can create (each file/directory consumes one inode).

Quotas can also be categorized by scope:

  • User Quotas: Apply to individual users.
  • Group Quotas: Apply to all users in a specific group.
  • Project Quotas (Advanced): Apply to directories or “projects” (supported by XFS and some ext4 setups).

Key Components

To configure quotas effectively, you need to understand three core concepts:

ComponentDefinition
Soft LimitA warning threshold. Users can exceed this temporarily but will be notified.
Hard LimitA strict ceiling. Users cannot exceed this, even temporarily.
Grace PeriodThe time window (e.g., 7 days) during which a user can exceed the soft limit before it’s enforced as a hard limit.

2. Prerequisites

Before enabling quotas, ensure your system meets these requirements:

Supported Filesystems

Quotas are supported on most Linux filesystems, but setup steps vary. We’ll focus on the two most common:

  • ext4: Default for many Linux distributions (Ubuntu, Debian, CentOS).
  • XFS: Popular for large-scale systems (Red Hat, Oracle Linux, cloud environments).

Unsupported filesystems: FAT32, NTFS (via FUSE), and some legacy filesystems (e.g., ext2 without patches).

Required Tools

Install quota management tools using your package manager:

  • Debian/Ubuntu:

    sudo apt update && sudo apt install quota quotatool  
  • RHEL/CentOS/Rocky Linux:

    sudo dnf install quota quota-nls  
  • Arch Linux:

    sudo pacman -S quota-tools  

Verify installation with:

quota --version  

3. Enabling Quota Support

Quota setup differs slightly between ext4 and XFS. Follow the steps for your filesystem.

For ext4 Filesystems

Step 1: Edit /etc/fstab

Add quota options to the target filesystem in /etc/fstab to enable user and group quotas.

  1. Open /etc/fstab with a text editor (e.g., nano):

    sudo nano /etc/fstab  
  2. Locate the line for your target filesystem (e.g., /dev/sda2 mounted at /home). Add usrquota (user quotas) and grpquota (group quotas) to the defaults option:

    /dev/sda2  /home  ext4  defaults,usrquota,grpquota  0  2  

    Note: Use usrquota for user quotas and/or grpquota for group quotas.

Step 2: Remount the Filesystem

Apply the changes by remounting the filesystem:

sudo mount -o remount /home  

Verify the mount options with:

mount | grep /home  
# Output should include "usrquota,grpquota"  

For XFS Filesystems

XFS handles quotas natively via the xfs_quota tool, and no /etc/fstab modifications are needed. Quotas are enabled dynamically.

Verify XFS quota support:

xfs_quota -x -c "help" /mountpoint  
# If no errors, quota support is available.  

Verifying Kernel Support

Ensure the Linux kernel supports quotas:

  • For ext4: Check if the quota_v2 module is loaded:

    lsmod | grep quota_v2  
    # If not loaded, run: sudo modprobe quota_v2  
  • For XFS: XFS quota support is built into the kernel (no separate module needed).

4. Configuring Quotas

Once quota support is enabled, follow these steps to set limits. We’ll use ext4 for examples, with XFS notes where applicable.

Step 1: Initialize Quota Databases with quotacheck

The quotacheck command scans the filesystem to create/update quota databases (aquota.user for users, aquota.group for groups on ext4). These files store quota limits and usage data.

For ext4:
Run quotacheck with the -cug flags (create, user, group):

sudo quotacheck -cug /home  
  • -c: Create quota files if they don’t exist.
  • -u: Scan user quotas.
  • -g: Scan group quotas.
  • -v: Verbose mode (optional, for debugging).

Output: Creates aquota.user and aquota.group in /home.

For XFS:
XFS does not use external quota files. Instead, run xfs_quota to initialize:

sudo xfs_quota -x -c "enable -ug" /home  
# -x: Expert mode (required for most operations).  
# -c "enable -ug": Enable user and group quotas.  

Step 2: Set Quota Limits with edquota

Use edquota (edit quota) to define soft/hard limits for users or groups.

Example 1: Configure User Quotas

Let’s set quotas for a user named alice:

  1. Launch the quota editor for alice:

    sudo edquota -u alice  
  2. A text editor (e.g., nano) will open with a template like this:

    Disk quotas for user alice (uid 1001):  
      Filesystem                   blocks       soft       hard     inodes     soft     hard  
      /dev/sda2                     50000          0          0         100        0        0  
    • blocks: Current disk space used (KB).
    • soft/hard (blocks): Soft/hard limits for disk space (e.g., 10GB = 10,485,760 KB).
    • inodes: Current number of files/directories used.
    • soft/hard (inodes): Soft/hard limits for inodes (e.g., 10,000 files).
  3. Set limits (e.g., soft 10GB, hard 15GB; inodes soft 10k, hard 15k):

    Disk quotas for user alice (uid 1001):  
      Filesystem                   blocks       soft       hard     inodes     soft     hard  
      /dev/sda2                     50000    10485760    15728640         100      10000     15000  

    Note: 1 GB = 1,048,576 KB. Adjust values as needed.

  4. Save and exit the editor.

Example 2: Configure Group Quotas

To set quotas for the developers group:

sudo edquota -g developers  

Edit the limits similarly (e.g., soft 100GB, hard 150GB for the group).

Example 3: Copy Quotas Between Users

To replicate alice’s quotas to bob:

sudo edquota -p alice bob  

Step 3: Enable Quotas with quotaon

Use quotaon to activate quotas (ext4 only; XFS uses xfs_quota enable).

For ext4:

sudo quotaon -uv /home  
# -u: Enable user quotas.  
# -g: Enable group quotas (add if needed).  
# -v: Verbose.  

To enable all quotas on all filesystems:

sudo quotaon -av  

For XFS:
Quotas are already enabled via xfs_quota -c "enable -ug", but you can verify with:

sudo xfs_quota -x -c "report -ug" /home  

Step 4: Set Grace Periods

By default, the grace period for soft limits is 7 days. Use edquota -t to adjust:

sudo edquota -t  

Edit the window (e.g., 3 days for blocks, 5 days for inodes):

Grace period before enforcing soft limits for users:  
Time units may be: days, hours, minutes, or seconds  
  Filesystem             Block grace period     Inode grace period  
  /dev/sda2                     3 days              5 days  

5. Monitoring and Managing Quotas

Check Individual User/Group Usage

Use quota to view usage for a specific user/group:

# For user alice:  
quota -u alice  

# For group developers:  
quota -g developers  

Sample Output:

Disk quotas for user alice (uid 1001):  
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace  
     /dev/sda2    80000 10485760 15728640          150    10000   15000  

Generate System-Wide Reports with repquota

The repquota command provides a summary of all users/groups on a filesystem:

sudo repquota -a  
# -a: Report all filesystems with quotas enabled.  

Sample Output:

*** Report for user quotas on device /dev/sda2  
Block grace time: 3days; Inode grace time: 5days  
                        Block limits                File limits  
User            used    soft    hard  grace    used  soft  hard  grace  
----------------------------------------------------------------------  
root      --   10000       0       0         1000     0     0       
alice     --   80000 10485760 15728640          150 10000 15000       
bob       --   50000  5242880  7864320           50  5000  7500       

Automate Alerts with warnquota

warnquota sends email notifications to users exceeding their soft limits.

  1. Configure email settings in /etc/warnquota.conf (set ADMIN and FROM).
  2. Run warnquota manually or via cron:
    sudo warnquota  

Add a cron job to run daily:

sudo crontab -e  
# Add: 0 9 * * * /usr/sbin/warnquota  

6. Advanced Topics

Project Quotas (XFS)

XFS supports project quotas to limit usage for specific directories (e.g., /var/www).

  1. Define a project in /etc/projects:

    echo "100:/var/www" | sudo tee -a /etc/projects  
  2. Map the project to a name in /etc/projid:

    echo "web_files:100" | sudo tee -a /etc/projid  
  3. Set a project quota (e.g., 50GB soft, 60GB hard):

    sudo xfs_quota -x -c "limit -p bsoft=50g bhard=60g web_files" /var/www  
  4. Report project quotas:

    sudo xfs_quota -x -c "report -p" /var/www  

Integrating Quotas with LDAP/Active Directory

For environments with centralized user management (LDAP/AD), automate quota setup with scripts:

  1. Use ldapsearch to fetch user/group lists.
  2. Loop through users and set quotas with edquota -p (copy from a template user).

Example script snippet:

#!/bin/bash  
# Fetch LDAP users and set quotas  
for user in $(ldapsearch -x -b "ou=users,dc=example,dc=com" uid | grep uid: | awk '{print $2}'); do  
  sudo edquota -p template_user "$user"  
done  

7. Troubleshooting Common Issues

”quotaon: Cannot find quota file” (ext4)

  • Cause: Quota files (aquota.user/aquota.group) are missing.
  • Fix: Run sudo quotacheck -cug /home to recreate them.

”Filesystem not mounted with quota options” (ext4)

  • Cause: /etc/fstab lacks usrquota/grpquota, or the filesystem wasn’t remounted.
  • Fix: Edit /etc/fstab, then run sudo mount -o remount /home.

”User exceeds hard limit but can still write files”

  • Cause: Quotas are disabled.
  • Fix: Enable quotas with sudo quotaon -uv /home (ext4) or xfs_quota -c "enable -ug" /home (XFS).

XFS Quota “Operation not permitted”

  • Cause: Not running xfs_quota in expert mode (-x).
  • Fix: Always use xfs_quota -x for configuration (e.g., xfs_quota -x -c "limit ...").

8. Conclusion

Disk quotas are a critical tool for maintaining stability in shared Linux environments. By following this guide, you’ve learned to enable quota support, configure soft/hard limits, monitor usage, and troubleshoot issues. Remember to:

  • Test quotas in a non-production environment first.
  • Regularly review repquota reports to adjust limits.
  • Use warnquota to keep users informed of approaching limits.

With quotas in place, you’ll ensure fair resource allocation and prevent disk-related outages.

9. References