The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 Managing User and Group Quotas in Linux

1.0 File Capacity Quotas
        quotacheck : Create Quota Database
        edquota : Edit Quotas
           quota : Display User/Group Quotas
           repquota : Summarize File System Quota
        quotaon : Enable Quotas
           quotaoff : Disable Quotas

ENG⇒中ENG⇒中
  1.0 File Capacity Quotas

The purpose of file capacity quotas (quota) is to limit the maximum number of files or the amount of storage space that a specific user or group can utilize, preventing a particular user from exhausting the available hard disk space.

Linux has the following specifications for file capacity quotas:

Setting up capacity quotas is straightforward, mainly involving the use of commands like quotacheck to create quota databases, edquota to edit quotas, and quotaon to enable them. However, three essential prerequisites must be met beforehand:
  1. Verify Kernel Support
    Modern Linux distributions typically support file capacity quotas (quota). You can check if quota support is enabled in the kernel configuration file "/boot/config-XXX" (where XXX is the kernel name, varying by distribution) as follows:

    # cat /boot/config-$(uname -r) | grep 'QUOTA'← Check if the kernel has quota support
    CONFIG_QUOTA=y
    CONFIG_QUOTACTL=y

    If you see "CONFIG_QUOTA=y", it means quota support is enabled. If not, it may be because quota support wasn't included during a custom kernel upgrade. You can recompile the kernel and include "Quota support" under "File systems ---> [*] Quota support".

  2. Edit "/etc/fstab"
    To enable capacity quotas for users, you need to add "usrquota" to the mount options. For groups, you should use "grpquota". Here's an example where "/home" is a separate partition, and both user and group capacity quotas are enabled:

    # grep 'quota' /etc/fstab
    LABEL=/home /home ext3 defaults,usrquota,grpquota 1 2 ←Enabling user and group capacity quotas for "/home" partition.

  3. Remount /etc/fstab
    After editing the quota options in "/etc/fstab", if the partition was previously unused, you can simply remount it. However, if the partition was already in use, you should remount it with mount -o remount DIR to apply the changes. Alternatively, you can reboot the system. Regardless of the method, ensure that "/etc/mtab" contains "usrquota" or "grpquota" to confirm that the configuration is correct.

    # grep 'quota' /etc/mtab ←Check if "/etc/mtab" contains "usrquota" or "grpquota"
    LABEL=/home /home ext3 defaults,usrquota,grpquota 1 2

    Once you see "usrquota" or "grpquota" in "/etc/mtab" concerning quota mount options, you can proceed to create quota databases using quotacheck. Otherwise, revisit steps 1 to 3 for verification.
quotacheck : Create Quota Database
quotacheck checks for the presence of "usrquota" or "grpquota" mount options in "/etc/mtab" . If these options are found, it generates quota database files, "[a]quota.user", and "[a]quota.group", respectively, at the top level of the filesystem. (Note that for xfs filesystems, file capacity quotas are natively supported, so quotacheck is not required.)

The syntax for quotacheck is as follows:
quotacheck [OPTIONS] [FILESYSTEM] .

Commonly used options include:

Examples:
# quotacheck -g /mnt ←Generates "aquota.group" for the "/mnt" directory
# quotacheck -u /mnt ←Generates "aquota.user" for the "/mnt" directory

Examples:
# quotacheck -gu -a ←Generates either "aquota.group" or "aquota.user" quota databases (based on `/etc/mtab`)
# find / -maxdepth 2 -name 'quota.*' -o -name 'aquota.*' ←Verifies the presence of quota database files
/home/aquota.user
/home/aquota.group

All commands related to quotas, such as quotacheck, repquota or quotaon, share a consistent behavior. When no options are specified, "-u" (user quotas) is the default, and you need to specify "-g" (group quotas) to set them. The -a option scans filesystems with quota options listed in "/etc/mtab". Otherwise, you can specify the filesystem or mount point directly, like quotacheck /dev/sda2 or quotacheck /home.

^ back on top ^



 edquota : Edit Quotas
After generating quota database files with quotacheck, you can use edquota (edit quota) to edit quotas. The "-u" option is used to edit user quotas (the "aquota.user" file must exist), while the "-g" option is used to edit group quotas (the "aquota.group" file must exist). You cannot use both "-u" and "-g" together; you must choose one option. To edit user and group quotas, you need to perform separate actions.

(Using edquota)
# edquota -u aaa ←Edit the quotas for the "aaa" user

Disk quotas for user aaa (gid 500):
Filesystem blocks soft hard inodes soft hard  
/dev/sda3 15368 256000 512000 534 0 0  
 
