How to manage disk quota in Linux step by step

Linux treats unused disk space as a shared resource. It allows everyone to use it. Anyone can create a file of any size or many files as long as the disk space is available. If a user fills up all the space, no space remains for others. Other users can not save their work or run applications without free disk space. An administrator can solve this issue by using disk quotas. This tutorial explains how to set up, verify, and manage disk quotas for users and groups on an XFS file system.

Selecting a disk for practice

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. The sda disk is blank. 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 a partition, use the mkfs command to create a file system on it. 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 xfs 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.

XFS v/s EXT file systems

Linux supports many file system types. Among them, EXT and XFS are the main ones. Starting from RHEL 7, XFS is the default file system. Before XFS, EXT was the default file system. Disk quotas work differently on both file systems. XFS provides it as the default functionality. EXT uses the quota package for it. This tutorial explains disk quota implementation on the XFS file system. Commands and steps described in this tutorial will not work on other file systems. If you have an EXT or other file system type, use the commands and steps explained in the fourth 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

XFS disk quota options

XFS provides quota as part of the file system. It includes six quota options. If any of these options are specified, it implements them when we or the boot process mounts the partition. The following table lists these options.

Quota optionDescription
uquota Enable user quotas for reporting and enforce usage limits.
gquota Enable group quotas for reporting and enforce usage limits.
pquota Enable project quotas for reporting and enforce usage limits.
uqnoenforce Enable user quotas only for reporting. Do not enforce usage limits.
gqnoenforce Enable group quotas only for reporting. Do not enforce usage limits.
pqnoenforce Enable project quotas only for reporting. Do not enforce usage limits.

Group and project quotas are mutually exclusive. We cannot implement both at the same time. However, we can implement user quota with group or project quota. Commands and steps to configure, test, and verify all quota types are the same. For demonstration, I will implement user quota and group quota. You can configure and verify other quota types by following the same commands and steps.

Implementing disk quotas

Quota options in the /etc/fstab file entry implement quota on the partition or mount point. The uquota option applies user quota. The gquota 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 	xfs	defaults	 0 0

Entry with quota options (updated entry)

/dev/sda1	/qtest 	xfs	defaults,uquota,gquota	 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

Verifying the XFS disk quota

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 uquota and gquota in the options list, it verifies that the quota has been implemented on the partition.

Enabling and starting the XFS disk quota

The XFS automatically enables and starts the configured disk quota options for the partition at boot time when Linux mounts it on the filesystem. After starting them, it continuously monitors them. If we create or update a quota entry, it immediately applies that.

The xfs_quota tool

XFS file system provides the xfs_quota tool to view, create, manage, and delete quota entries. It works in two modes: basic and expert. In the basic mode, it supports only reporting functionality. This mode does not allow users to create and update quotas. All users can use this mode to view their quota limits and usage. In the expert mode, it supports all functionalities. However, only administrators can use this mode.

This tool also supports interactive mode. In interactive mode, it starts a sub-command mode and accepts options as commands.

The following command starts interactive mode for basic uses.

#xfs_quota

The following command starts interactive mode for administrative use.

#xfs_quota -x

Use the quit command to exit from the interactive mode.

interactive mode

Interactive mode is optional. You can run all commands without interactive mode by using related options.

The following table lists essential options.

Option Description
c Command to execute
x Run command in expert mode
bsoft Set the soft limit value for block size
bhard Set the hard limit value for block size
isoft Set the soft limit value for inode
ihard Set the hard limit value for inode
timer Set the grace period
limit Set quota limit
report Display quota reports

Command syntax

The following command line syntax creates a new block quota entry.

#xfs_quota -x -c "limit bsoft=[value] bhard=[value] username" quota_path

The following command line syntax sets a grace period.

#xfs_quota -x -c "timer [value] username" quota_path

The following command displays quota reports.

#xfs_quota -x -c "report -h" /qtest

User quota example (Block size)

