Implementing and Managing Disk Quotas in Linux (Step by Step Guide)

In this lesson we’re going to talk about Disk Quotas.

Introduction

Now disk quotas are a very valuable management tool when you’re administering a multi-user Linux system.

Here’s the problem because Linux is a true multi-user operating system.

It is possible for one or two users on that system to completely hog up all the disk space available on the file system.

This is especially a problem today where it’s not uncommon for a user to download very large music files from the internet and even larger movie files.

Now you can keep these users from consuming more than their fair share of the disk space on the system by implementing Disk Quotas.

And these Quotas establish space limitations to specify how much space a particular user is allowed to use.

For example, you can specify with these quotas that a user is only allowed to use a certain amount of disk space or you can limit the number of files that they’re allowed to create by setting a Quota on the number of Inode they’re allowed to consume.

Now Quotas can be configured to either temporarily allow users to exceed limits or you may specify that they’re not allowed to exceed those limits at all.

Installing quota Package

In order to implement quotas on your Linux file system you first need to install the quota package on your system.

Some distributions will install this package as part of the base installation.

Right now I am using the CentOS 8 operating system and it has a Quota package already installed.

# rpm -qa | grep quota
quota-4.04-10.el8.x86_64

My experience has been however that most distributions do not and you’ll have to install it separately after the system’s all been installed and set up.

To see if quota has already been installed on your system you can use the appropriate package management utility for your distribution to see if the quota package has been installed.

And if it hasn’t been installed then you need to use yum or apt-get to go out and get it and install it.

If you are on a Debian based Linux machine and don’t have quota installed then you can install that with the help of the following command.

# sudo apt-get update

# sudo apt-get install quota

and If you are on an RPM based Linux machine like Redhat or CentOS then you can install this with the help of the following command.

#  yum -y install quota

Now once that package has been installed there are several tasks you need to complete in order to establish Quotas.

Let’s take a look at what they are.

Enabling the Quotas on a File System

The first thing we need to do is go in to our /etc/fstab configuration file on the system where we want to establish quotas and open it up in a text editor and add these two parameters to our mount options usrquota and grpquota.

Suggested Read: How to Create Disk Partitions (MBR) in Linux

Here I am enabling the quota on the /dev/sdc1 partition.

# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sun Jan 24 10:20:39 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b119d862-07b7-4842-a6ea-cd2da64f8eae /                       xfs     defaults        0 0
UUID=e9f23f2d-118e-4a5e-94ab-b08afb78a3be /boot                   ext4    defaults        1 2
UUID=d3e3cc9a-6c24-4d58-9d33-4aeaa2eef5d1 swap                    swap    defaults        0 0
/dev/sdc1				  /mydata		  ext4	  defaults,usrquota,grpquota	0 0

Remounting the File Systems

Now before these will take effect the /etc/fstab file has to be reread, probably the easiest way to do this is to simply just restart the system.

You can run the following command to restart the system.

# shutdown -r

Or you can remount the file system using following command.

# mount -o remount /dev/sdc1

Suggested Read:

Enable quota on a file system (Temporary Method)

If you want to temporarily enable quota on a file system, type the following command.

# mount -o remount,usrquota,grpquota /mydata/

Creating the Quota Database Files

Then after the system’s been rebooted you need to create your quota files.

You do this using the command that you see here.

# quotacheck -amvug

Now the quotacheck command will scan the file system and establish what the current disk usage looks like and it will also create our quota files.

Let’s try to understand all the options that we are using with the quotacheck command.

  • -a – Check all File Systems
  • -m – Check all mounted file system
  • -v – Operate in Verbose mode
  • -u – Check User Files
  • -g – Check Group Files

When the quotacheck command is complete, you will see two files created at the mount point of the device in your file system that is aquota.group and aquota.user.

Here I have mounted the /dev/sdc1 partition in the /mydata directory and as you can see the quota files have been created.

# ls /mydata/
aquota.group  aquota.user  lost+found

And this is important because if these two files don’t exist then we can’t create quotas on that file system.

Enabling quotas

