Limit User’s Disk Space (Setting Quotas)

General Information

This guide details how to set up user accounts with disk allocation. Let’s say you want to limit each user to a specific size disk usage — 100 megs each. You will need to set “user quotas.” Here we go!!

Requirements

  1. Root Access or Sudo Right

Configuration

First step is to make sure that quotas are configured into the Kernel. If you are not familiar with the process, learn to compile your kernel.

Enable the following line in the kernel config:

options QUOTA

Next you will need to enable disk quotas in /etc/rc.conf.

# echo 'enable_quotas="YES"' >> /etc/rc.conf

Note: For finer control over your quota startup, there is an additional configuration variable available. Normally on bootup, the quota integrity of each file system is checked by the quotacheck program. The quotacheck facility ensures that the data in the quota database properly reflects the data on the file system. This is a very time consuming process that will significantly affect the time your system takes to boot. If you would like to skip this step, a variable is made available for the purpose:

# echo 'check_quotas="NO"' >> /etc/rc.conf

If you are running FreeBSD prior to 3.2-RELEASE, the configuration is simpler, and consists of only one variable. Set the following in your /etc/rc.conf:

# echo 'check_quotas="YES"' >> /etc/rc.conf

Finally you will need to edit /etc/fstab to enable disk quotas on a per-file system basis. This is where you can either enable user or group quotas or both for all of your file systems. To enable per-user quotas on a file system, add the userquota option to the options field in the /etc/fstab entry for the file system you want to to enable quotas on. For example:

/dev/da1s2g /home ufs rw,userquota 1 2

Similarly, to enable group quotas, use the groupquota option instead of the userquota keyword. To enable both user and group quotas, change the entry as follows:

/dev/da1s2g /home ufs rw,userquota,groupquota 1 2

By default, the quota files are stored in the root directory of the file system with the names quota.user and quota.group for user and group quotas respectively. See the fstab man page for more information. Even though that man page says that you can specify an alternate location for the quota files, this is not recommended because the various quota utilities do not seem to handle this properly. At this point you should reboot your system with your new kernel. /etc/rc will automatically run the appropriate commands to create the initial quota files for all of the quotas you enabled in /etc/fstab, so there is no need to manually create any zero length quota files. In the normal course of operations you should not be required to run the quotacheck, quotaon, or quotaoff commands manually. However, you may want to read their man pages just to be familiar with their operation.

Now ensure quotas are enabled

# quota -v

You should see a one line summary of disk usage and current quota limits for each file system that quotas are enabled on.

You are now ready to start assigning quota limits with the edquota command. There are two types of limits: hard and soft limits.

A hard limit may not be exceeded. Once a user reaches their hard limit they may not make any further allocations on the file system in question. For example, if the user has a hard limit of 500 blocks on a file system and is currently using 490 blocks, the user can only allocate an additional 10 blocks. Attempting to allocate an additional 11 blocks will fail.

Soft limits on the other hand can be exceeded for a limited amount of time. This period of time is known as the grace period, which is one week by default. If a user stays over his or her soft limit longer than their grace period, the soft limit will turn into a hard limit and no further allocations will be allowed. When the user drops back below the soft limit, the grace period will be reset.

The following is an example of what you might see when you run the edquota command. When the edquota command is invoked, you are placed into the editor specified by the EDITOR environment variable, or in the vi editor if the EDITOR variable is not set, to allow you to edit the quota limits.

# edquota -u test

Quotas for user test:
/usr: blocks in use: 65, limits (soft = 50, hard = 75)
inodes in use: 7, limits (soft = 50, hard = 60)
/usr/var: blocks in use: 0, limits (soft = 50, hard = 75)
inodes in use: 0, limits (soft = 50, hard = 60)

The new quota limits will be in place when you exit the editor! See the edquota manpages for more detailed information.

Speak Your Mind

*