When someone talks about quotas, do you immediately think of the Soviet Union's five-year plans and seldom-met production goals? Well, think again. Quotas are an essential part of a Linux system administrator's duties—even if no other users have access to the system that you're using. Sooner or later, you'll set up a server that exists for no other purpose than to provide network users with additional disk space. To prevent certain users from filling up the disk at the expense of other users' privileges, you need to establish quotas that will give each user a fixed amount of space.
Although quotas are so important, documentation is surprisingly scarce. Most entry- or intermediate-level Linux books, including the Linux Documentation Project's System Administrator's Guide, don’t cover this topic. There's a very good Quota mini-HOWTO, but it's out-of-date. O'Reilly's Essential System Administration provides essential background information, but it's too focused on UNIX utilities. In this Daily Drill Down, I’ll explain how to create, define, and manage user and group disk usage quotas on a Linux system. I’ll also discuss the latest versions of the key underlying utilities and several user-friendly packages that make quota-setting much easier, including Kmuser, a KDE utility that's currently tracking 200 student users in a German school.
Understanding disk usage quotas
System administrators with root privileges on a Linux system can establish quotas for any users on their systems. They also can establish quotas for groups. Whether you’re establishing quotas for users or for groups, you can assign as much or as little disk space to each user as you please; however, you'll probably want to assign the same restrictions to all users. For example, in schools that set up accounts for students in programming classes, each student may have a soft limit of 20 megabytes.
So far, I've talked about quotas as if they pertain exclusively to the amount of disk space that users are allowed to consume (or, to be more specific, to the number of 1-K blocks of disk space that they can use). However, you also can assign a limit on the number of inodes that users can create. (An inode is a named entity that's stored on disk. Usually, inodes are files, but devices and other special files are also inodes.) Every Linux filesystem has a fixed number of inodes, but this number is so large that it's unlikely to be exhausted. Since most system administrators ignore inode restrictions, I’ll focus on setting quotas for disk usage consumption.
For each user and group account, you can create two kinds of quotas:
- A soft quota generates a warning to the user, but it doesn't prevent the user from exceeding the specified limit right away because users often perform operations that temporarily gobble up more disk space than they usually consume. For example, when you downloaded StarOffice, the download files went into a temporary directory (so51inst). From there, you ran the Setup utility. By the time you had finished the installation, you had eaten up a lot of disk space, but the amount decreased when you deleted the so51inst directory. Similarly, you should assign soft limits when you know that users will exceed them occasionally for legitimate reasons.
- A hard quota establishes an absolute limit, beyond which the system won’t permit the user to store additional data. You should make the hard limit higher than the soft limit (as much as 25 to 50 percent higher) so that the user has some room for temporary instances of inordinate disk usage.
When users exceed the soft quota, they receive a message that tells them to bring their disk consumption below the quota level. This message will appear only if the user is working at the console or has a terminal window open within X. Users should run the quota command frequently; when it’s run without options from a user account, this command informs users if they’ve exceeded the soft quota. If they fail to lower their disk consumption level within a grace period that you define (such as seven days), the system will prevent them from writing additional data above the soft limit.
Understanding the disadvantages of disk usage quotas
Although disk usage quotas are often necessary on multiuser systems, they have several drawbacks. To determine how much disk space each user is consuming, the quota software must scan the disk regularly, and this scanning takes time. Users who violate the hard limit (or the soft limit’s grace period) may find that they can’t save data that they've spent time creating. If users can’t save data because they’ve exceeded the quota, they can switch to a file management program, delete files until their consumption is under the quota, and then save their data. Make sure that you teach users how to perform these actions so that they don’t lose data unnecessarily. Also note that Linux quota support is designed for use with the Second Extended Filesystem (ext2) only. You can't enable partitions that contain filesystems other than ext2.
Considering these drawbacks, you may want to consider other alternatives before you implement any quotas. For a system that only two or three users share, you might try an honor system; you can ask users to observe disk usage limits on their own. You'll need to keep an eye on actual disk usage, though. For a system with a dozen or more users, there may be no practical alternative to disk usage quotas.
Enabling quota support in the kernel
To set quotas on your Linux system, you need to make sure that quotas are enabled in your kernel. Typically, Linux distributions enable quota support by default, but you should check to make sure. To do so, follow these steps:
- Open a terminal window and switch to superuser (type su and supply your root password).
- Type cd /usr/scr/linux and press [Enter].
- Type make xconfig and press [Enter]. You'll see the X kernel configuration utility.
- Click Filesystems. You'll see the Filesystems options.
- See if the Quota Support option is enabled. If not, click Y.
- Click Main Menu.
Now, do one of the following:
- If quota support was enabled, click Quit Without Saving.
- If you had to enable quota support, click Save and Exit. You'll need to recompile your kernel, which isn't as difficult as it sounds.
Obtaining the quota utilities
To implement quotas, you'll need the following quota utilities:
- quotacheck creates the binary quota databases (quota.user and quota.group).
- edquota enables the system administrator to define quotas for individual users.
- quotaon and quotaoff launches or terminates the quota daemon.
- quota enables users to check their quota status.
These utilities are provided with most Linux distributions. If you need to obtain the source code, you'll find it and the latest version of the quota utilities here.
Implementing disk usage quotas
You're sold on the quota concept. But how do you implement quotas? Here's an overview of the process:
- Modify /etc/fstab by adding usrquota (for user quotas) or grpquota (for group quotas) to the mount options of the filesystems that will have quotas.
- Run quotacheck -av to create the required binary files (quota.user and quota.group) at the top-level directory of each quota-enabled filesystem.
- Run edquota to create a prototype user quota.
- Run edquota -t to define the default grace period.
- Copy the prototypical quota to other users.
- Run quotaon to enable quotas on the system.
- Modify your system initialization script to run the quota software automatically.
- Check quota statuses periodically with repquota.
Enabling quotas in /etc/fstab
Quotas can be enabled (or disabled) independently for each partition and filesystem defined in /etc/fstab. In most cases, however, you’ll enable quotas only on the partition that contains the users' home directories. If you created a separate partition for /home, enable quotas on this partition. If users' home directories are part of a larger partition, such as the root partition (/), enable quotas on the partition that contains the home directories. To enable quotas on one of the partitions listed in /etc/fstab, switch to the root user or superuser. Then, add usrquota, grpquota, or both to the mount options for this partition. (The mount options are found in the fourth column from the left.)
Creating the quota files
After you’ve added the quota-enabling options (usrquota or grpquota) to /etc/fstab, you need to run the quotacheck utility to create the necessary quota files (quota.user and quota.group). These files are created in the top-level directory of each of the quota-enabled partitions. To create the needed files for all of the partitions on which you’ve enabled quotas, open a terminal window, switch to the superuser (if necessary), and type quotacheck -av. The -a option tells quotacheck to search all of the quota-enabled partitions, while the -v option displays status messages.
Defining a prototype user
Quotas are defined individually for each user. On a system with dozens or even hundreds of users, the task of setting up quotas could become very time-consuming. However, you can cut down on all that work by creating a prototype user. Basically, a prototype user quota contains the quota settings that you want to apply to all or most of the other users on the system. For example, if you've decided to give each user 20 MB of storage space, you should set up a prototype user quota and define this amount of storage space as the default.
Do your homework before you proceed. Let's say that you're setting up a system for a class with 30 students and you have 1.2 GB of disk space to distribute among them. That works out to a maximum of 40 MB per student, so this figure is a good one to use for the hard limit. A good rule of thumb is to begin by creating a soft limit that's 50 percent of the hard limit (20 MB). This figure leaves plenty of room for those occasional instances of heavier-than-usual disk consumption. To create a prototype user, do the following:
- Open a terminal window and switch to the superuser (type su and supply your root password).
- Type edquota and the name of the user whose disk usage quotas will be typical of users on this system. (If your shell can’t find this command, type whereis edquota to locate it.)
Now, you'll see the following lines in the default editor (which will be vi unless you've changed the default editor on your system):
Quotas for user bryan: /dev/hda5:
blocks in use: 1231756, limits (soft = 0, hard = 0)
inodes in use: 6054, limits (soft = 0, hard = 0)
- If you're using vi, type i to enter the insert mode.
- In the line that contains blocks in use, delete the zero, and type the soft limit (in 1-K blocks). To create a soft limit of approximately 20 MB, this line should contain soft = 20000.
- Now, set the hard limit by erasing the zero next to hard =. To create a hard limit of 40 MB, this line should contain hard = 40000.
- Save the file and exit. If you're using vi, press [Esc] to exit the insert mode, type :x, and press [Enter].
Defining the default grace period
To define the default grace period, type edquota -t and press [Enter]. You'll see the default text editor again. This time, you'll see something like the following:
Time units may be: days, hours, minutes, or seconds
Grace period before enforcing soft limits for users:
/dev/hda5: block grace period: 0 days, file grace period: 0 days
You'll see a grace period line for each of the partitions for which quotas have been enabled, such as /dev/hda5 in the above example. If you enabled quotas on more than one partition, you can assign a unique grace period for each of them. In the same way that you edited soft and hard limits in the previous step, use your text editor to change these values to ones that you prefer. A typical grace period is seven days.
Copying the prototypical user
Now that you've created the prototypical user and defined the grace period, you should copy this user's quota settings to other users. To do so, type edquota -p, the name of the prototypical user, and the name of the user whose quotas you want to define. If the prototypical user’s name is Kyung and you want to define Suzanne's quota, here's what the command would look like:
edquota -p kyung suzanne
Repeat this command for all of the users to whom the prototypical restrictions should apply.
Turning on quotas
To switch quota-tracking on, switch to the superuser, if necessary. Now, type quotaon and press [Enter]. If you'd like to disable quotas, run quotaoff. Both of these utilities require root or superuser status.
Running quotas automatically
Once you've implemented quotas, you'll want them to be enabled automatically when you start your system. To do so, just add the following script to the end of /etc/rc.d/rc.sysinit:
#Check for quotas and turn quotas on
if [ -x /usr/sbin/quotacheck ]; then
echo ``Checking quotas...''
if [ -x /usr/sbin/quotaon ]; then
echo ``Turning quotas on...''
To make sure that the quota utilities run on mounted filesystems, this script should be positioned at the end of /etc/rc.sysinit. (Usually, the commands that mount the filesystems are located in the middle of the initialization script.) Note that your initialization script may already contain a commented-out version of the above script. If so, delete the comment marks instead of adding the above script.
Try rebooting your system to make sure that the script modifications work correctly. If the script fails, carefully check your typing. Also, make sure that the referenced utilities (quotacheck and quotaon) are indeed located in the indicated directory (/usr/sbin).
Checking quota status
Once you've enabled quotas, you should run repquota periodically in order to view the status of the quotas that you've enabled. To start repquota, type repquota -a and press [Enter]. You'll see a list of users, their quota settings, and their actual resource usage. You can see at a glance if any of the users have exceeded their soft quotas.
If you set and edit quotas frequently, you may wish to explore more user-friendly utilities than the ones I've just discussed. Here are the best of these utilities:
- Linuxconf is an X-based system administration utility that enables you to set quotas in an easy-to-use, menu-driven environment. A particularly nice feature of Linuxconf is its ability to define defaults for user and group quotas. Once you've set these defaults, you can assign the default quotas to individual users. (You also can create custom quotas for each user, if you don't want to use the defaults.) You can learn more about Linuxconf at Linuxconf.
- For KDE systems, kmuser is a nicely designed user administration tool that includes quota capabilities. In addition to providing easy-to-use tools for user creation and passwords, kmuser enables you to define quota defaults. Then, you can assign the default quotas to individual users, or you can customize each user's quota as you please. Take a look at kmuser.
Please note that both of these utilities require you to enable quotas in the kernel and in /etc/fstab. You also need to run quotacheck to create the quota files and quotaon to start the quota software.
Bryan Pfaffenberger, a UNIX user since 1985, is a University of Virginia professor, an author, and a passionate advocate of Linux and open source software. A Linux Journal columnist, his recent Linux-related books include Linux Clearly Explained (Morgan-Kaufmann) and Mastering Gnome (Sybex; in press). His hobbies include messing around with his home LAN and sailing the southern Chesapeake Bay. He lives in Charlottesville, VA. If you’d like to contact Bryan, send him an e-mail.The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.