Implement User Based Quota On File System To Control Disk Space

There may be situations where the users utilizing a file system may end stored storing some unnecessary files taking up valuable disk space. In Linux, it is possible to set up disk space quotas on a per-user basis to make sure that users are allowed to use only the chunk of space allocated to them and not anything more. Quotas can also be applied to groups in case we want to limit the space consumption for users belonging to a particular group. Along with setting quotas based on the amount of disk space a user can consume, we can also set a limit on the number of files that a user can create. Let’s see Implement User Based Quota On File system.

Note: Quotas cannot be set up on directories. They are a file system level restriction only.

You can only configure quotas when you install package named quota. Let’s verify that we have the quota system available on our server.

# rpm -qa | grep quota

When we set up disk quotas we can define a soft limit as well as a hard limit for the quota.

Soft limit: This is the limit where the user gets just a warning message saying that your disk quota is going to expire. This gives the users a grace period of 7 days by default to clear up space to fall below the quota threshold.

Hard limit: This is the limit where the user gets an error message and under no circumstances will the user be allowed to exceed this limit.

Implement User Based Quota On File System

With a brief overview of what are user based disk quotas in Linux, we’ll now demonstrate how to implement disk quotas on a file system named /quota_test for the user Arkit. I’ll be using a Centos 6.x/Centos 7.x system for all the practical demonstrations in this article.

Step1: Modify /etc/fstab entry for the file system

Quotas are not implemented on file systems under default mount options, need modifications

[root@Arkit-centos ~]# grep /quota_test /etc/fstab
/dev/test/lv07 /quota_test ext4 defaults 1 1

We need to add the usrquota option to enable disk quotas on the file system
I have modified the /etc/fstab file added the usrquota mount option for the file system entry as shown below

[root@Arkit-centos ~]# grep /quota_test /etc/fstab
/dev/test/lv07 /quota_test ext4 defaults,usrquota,grpquota 1 2

After editing the file we need to remount the file system for the new mount option to take effect
If the file system is not in use then you may unmount and mount the file system back again
But since that would be a disruptive operation, it’s safer to use mount the file system using the mount command with the remount option

# mount -o remount,rw /quota_test

Let’s verify that the changes have taken effect

[root@Arkit-centos ~]# grep /quota_test /proc/mounts
/dev/mapper/test-lv07 /quota_test ext4 rw,seclabel,relatime,barrier=1,data=ordered,usrquota 0 0

As you may observe from the above output, the usrquota option is now enabled for this file system

Step2: Create the quota database

Use the quotacheck command to create a quota database. This will create a file named quota.user at the root of the file system
This file will contain quota related information for the concerned file system on which quotacheck command was run to Implement User Based Quota on File system

# quotacheck -cu /quota_test

Option -c  for creating disk quota DB and -u for user quota
Above command has created a file named aquota.user in the directory /quota_test

Verified the same as shown in the below output:

[root@Arkit-centos ~]# ls -l /quota_test/aquota.user
-rw-------. 1 root root 6144 Nov 28 14:11 /quota_test/aquota.user

Step3: Turn on quotas

In the steps performed thus far, we’ve only enabled quotas for the file system /quota_test
Now will turn on the quota mechanism using the quotaon command

# quotaon /quota_test

If you do not run quotaon now, the mechanism will be turned on automatically during the mount phase of the file system during next reboot.

Step4: Apply quota on a user

Quota limits for disk space is always set in 1KB blocks. Here we set a soft limit of 5MB and hard limit of 10MB.
Use the setquota or edquota command to set the quota.
The edquota is an interactive command and opens up an editor where you can fill in the details for the quota.
For this demonstration, we’ll use the setquota command.

[root@Arkit-centos ~]# setquota -u Arkit 5000 10000 0 0 /quota_test

Command sets a soft limit of 5MB and a hard limit of 10MB to restrict the amount of space the user Arkit can utilize on the /quota_test file system.
Two zeros indicate that we are not setting any soft or hard limits for the inodes i.e. to restrict the number of files that the user can create.

You can use the repquota command to view quota restrictions for users. Implement User Based Quota On file system.

[root@Arkit-centos ~]# repquota -au
*** Report for user quotas on device /dev/mapper/test-lv07
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
root -- 13 0 0 2 0 0
Arkit -- 1 5000 10000 1 0 0

Note: If a user has not created any files/directories in the file system on which the quota has been applied then their name will not be listed in the repquota output.