Now if those two files in place, the next thing we need to do is enable quotas on the file system.

We do that using the quotaon command with the -a and -v options.

# quotaon -av
/dev/sdc1 [/mydata]: group quotas turned on
/dev/sdc1 [/mydata]: user quotas turned on

Reporting on Disk Quotas

Once that command has been run you can then view the current disk space used by all the users on your system using repquota command with the -a and -v options.

When you run this command, a report is displayed on the screen showing how much space each user is consuming and you can see that right here.

# repquota -av
*** Report for user quotas on device /dev/sdc1
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      20       0       0              2     0     0       
helpdesk  --    2048       0       0              2     0     0       
linuxadmin --    4096       0       0              4     0     0       

Statistics:
Total blocks: 7
Data blocks: 1
Entries: 3
Used average: 3.000000

As you can see, we have the root user, we have the helpdesk user and a user named linuxadmin.

In this report, you can see such a symbol(--) in front of every user.

If the limit of one of the blocks and inodes is exceeded, then here (--) you will get a + Indication.

Out of two Hyphen’s (--), First - represents the block limit, and the second - represents the inode limit.

This report is also showing how much disk space they have used here and how many inodes each user has used.

Now notice over here we have columns named soft and hard.

These are the quotas that have been set for each user.

Now you’ll notice here that all the quotas are set to 0 which means there aren’t any quotas configured for each user we need to change that.

Assigning Disk Quotas per User and Group

You create this quota for user using the commands shown here.

Assigning Quotas per User

We enter edquota -u followed by the username of the user that we want to create a quota for.

In this case I have decided to create a disk quota for the helpdesk user.

# edquota -u helpdesk

Essentially what this will do is open up the vi editor and load the quota file for the user specified.