The partition 
where the
filesystem
is located
(self-generated,
no need for
editing)
The current
no. of blocks
(self-generated, no need for 
editing)

Soft
limits 
on capacity



hard
limit 
on capacity



Current no. 
of inodes
(self-generated,
no need for
editing)


Soft limits
for inodes

Hard limit
on inode


←meaning



1 2 3 4 5 6 7 ←Number of fields

In the example above, entering edquota for editing quotas defaults to using the vi editor. There are a total of 7 fields, each with the following meanings:
  1. filesystem:
    Displays the current partition where quotas are applied, and this field is for informational purposes only; you cannot edit it.
  2. blocks:
    Displays the current partition where quotas are applied, and this field is for informational purposes only; you cannot edit it.
  3. soft:
    Represents the soft limit for disk space usage in KB. The "soft limit" allows exceeding the limit temporarily within a grace period, but exceeding it after the grace period results in an inability to save files.
  4. hard:
    Represents the hard limit for disk space usage in KB. The "hard limit" is an absolute maximum and cannot be exceeded.
  5. inodes:
    Represents the hard limit for disk space usage in KB. The "hard limit" is an absolute maximum and cannot be exceeded.
  6. soft:
    Represents the soft limit for the number of files (inodes).
  7. hard:
    Represents the hard limit for the number of files (inodes).

You can edit the "soft" or "hard" limits by entering values. Entering "0" indicates no quota limit. Fields 1, 2, and 5 are automatically generated and should not be edited; if edited accidentally, they cannot be saved.

After editing the soft and hard quotas, you can also edit the grace period (if necessary). By default, the grace period is set to 7 days. The "-t" option is used to edit the soft limit's grace period for the entire filesystem, and the "-T" option is used to edit individual user or group soft limit grace periods. The grace period units can be seconds, minutes, hours, or days.

Examples:
# edquota -u -t ←Edit the soft limit grace period for the filesystem


Examples:
# edquota -g -T bbb ←Edit the soft limit grace period for the "bbb" group

If you have many user accounts, it can be cumbersome to edit quotas for each one individually. In such cases, you can create a quota prototype account and then copy its settings to other accounts. To copy quotas from a prototype to other accounts, you can use the "-p" option:

Here are some examples provided in the text:

Copy the quota settings from the "morris" user account to the "sam" user account:
# edquota -u -p morris sam

Copy the quota settings from the "rd" group to the "rd1," "rd2," and "hr" groups:
# edquota -g -p rd rd1 rd2 hr

When dealing with a large number of user accounts, a smarter approach is to use filters to collectively set quotas based on the "/etc/passwd" and "/etc/group" files. Here are some examples:

Copy the quota settings from the "morris" account to all user accounts where the home directory is specified in "/etc/passwd":
# edquota -u -p morris $(grep 'home' /etc/passwd | cut -d":" -f1)

Copy the quota settings from the "morris" account to all user accounts with User IDs (UIDs) greater than 499 (assuming user UIDs start from 500):
# edquota -p morris $(awk -F":" '$3 > 499 {print $1}' /etc/passwd)


Copy the quota settings from the "rd" group to all groups with Group IDs (GIDs) greater than 499 (assuming group GIDs start from 500):
# dquota -g -p rd $(awk -F":" '$3 > 499 {print $1}' /etc/group)

These examples demonstrate how to efficiently set or copy quotas for multiple user accounts or groups based on specific criteria.


quotaon : Enable Quotas
After configuring quotas using edquota , the final step is to enable them using quotaon. The syntax is quotaon [-u|g][-fpv] -a|FS.

The "-p" option displays whether user or group quotas are enabled on the filesystem.

Example:
# quotaon -p /home ←← Check if quotas are enabled within "/home"
group quota on /home(dev/sda2) if off
user quota on /home (dev/sda2) if off

he "-v" option displays when quotas are enabled or disabled.

Example:
# quotaon -g -a ←Enable group quotas (no output because `-v` option is not used)
# quotaon -uv -a ←Enable user quotas and display the status
/dev/sda2 [/home]: user quotas turned on

The "-f" option is used to disable quotas.

Example:
# quotaon -ufv /home ←關閉〝/home〞使用者的配額
/dev/sda2 [/home]: user quotas turned off

quotaoff : Disable Quotas
The quotaoff command is used to disable quotas, and it is equivalent to quotaon -f. The usage is the same as that of quotaon .

Example:
# quotaoff -uv -a ←Disable user quotas (based on "/etc/mtab")
/dev/sda2 [/home]: user quotas turned off

These commands allow you to manage and enable/disable disk quotas for users and groups on a Linux system.

^ back on top ^