This tutorial will explain the process of configuring disk quota on a Linux system with practical examples. The disk quota system is based on Berkeley Disk Quota System and it provides a way for system administrators to control disk space usage. It can be defined for individual Linux users or groups.
The limits established on disk are based on the parameters that you can change with the edquota command for JFS file systems and the j2edlimit command for JFS2 file systems:
- User’s or group’s soft limits
- User’s or group’s hard limits
- Quota grace period
The disk quota system does the tracking of user and group quotas in the quota.user and quota.group files that reside in the root directories of file systems enabled with quotas. These files are created with the quotacheck and edquota commands and are readable with the quota commands.
Let’s first identify the filesystem of our Linux system and if quotas are already enabled.
$ mount | grep ' / '
# Ext4
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)
# XFS
/dev/mapper/cs-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
From the output it’s evident disk quota is not enabled on both ext4 and xfs filesystem.
Enable Disk Quota on XFS Filesystem
From mount | grep ' / '
output check if quotas are disabled, it will have noquota in the output if so.
$ mount | grep ' / '
/dev/mapper/cs-root on / type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
If disk quotas are enabled you will see ‘usrquota’ and ‘groupquota’ from the output.
Edit Grub configuration file using the following commands.
sudo vim /etc/default/grub
Add rootflags=usrquota,grpquota
to the end
GRUB_CMDLINE_LINUX="crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap rootflags=usrquota,grpquota"
Backup your current Grub2 configuration file.
sudo cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bak
For EFI booted systems, the file is located at ‘/boot/efi/EFI/centos/grub.cfg‘
sudo cp /boot/efi/EFI/centos/grub.cfg /boot/efi/EFI/centos/grub.cfg.orig
Generate new Grub configuration file.
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
For EFI
sudo grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
Reboot the server then check
mount | grep ' / '
Setting user quota.
sudo xfs_quota -x -c 'limit bsoft=4g bhard=5g myuser' /home
sudo xfs_quota -x -c 'limit -g bsoft=4g bhard=5g myuser' /home
Show reports.
sudo report -h -u
Enable Disk Quota on EXT4 Filesystem
Check if disk quotas are already enabled in your system.
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)
If quotas are already enabled in the system, the output will contain ‘usrquota’ and ‘groupquota’.
$ mount | grep ' / '
/dev/xvda1 on / type ext4 (rw,usrquota,grpquota)
Edit the /etc/fstab
file and
UUID=<UUID> / ext4 defaults,usrquota,grpquota 0 0
My server example:
UUID=b60999df-940a-4c3a-9128-8312e991abeb / ext4 defaults,usrquota,grpquota,errors=remount-ro 0 1
You can remount the filesystem for the changes to make effect.
sudo mount -o remount /
Install quota package which provides quotacheck command.
sudo apt update && sudo apt install quota -y
Next use quotacheck
command to create quota database. This creates aquota.user
and aquota.group
under /:
sudo quotacheck -cugmv /
Where:
-c
specify that we are creating file quota-u
specify creation of user-based quota file-g
specify creation of group disk quota-m
disables remounting the filesystem as read-only while performing the initial tallying of quotas-v
verbose output
Turn on disk quotas by running the commands below.
sudo quotaon /
Use edquota command to to edit disk quotas for a user.
sudo edquota -u myuser
For group’s quota use the option.
sudo edquota -g mygroup
For a single command use setquota
sudo setquota -u myuser 800M 2G 0 0 /
sudo quota -vs myuser
Quota reports can be printed using.
sudo repquota -s /
Conclusion
In this tutorial we have been able to configure disk quotas on ext4 and xfs filesystems. We also installed the quota command line tools, enable disk quotas for user and group, as well as verifying our Linux kernel can monitoring and reporting on disk quotas. We hope the guide was of help to you.