Linux Disk Quota Explained with Examples

Disk quota refers to a disk management system that limits the amount of disk space or the number of files (inodes) a user or group can use on a filesystem. It prevents a single user from utilizing all available space, which can lead to system performance issues or crashes. This tutorial explains how to set up, verify, and manage disk quota in Linux.

The btrfs file system does not support disk quota. We cannot implement disk quotas on it. The xfs file system provides this functionality as part of the file system. All remaining file systems, such as ext3 and ext4, use the quota package to provide this functionality. This tutorial explains how to create and manage disk quotas on the file systems that use the quota package to provide disk quota functionality.

If you have a file system other than xfs and btrfs, use the commands and steps explained in this tutorial to implement disk quotas. If you have an XFS file system, use the third tutorial of this tutorial series.

This tutorial is part of the tutorial series 'Linux Disk Quota Management Explained with Examples'. Other parts of this series are the following.

Chapter 01  Basic Concepts of Disk Quota
Chapter 02  Disk Quota Terminology Explained
Chapter 03  How to manage disk quota in Linux Step-by-Step
Chapter 04  Linux Disk Quota Explained with Examples

Lab setup

We can use any partition or drive to practice disk quota. However, using a separate disk with no meaningful data eliminates the risks of data loss. I attached a new virtual disk to my virtual system to demonstrate the exercises in this tutorial.

attaching hard disk

The lsblk command lists all disks and their partitions.

the lsblk command

As the output shows, this system has two disks: sda and nvme0n1. The nvme0n1 disk contains the operating system and user data. I will not touch it to avoid any chance of data loss. I attached the disk sda for practice. I will use it to create, implement and test disk quota.

The disk you use for practice must contain at least one partition with the file system mounted on the Linux file system. Linux provides three native tools: fdisk, gdisk, and parted for disk partition management. You can use any of these to create partitions on the selected disk. After creating partitions, use the mkfs command to create a file system on partitions. Create a mount point and add an entry for the partition in the /etc/fstab file that mounts it to the mount point at the boot time.

The following figure shows creating a single partition that spans the entire disk using the fdisk command with all default options.

creating a partition

The following figure shows the creation of the ext4 file system on the partition.

creating a file system

The following image shows the mount point creation and the fstab entry.

the fstab entry

After a system restart, use the lsblk command to verify the partition.

the lsblk command

To learn more about these steps, check the following tutorials.

The quota package

The quota package provides all the tools, modules, and configurations we need to implement disk quota. This package is part of the default installation. We can use the following command to verify it.

#dnf list quota

If this package is not present, use the following command to install it.

#dnf install quota

package verification

Implementing disk quota

The usrquota and grpquota options in the /etc/fstab file entry implement quota on the respective partition or mount point. The usrquota option applies user quota. The grpquota option implements group quota. We can specify both or any one option based on our requirements. For example, if we add these options to the /dev/sda1 partition's entry, Linux will implement both quotas on this partition at the boot time.

Open the /etc/fstab file, add these options to the /dev/sda1 partition entry, and save the file.

Entry without quota option (original entry)
/dev/sda1	/qtest 	ext4	defaults	 0 0
Entry with quota options (updated entry)
/dev/sda1	/qtest 	ext4	defaults,usrquota,grpquota	 0 0

/etc/fstab file

Linux reads the /etc/fstab file at the boot time to mount all partitions. Restart the system.

restart the system

After restart, run the following command to verify the disk quota is applied on the /qtest.

#mount | grep /qtest

mount option

If the output shows usrquota and grpquota in the options list, it verifies that the quota has been implemented on the partition.

Enabling and starting quota

The quota package uses two files aquota.user and aquota.group to save quota entries. It uses the aquota.user file to save quota entries for users. To save quota entries for groups, it uses the aquota.group file. By default, these files are not present on the system. The following command creates these files.

#quotacheck -cug [path of the partition]

After creating the necessary database files, we use the following command to start quotas.

#quotaon  [path of the partition]

For example, to enable and start quotas on the /qtest partition, we will use the following commands.

