January 11, 2011

Step by Step on Using Per-User Quotas with Linux


The price of hardware has reached a point where extra drives (or single, massive drives) is not an issue for desktop users. But when you have multiple users using a single machine, keeping their usage down is imperative. The last thing you want is to find yourself with a full partition. One way to guard against this is to enable per-user quotas.

What's so bad about filling up a disk? It keeps the system from saving data to the affected partition, and can cause serious performance degradation.

But how can you avoid this? There are a few ways. One way is to separate the users' home partitions and have each user's $home reside on a separate drive all together. But that is cost prohibitive on a system with more than a handful of users. The more sane way to tackle the problem is with disk quotas. Disk quotas allow you to define just how much space each user is given on a system. It's more cost effective and it keeps your users from filing their home directories up with files they do not need to keep around.

Unfortunately Linux does not have a handy GUI tool for this task. There are a number of steps that have to be undertaken and you will have to edit some fairly critical files (such as /etc/fstab). So, before you continue on, I want to make sure you do so with caution. Back up any file you edit (just in case) and make sure you edit carefully. You do not want to render your system unbootable.

For the purposes of this tutorial, I'm using CentOS 5. This shouldn't vary much on other distributions.


The first thing you must do is install a single tool for the enabling of quotas. That tool is quota. This can be installed quickly and easily with the following steps:

  1. Open up a terminal window.
  2. su to the root user.
  3. Issue the command yum install quota .
  4. Accept any dependencies that might be necessary.
  5. Allow the install to complete.

You are now ready to begin the configuration process. The commands below require root user privileges. So all commands will be done once you have successfully su'd to the root user.

Configuring for Disk Quotas

The first step is to edit your /etc/fstab file so that your system knows what to apply quotas to. I am going to assume you have the entire /home partition on its own drive. The fstab line for this drive, before editing, looks like:

/dev/VolGroup00/LogVol02    /home     ext3     defaults     1 2

Since we are only going to assign quotas to individual users, we are going to add to this the option userquota. So the updated line will look like:

/dev/VolGroup00/LogVol02     /home     ext3     defaults, usrquota     1 2

After you have saved /etc/fstab it needs to be remounted so the changes will take effect. Before you remount this file system you must make sure no one is using it. So have all users log off before you issue the command. To remount /home issue the command:

mount -o remount /home

Create the Database Files

Although your system now knows that quotas are enabled, it has no idea what to do with that feature. Before quotas can actually be enforced, we have to use the quotacheck command will build a table of the current disk usage of the file systems. To create the tables we issue the quotacheck command with the -c option and the option that instructs quotacheck if we are enabling quotas for groups, users, or both. So, for enabling quotas for users only, our command looks like:

quotacheck -cu /home

Now we run the command again, replacing the -c option with the -av options:

  • -a — Check all locally mounted, quota-enabled partitions.
  • -v — Use verbose output.

Our new command looks like:

quotacheck -avu

Now the database has the necessary data it needs to use quotas. We're not done yet, though. We still have to configure a per-user quota.

Per-User Quota

As you might expect, you can configure different quotas for different users. This comes in handy when you have particular users that have different duties requiring larger quotas (Say, for example, a graphics designer who works with larger files). The primary command to edit quotas is edquota. The use of the command is simple. Let's say you want to edit a quota for the user stephanie. The command would be:

edquota stephanie

When you issue this command you will see something similar to:

Disk quotas for user stephanie (uid 507):  
Filesystem                blocks     soft     hard    inodes   soft   hard  
/dev/VolGroup00/LogVol02  440436        0        0     41      0      0

When you issue the edquota command you will be in your default text editor. What you need to do is edit the soft and hard limits for the user. But what are the hard and soft limits?

Hard limits are an absolute limit. When you set this limit a user absolutely can not go beyond that limit. Soft limits, on the other hand, allows the user the exceed that limit with a grace period.

Let's give the user stephanie a 5GB hard limit. To do this we replace the 0 under hard with 10485760 (GB to blocks conversion). Make that change and save the file.  Now issue the command:

quota stephanie

to verify the quota has been set in place.

If you would like to put into place a soft limit that precedes the hard limit you would want to set that limit so that it is under the hard limit. Let's give user stephanie a 4GB soft limit to go along with the 5GB hard limit. For this you would replace the 0 associated with soft limit with 8388608. With that file edited and saved, you now have to set the grace period for the soft limit. To do this issue the command:

edquota -t

When you run this command, you will see something similar to:

Filesystem                  Block grace period     Inode grace period
/dev/VolGroup00/LogVol02         7days                  7days

Change the number of days under Block and Inode to suit your needs. Make sure the format is exactly as you see it above (no space in 7days). Note that you can set up this grace period for days, hours, minutes, or seconds.

Disk Quota Reports

Every once in a while you will want to see a report on quota usage. There is a simple to use command for this:

repquota -a

This command will give you all of the information you need to know where each user is with regards to their set disk quotas.

Wrap It Up

Disk quotas are a simple means to control how your users use their allotted hard disk space. By keeping this in check you can be sure that no one user is going to consume so much hard disk space that your system's performance will be compromised.