+91-81782-71104

Home » Automotive » Setting Up User Quotas on Ext4 File System in CentOS 7.9

Setting Up User Quotas on Ext4 File System in CentOS 7.9

In this article, we will walk through the process of setting up and managing user quotas on an ext4 filesystem in CentOS 7.9. We’ll go from checking the filesystem to applying custom quotas for multiple users using a shell script.

1. Fstab Entry for Quota Configuration

First, ensure that the /etc/fstab file contains the necessary entry for enabling quotas. The entry for /home/userfolder in /etc/fstab should look something like this:
UUID=xxxxxxxxx-f5xx-43xx-xxbb-xxxxxxxxxxxxxx /home/userfolder auto defaults,rw,nofail,usrquota,grpquota 0 0

This entry enables user quotas (usrquota), group quotas (grpquota), and ensures that the filesystem is mounted with the correct options to support quota management.

2. Verify Mount and File System Type

To verify that the filesystem is mounted correctly, check the mount point and its filesystem type:

[root@prac1 ~]# mount | grep /home/userfolder
/dev/mapper/centos-7.9 on /home/userfolder type ext4 (rw,relatime,seclabel,data=ordered)

This confirms that the /home/userfolder directory is mounted with ext4, and it is ready to have quotas enabled.

3. Install Quota Package

Before proceeding, ensure the quota package is installed on your system. This can be done using the following command:

yum install quota -y

4. Remount to Enable Quota

To enable quotas on the filesystem, remount the partition with the appropriate options. Run the following command:

sudo mount -o remount /home/userfolder

You should see the filesystem mounted with the usrquota and grpquota options, as shown:

/dev/mapper/centos-7.9 on /home/userfolder type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)

5. Run Quota Check

Next, run the quotacheck command to check and create the necessary quota files:

sudo quotacheck -um /home

This command checks the /ve filesystem for existing quotas and creates the necessary quota files (aquota.user and aquota.group).

6. Enable Quotas

Once the quota check is complete, enable user quotas on the /ve filesystem:

sudo quotaon -uv /home/userfolder

This will turn on the user quotas for /home/userfolder, and you should see output similar to:

/dev/mapper/centos-7.9 [/home/userfolder]: user quotas turned on

7. Verify Quotas Are Active

You can verify that quotas are enabled by using the following command:

quotaon -ap

This will display the status of quotas for all mounted filesystems.

8. Set Quotas for Users

To set quotas for individual users, use the setquota command for ext4. For example, to set a soft limit of 1.8GB and a hard limit of 2GB for the user username, run the following command:

sudo setquota -u <username> 1800M 2048M 0 0 /home/userfolder

This will apply the quota limits to the specified user.

9. Verify Quotas for Users

After applying the quotas, verify them using the repquota command. This will show a summary of disk usage and quotas for all users:

sudo repquota /home/userfolder

The output will display the current quota usage and limits for users on the /home/userfolder filesystem.

10. Automate Quota Setup for Multiple Users

If you have multiple users for whom you need to apply quotas, you can automate the process using a shell script. Below is an example shell script setupquota_ext4.sh that applies the same quota settings for all users in the /home/userfolder directory.

#!/bin/bash

mntpnt=/home/userfolder
users="$( ls $mntpnt )"

for user in $users
do
    echo "Setting quota for user: $user"
    sudo setquota -u $user 1800M 2048M 0 0 /home/userfolder
done

This script:

  • Lists all users in /home/userfolder.
  • Sets a soft limit of 1.8GB and a hard limit of 2GB for each user using setquota.
  • Automates the quota setup process for all users in the directory.

11. Final Check

After running the script, you can perform a final check to ensure the quotas were set correctly for all users:

sudo repquota /home/userfolder

This will show a report of quota usage and limits for each user in the /ve filesystem.


Conclusion

By following these steps, you can successfully enable and manage user quotas on an ext4 filesystem in CentOS 7.9. The process involves configuring the filesystem, installing necessary tools, and applying quotas either manually or through a shell script for automation. This ensures that users are limited in their disk usage according to the policies set by the system administrator.

Leave a Reply

Your email address will not be published. Required fields are marked *