#quotacheck -cug /qtest
#quotaon  /qtest

creating quota database files

Creating quota entries

The edquota command allows us to create and manage quotas. It accepts a username or group name as an argument. The following command creates and manages user quotas.

#edquota [username]

Use the g option to create quota entries for the group.

#edquota -g [group name]

A quota entry has the following seven fields.

NumberName Description
1 Filesystem Partition on which this quota applies
2 blocks Number of blocks currently used
3 soft Soft block size limit
4 hard Hard block size limit
5 inodes Number of inodes presently used
6 soft Soft inodes limit
7 hard Hard inodes limit

Creating and verifying disk quotas for users

Create a user account for testing.

#useradd rheluser1
#passwd rheluser1

Change the file permission of the partition you created for the testing to 777. The 777 permission allows everyone to read, write, and execute. Use this permission only on a testing system. On a production system, set permissions as your environment requires.

#chmod 777 /qtest

Create a quota entry for rheluser1 with the following configuration values.

Block softBlock hardInode softInode hardGrace period
1000200020202 minutes
#edquota rheluser1

Set the above-listed values and save the file.

The default grace period is 7 days. We can change it to a lower time for testing. For example, we can set it to two minutes. The -t option changes it for all users. The -T option changes it for a specific user.

Set the grace period to 2 minutes for both quotas and save the file.

#edquota -t

Do not add a space between the value and the unit. For example, to set 2 minutes, use 2minutes.

editing quota

Testing and verifying the inode quota

Login from or switch the user account to rheluser1 and change the current directory to /qtest.

#su rheluser1
$cd /qtest

testing and verifying indoe quota

Create ten directories. Each directory needs one inode. The soft limit for the inode number is 10. Use the quota command to view the current use and limits.

$mkdir {d1,d2,d3,d4,d5,d6,d7,d8,d9,d10}
$quota

creating directories

Create one more directory and recheck the quota.

$mkdir d11
$quota

Since the user did not cross his hard limit, the system allows the user to create a new directory. Wait for two minutes and create one more directory.

$mkdir d12

This time, the system gives the Disk quota exceeded error.

soft limit

Remove a few directories to bring the total number of directories below the threshold.

$rmdir {d1,d2,d3}
$quota

Create a new directory again. This time, the system allows the user to create a new directory.

$mkdir d12
$quota

testing soft limit

The grace period is only for the soft limit. It does not apply to the hard limit. The system does not allow the user to cross his hard limit. To verify it, remove all directories and create twenty directories.

$rm -rf *
$mkdir {d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d19,d20}
$quota

Create one more directory. The system does not allow it, even if the grace period is valid.

$mkdir d21
$quota

crossing hard limit

Testing and verifying the block size quota

Remove directories to free up inodes and create a file that fills up all blocks. The dd command allows us to create a file of any size with raw data for testing.

$rm -rf *
$ls
$quota
$dd if=/dev/zero of=/qtest/file1 bs=1K count=1000
$quota
soft limi

Create another file of 100 blocks. It crosses the soft limit.

$dd if=/dev/zero of=/qtest/file2 bs=1K count=100
$quota

block size quota

Create one more file of 1000 blocks. The quota will not allow it.

$dd if=/dev/zero of=/qtest/file3 bs=1K count=1000

hard quota limit

Creating and managing group quotas

Creating and managing group quotas are similar to user quotas. We use the same commands for both quota types. The only difference is we use option -g while working with group quotas.

The following command creates a quota entry for the group rhel.

#edquota -g rhel

The following command displays quota reports for the rhel group.

#quota -g rhel

Reporting and monitoring quota

The repquota command with the -a option allows the administrator to monitor and report quota usage on all filesystems.

#repquota -a

repquota

Conclusion

Disk quotas work differently on file systems. In this tutorial, I explained how to implement and manage them on an EXT file system. You can effectively create and manage disk quotas on any EXT file system using the steps and commands outlined in this tutorial.

ComputerNetworkingNotes Linux Tutorials Linux Disk Quota Explained with Examples

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com