Step5: Test quota restriction

So we’ve applied a quota restriction for user Arkit to limit the amount of disk space he can consume on the /quota_test file system.
Now let’s test this to make sure it works.

If you would like to view quota restrictions for an individual user only then you may use the quota command followed by the username.

[Arkit@Arkit-centos ~]$ quota Arkit
Disk quotas for user Arkit (uid 507):
Filesystem blocks quota limit grace files quota limit grace
26 5000 10000 1 0 0

Let’s create a file of size 1Mb

[Arkit@Arkit-centos quota_test]$ dd if=/dev/zero of=/quota_test/test.txt bs=512 count=2000
2000+0 records in
2000+0 records out
1024000 bytes (1.0 MB) copied, 0.0136737 s, 74.9 MB/s
[Arkit@Arkit-centos quota_test]$ ls -lh /quota_test/test.txt
-rw-rw-r--. 1 Arkit Arkit 1000K Nov 28 15:41 /quota_test/test.txt

If you run the quota command again, you’ll notice that the file creation has been updated in the quota database

[Arkit@Arkit-centos quota_test]$ quota Arkit
Disk quotas for user Arkit (uid 507):
Filesystem blocks quota limit grace files quota limit grace
1027 5000 10000 2 0 0

Now let’s create a file slightly larger than the soft limit of 5MB.

[Arkit@Arkit-centos quota_test]$ dd if=/dev/zero of=/quota_test/test.txt bs=512 count=10000
dm-8: warning, user block quota exceeded.
10000+0 records in
10000+0 records out
5120000 bytes (5.1 MB) copied, 0.0503911 s, 102 MB/s
[Arkit@Arkit-centos quota_test]$ ls -lh /quota_test/test.txt
-rw-rw-r--. 1 Arkit Arkit 4.9M Nov 28 15:43 /quota_test/test.txt

You can see that we get a warning message.

Now let’s create try to create a file larger than 10MB.

[Arkit@Arkit-centos quota_test]$ dd if=/dev/zero of=/quota_test/test.txt bs=512 count=20000
dm-8: warning, user block quota exceeded.
dm-8: write failed, user block limit reached.
dd: writing `/quota_test/test.txt': Disk quota exceeded
19947+0 records in
19946+0 records out
10212352 bytes (10 MB) copied, 0.110704 s, 92.2 MB/s

[Arkit@Arkit-centos quota_test]$ ls -lh /quota_test/test.txt
-rw-rw-r--. 1 Arkit Arkit 9.8M Nov 28 15:45 /quota_test/test.txt

As you can observe from the above output we were not allowed to exceed the hard limit for the disk space quota set up for the user.

Turning off quotas

We’ve seen how to apply disk space restrictions using the quota system. Now let’s see how to remove quota limitations from a file system.

Step1: Turn off quotas using the quotaoff command

[root@Arkit-centos ~]# quotaoff /quota_test

Step2: Delete the aquota.user file

[root@Arkit-centos ~]# cd /quota_test

[root@Arkit-centos quota_test]# ls
aquota.user lost+found test test.txt

[root@Arkit-centos quota_test]# rm aquota.user
rm: remove regular file `aquota.user'? y

Step3: Modify the file system options and remount the file system

[root@Arkit-centos ~]# umount /quota_test
[root@Arkit-centos ~]# mount /quota_test
[root@Arkit-centos ~]#
[root@Arkit-centos ~]# grep /quota_test /proc/mounts
/dev/mapper/test-lv07 /quota_test ext4 rw,seclabel,relatime,barrier=1,data=ordered 0 0

Step4: Use repquota command to verify that quotas are now turned off

[root@Arkit-centos ~]# repquota -au


In this article we showed you how to implement disk space quotas based on users. Using the quota system can prove to be use very useful for preserving file system space.
As we mentioned earlier, in addition to disk space quotas you can also apply quotas based on number of files/directories that users can create within the file system. Same Steps works for also RHEL 7/Centos 7.

In addition to user based quotas we could implement group-based quotas as well.
We encourage you to try out these alternatives and as always we welcome your suggestions and feedback.

Related Articles

What is Shell and shell Script

Linux Learners Guide – the Beginners Linux Book

Disk space Monitoring Script

Group Quota Management

Thanks for your wonderful Support and Encouragement

Sahil Suri

I am a system administrator who loves to learn and share my knowledge with the community. I've been working in the IT industry since 2011.

Leave a Reply

Your email address will not be published.