Create a user account for testing.

#useradd rheluser1
#passwd rheluser1

Change the file permission of the partition you created for 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 a restrictive permission as your environment requires.

#chmod 777 /qtest

Create a quota entry for rheluser1 that sets the soft limit for block size to 50M and the hard limit to 100M. Set a grace period of 2 minutes.

The following command creates a quota entry for rheluser1 on the /qtest directory that sets the soft limit to 50 Mb and the hard limit to 100 Mb.

#xfs_quota -x -c "limit bsoft=50m bhard=100m rheluser1" /qtest

The following command sets a grace period of 2 minutes for rheluser1.

#xfs_quota -x -c "timer 2m rheluser1" /qtest

The following command displays quota reports.

#xfs_quota -x -c "report -h" /qtest

user quota

Testing and verifying the block size quota

Switch the user account to rheluser1 and change the current directory to /qtest.

#su rheluser1
$cd /qtest

Create a file of 60 MB to cross the soft limit. The dd command allows us to create a file of any size with raw data for testing.

$dd if=/dev/zero of=/qtest/file1 bs=1M count=60

Exit the user account and check quota entries.

$exit
#xfs_quota -x -c "report -h" /qtest

reporting quota

The above output verifies the user has crossed his soft limit and a timer in the grace period field has started.

Switch the user account again and use the dd command to create one more file of 50MB.

#su rheluser1
$cd /qtest
$dd if=/dev/zero of=/qtest/file2 bs=1M count=50

This time, the dd command creates a file of only 40 MB. After 40 MB, the user crosses his hard limit, and the command returns the Disk quota exceeded error.

Exit the user account and recheck the quota report.

$exit
#xfs_quota -x -c "report -h" /qtest

testing quota limit

Testing and verifying the inode quota

Remove all files and directories from the testing directory and create a quota entry that sets the soft inode limit to 5 and the hard limit to 10.

#xfs_quota -x -c "limit isoft=5 ihard=10 rheluser1" /qtest

Set the grace period to 2 minutes.

#xfs_quota -x -c "timer 2m rheluser1" /qtest
creating inode limit

Switch the user account and change the current directory to the testing directory.

#su rheluser1
$cd /qtest

Create six directories and exit the user account.

$mkdir d1 d2 d3 d4 d5 d6
$exit

Recheck the quota report. Wait till the grace period is over.

Switch to rheluser1 again, change the current directory to /qtest, and create a new directory.

#su rheluser1
$cd /qutest
$mkdir d7

The system does not allow us to create a new directory even if the user is under the hard limit. Once the grace period is over, the hard limit automatically applies.

Exit the user account and recheck the quota limit.

$exit
#xfs_quota -x -c "report -ih" /qtest

Use the i switch to view the inode report.

testing inode limit

Creating and verifying group quota entries

To create a group quota entry, use the -g option. For example, the following command creates a group quota entry for block size for the student group, setting the soft limit to 1GB and the hard limit to 10GB on the /qtest directory.

#xfs_quota -x -c "limit -g bsoft=1g bhard=10g student" /qtest

The following command creates a group quota entry for inode for the student group, setting the soft limit to 100 and the hard limit to 200 on the /qtest directory.

#xfs_quota -x -c "limit -g isoft=100 ihard=200 student" /qtest

The following command sets the grace period to 2 days.

#xfs_quota -x -c "timer -g 2d student" /qtest

The following command reports block size quota entries for the /qtest directory.

#xfs_quota -x -c "report -h" /qtest

The following command reports inode quota entries for the /qtest directory.

#xfs_quota -x -c "report -ih" /qtest

group quota

Conclusion

Disk quotas in Linux are essential for managing disk space efficiently and preventing individual users from monopolizing system resources. By implementing disk quotas, system administrators can ensure better disk space management, improving the Linux system's performance and reliability.

ComputerNetworkingNotes Linux Tutorials How to manage disk quota in Linux step by step

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