Disk quotas for user helpdesk (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdc1                      2048          0          0          2        0        0

What you need to do is then specify what quotas you want to set.

We have our blocks quota and we have our inode quotas that we can set.

Now underneath blocks it tells us how many have currently been consumed and inode tells us how many Inode have been consumed on this file system for this particular user.

So don’t be too worried about those parameters.

What we do want to be concerned with is the soft and hard parameters or inodes and for blocks.

You can go ahead and replace these zeros(0) with a number, and you can set a soft and hard quota for blocks and a soft and hard quota for inodes.

Now be aware that soft quotas can be temporarily exceeded for a period of time but hard quotas on the other hand cannot be exceeded at all.

So when we set a block quota we are specifying how many blocks on the disk the user is allowed to consume.

And if you do the math you can figure out how much space equates to how many blocks.

The inode quota on the other hand specifies how many files the user can own on that filesystem.

So let’s set the quota for the helpdesk.

Disk quotas for user helpdesk (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdc1                      2048       10000      15000         2      800     1000

After making the necessary changes, press the ESCAPE button on your keyboard then type :wq! and press ENTER button to save your changes.

Confirming Quotas configuration

To check whether the quota is configured, you can run the following command.

# quota helpdesk
Disk quotas for user helpdesk (uid 1000): 
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sdc1    2048   10000   15000               2     800    1000       

Copy Quotas from one User to other User

Now before we go on be aware that if you have a lot of users on the system then this process right here can be somewhat time consuming.

Going in and editing quotas for each and every individual user.

If the users on your system all have very similar quotas that you want to set, in other words the the block and the inode quotas that you want to set are pretty much the same across multiple users.

You can go through this process here using the edquota command to setup a quota for one user on the system and then you can use the edquota -p command to then say please copy the quotas that are set for this user and copy them over to this other user.

Step #1 Setup a quota for one user

Here I am setting quota for helpdesk user.

# edquota -u helpdesk
isk quotas for user helpdesk (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdc1                      2048       10000      15000         2      800     1000

Step #2 Copy the quotas from one user to other user

Syntax:

edquota -p source_user destination_user

So let’s apply the quota we set for the helpdesk user over linuxadmin user.

# edquota -p helpdesk linuxadmin

And as you can see the quotes have been applied and if you notice, the quota settings of both users are the same.

Disk quotas for user linuxadmin (uid 1005):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdc1                      4096       10000      15000         4      800     1000

So, basically you only have to edit the file once and then copy the quotas that are set to all the other users. It’s a lot faster that way.

Assigning Quotas per Group

Another option for getting around this issue is to establish quotas for groups instead of individual users.

So, you can put all the users that have a similar quota requirement in to the same group.

And then you can use this command edquota -g followed by the group name in order to establish the quota for the group which will then be automatically applied to all members of that group.

Here in this example I’m setting quota for a group called developer.

# edquota -g developer

When you do, you see basically the same interface that you see here you just set the quotas for a group name instead of a user name.

Disk quotas for group developer (gid 1002):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdc1                         0       10000      15000       0        800     1000

Managing the Grace Period

Now before we end, we need to talk about managing the grace period.

Now the grace period is the amount of time that the end user is allowed to exceed a soft quota limit that you set.

The default is to allow users to exceed a soft quota for a maximum of 7 days.

If you don’t like this grace period, you can change it.

This is done by running the following command.

# edquota -t

When you run this command, you see this interface right here.

For the given file system, you can specify what the block grace period is and what the inode grace period is and is again you can see it’s set to 7 days by default.

Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
  Filesystem             Block grace period     Inode grace period
  /dev/sdc1                     7days                  7days

If you want to be shorter or longer you can enter that value here.

Let’s change the grace period to 10 days.

Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
  Filesystem             Block grace period     Inode grace period
  /dev/sdc1                     10days                  10days

Once you’re done you can then check your new quotas by running the same command again that we ran earlier.

Note: Following report will display the disk usage report for all (option -a) quota enabled file systems

# repquota -av
*** Report for user quotas on device /dev/sdc1
Block grace time: 10days; Inode grace time: 10days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      20       0       0              2     0     0       
helpdesk  --    2048   10000   15000              2   800  1000       
linuxadmin --    4096   10000   15000              4   800  1000       

Statistics:
Total blocks: 7
Data blocks: 1
Entries: 3
Used average: 3.000000

This time you’ll notice that some thing is different in the output of the command.

And notice that from my helpdesk user now have a soft and hard limit set for my block usage, I also have a soft and hard limit set for my inode usage effectively.

helpdesk now has quota is set on the /dev/sdc1.

So, at this point my system is now protected from disk space hogs.

To check the quota report of a particular file system, you can run the following command.

Here I am checking the quota report of the /mydata file system.

# repquota /mydata/
*** Report for user quotas on device /dev/sdc1
Block grace time: 10days; Inode grace time: 10days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --      20       0       0              2     0     0       
helpdesk  --    2048   10000   15000              2   800  1000       
linuxadmin --    4096   10000   15000              4   800  1000   

Enabling and Disabling Quotas

If you want to turn the quotas on or off, you can do so.

Type the following command to disable quota for all users and groups.

# quotaoff -vaug
/dev/sdc1 [/mydata]: group quotas turned off
/dev/sdc1 [/mydata]: user quotas turned off

Run the following command to disable quotas only for users.

# quotaoff -vau
/dev/sdc1 [/mydata]: user quotas turned off

You can use the following command to disable quotas only for groups.

# quotaoff -vag
/dev/sdc1 [/mydata]: group quotas turned off

Following command will enable quota for all users and groups.

# quotaon -vaug
/dev/sdc1 [/mydata]: group quotas turned on
/dev/sdc1 [/mydata]: user quotas turned on

To enable quotas for a specific file system, such as /mydata, use the following command.

# quotaon -vug /mydata

Get more information on disk quotas.

Conclusion

I hope that now you have a good understanding of How to Implement and Manage Disk Quotas in Linux.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

1 thought on “Implementing and Managing Disk Quotas in Linux (Step by Step Guide)”

  1. Hello Balamukunda, very usefull article. One question, how can I add a specific amount of space ej 500MB to all my users in a bulk manner? In their current quota have different sizes assigned and I don´t want to edit one by one. Thanks in advance, Gustavo

    Reply

Leave a Comment