19. Disk Quotas
On a multi-user system, a single user or application could accidentally (or maliciously) fill up the entire hard drive, crashing the server. Disk Quotas allow administrators to restrict how much disk space and how many files a specific user or group can consume on a filesystem.
19.1 Quota Concepts
Section titled “19.1 Quota Concepts”There are two primary ways to measure quota usage:
- Block Quotas: Limits the total size of the data (e.g., maximum 5 GB).
- Inode Quotas: Limits the total number of files a user can create (e.g., maximum 10,000 files), regardless of how small the files are.
Limits and Grace Periods
Section titled “Limits and Grace Periods”For both Blocks and Inodes, you define two thresholds:
- Soft Limit: A warning threshold. A user can temporarily exceed this limit, but a timer (the Grace Period) begins.
- Hard Limit: The absolute maximum. A user can never exceed this limit. Any attempt to write data past this limit will result in a “Disk quota exceeded” error.
- Grace Period: The amount of time a user is allowed to remain above the Soft Limit (usually 7 days). If they don’t clean up their files before the grace period expires, the Soft Limit converts into a Hard Limit, preventing further writes until they delete data.
19.2 Enabling Quotas
Section titled “19.2 Enabling Quotas”Quotas must be explicitly enabled at the filesystem level.
1. Update /etc/fstab
Section titled “1. Update /etc/fstab”To enable user and group quotas, you must add the usrquota and grpquota options to the mount entry in /etc/fstab.
# Example /etc/fstabUUID=a1b2c3d4... /home ext4 defaults,usrquota,grpquota 0 22. Remount the Filesystem
Section titled “2. Remount the Filesystem”After editing /etc/fstab, remount the filesystem for the options to take effect.
sudo mount -o remount /home3. Initialize Quota Databases
Section titled “3. Initialize Quota Databases”Use the quotacheck command to scan the filesystem and build the initial quota database files (aquota.user and aquota.group).
# -c (create), -u (user), -g (group), -m (don't remount read-only)sudo quotacheck -cugm /home4. Turn on Quotas
Section titled “4. Turn on Quotas”Finally, activate the quota system.
sudo quotaon -v /home19.3 Managing Quotas
Section titled “19.3 Managing Quotas”Setting Limits (edquota)
Section titled “Setting Limits (edquota)”To define the soft and hard limits for a specific user, use edquota. It opens the quota configuration in your default text editor.
# Edit quotas for the user 'alice'sudo edquota -u aliceEditor Output:
Disk quotas for user alice (uid 1001): Filesystem blocks soft hard inodes soft hard /dev/sdb1 0 5000000 6000000 0 10000 12000(In this example, Alice has a 5GB soft block limit and a 6GB hard block limit).
Setting the Grace Period
Section titled “Setting the Grace Period”To modify the global grace period (default is 7 days):
sudo edquota -tCopying Quotas
Section titled “Copying Quotas”If you have configured Alice and want to apply the exact same limits to Bob:
sudo edquota -p alice bob19.4 Monitoring Quota Usage
Section titled “19.4 Monitoring Quota Usage”As an administrator, you need tools to view quota consumption across the system.
quota: Shows the quota usage for the currently logged-in user.Terminal window quota -srepquota: Prints a comprehensive summary report of all user quotas on a specific filesystem. Essential for SysAdmins!Terminal window sudo repquota -a -s
(The -s flag makes the output human-readable, formatting blocks into Megabytes and Gigabytes).