Difference between revisions of "Disk quota"

From ArchWiki
Jump to: navigation, search
(Enable quota for user/group: Service doesn't exists anymore and it's pretty straightforward how to disable)
 
(68 intermediate revisions by 15 users not shown)
Line 1: Line 1:
 
[[Category:Security]]
 
[[Category:Security]]
 
[[Category:File systems]]
 
[[Category:File systems]]
 +
[[ja:ディスククォータ]]
 
From [[Wikipedia:Disk quota|Wikipedia]]:
 
From [[Wikipedia:Disk quota|Wikipedia]]:
 
:"''A '''disk quota''' is a limit set by a system administrator that restricts certain aspects of file system usage on modern operating systems. The function of setting quotas to disks is to allocate limited disk-space in a reasonable way.''"
 
:"''A '''disk quota''' is a limit set by a system administrator that restricts certain aspects of file system usage on modern operating systems. The function of setting quotas to disks is to allocate limited disk-space in a reasonable way.''"
 
This article covers the installation and setup of disk quota.
 
This article covers the installation and setup of disk quota.
  
==Installing==
+
== Installation ==
Disk quota only requires one package:
 
# pacman -S quota-tools
 
  
==Enabling==
+
[[Install]] the {{pkg|quota-tools}} package.
''For journaled quota, see the notes in [[#Journaled quota]].''
 
  
1. First, edit {{ic|/etc/fstab}} to enable the quota mount option(s) on selected file systems:
+
== Configuration ==
  /dev/sda1 /home ext4 defaults 1 1
+
=== Setup the filesystem ===
:edit it as follows;
 
  /dev/sda1 /home ext4 defaults''',usrquota''' 1 1
 
:or aditionally enable the group quota mount option;
 
  /dev/sda1 /home ext4 defaults''',usrquota,grpquota''' 1 1
 
  
2. Create the quota files in the file system:
+
Edit [[fstab]] to enable the quota mount option(s) on selected file systems, e.g.:
  # touch /home/aquota.user
+
 
  # touch /home/aquota.group     # For group quota
+
{{hc|/etc/fstab|/dev/sda3 /home ext4 defaults''',usrquota''' 0 2}}
 +
 
 +
To additionally enable the group quota mount option:
 +
 
 +
{{hc|/etc/fstab|/dev/sda3 /home ext4 defaults''',usrquota,grpquota''' 0 2}}
 +
 
 +
If supported by the [[kernel]] and [[File_systems#Journaling|file system]] it is recommended to use journaled quota instead:
 +
 
 +
{{hc|/etc/fstab|2=/dev/sda3 /home ext4 defaults''',usrjquota=aquota.user,jqfmt=vfsv1''' 0 2}}
 +
 
 +
Append {{ic|1=grpjquota=aquota.group}} to enable group quota.
 +
 
 +
Remount the partition to apply the change:
  
2. The next step is to remount:
 
 
   # mount -vo remount /home
 
   # mount -vo remount /home
  
4. Create the quota index:
+
=== Create quota index ===
  # quotacheck -vgum /home
 
:or for all partitions with the quota mount options in {{ic|/etc/mtab}};
 
  # quotacheck -vguma
 
{{tip|If you end up with the output "[...]Quotafile $FILE was probably truncated. Cannot save quota settings..." you can try removing the previously created files aquota*}}
 
{{tip|In Addition you can try to use "-F vfsold" and "-F vfsv0" afterwards <b>NOTE:</b> As of 3.1.6-1, Arch does not support "vfsv1"}}
 
  
5. Finally, enable quotas:
+
To create the quota index for {{ic|/home}}:
  # quotaon -av
 
  
{{tip|to automatically enable quota on boot, add {{ic|quotaon -a >/dev/null 2>&1}} to {{ic|/etc/rc.local}}.}}
+
  # quotacheck -vucm /home
 +
 
 +
Append the {{ic|-g}} parameter to also create a group index.
 +
 
 +
To enable disk quotas for the desired file system:
 +
 
 +
# quotaon -v /home
 +
 
 +
To disable disk quotas for the file system:
 +
 
 +
# quotaoff -v /home
 +
 
 +
== Usage ==
 +
 
 +
=== Enable quota for user/group ===
 +
 
 +
{{Tip|
 +
*To find out how many 1 kilobyte blocks are there for a partition use {{ic|$ df}}.
 +
*You may use a online bytes converter to calculate the correct amount of blocks [http://whatsabyte.com/P1/byteconverter.htm].
 +
*The command {{ic|# setquota}} may be used as an alternative of {{ic|# edquota}} [https://gehrcke.de/2013/05/setting-up-quotas-on-a-local-linux-file-system/].}}
 +
 
 +
{{Note|Block size is statically set to 1k regardless of filesystem block size [http://stackoverflow.com/questions/2506288/detect-block-size-for-quota-in-linux/2506311#2506311].}}
 +
 
 +
Quotas are configured using {{ic|# edquota}} that will be opened in the default configured text editor:
 +
 
 +
{{hc|# edquota ''user''|
 +
Disk quotas for user ''user'' (uid 1000):
 +
  Filesystem                  blocks      soft      hard    inodes    soft    hard
 +
  /dev/sda3                        24          0          0          6        0        0
 +
}}
  
===Journaled quota===
+
;blocks: Indicates number of 1k blocks currently used by the user/group.
Enabling journaling for disk quota adds the same benefits journaled file systems do for forced shutdowns, meaning that data is less likely to become corrupt.
+
;soft: Indicates max number of blocks for the user/group before a warning is issued and grace period countdown begins. If set to "0" (zero) then no limit is enforced.
 +
;hard: Indicates max number of blocks for the user/group can use. If maximum amount has been reached, no further disk space can be used. If set to "0" (zero) then no limit is enforced.
 +
;inodes: Indicates the current inodes amount used by the user/group.
 +
;soft: Indicates the soft inode limit for the user/group.
 +
;hard: Indicates the hard inode limit for the user/group.
  
Setting up journaled quota is the same as above, except for the mount options:
+
Consider the following configuration for ''ftpuser1'':
/dev/sda1 /home ext4 defaults''',usrjquota=aquota.user,jqfmt=vfsv0''' 1 1
 
or aditionally enable the group quota mount option;
 
/dev/sda1 /home ext4 defaults''',usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0''' 1 1
 
  
==Configuring==
+
{{hc|# edquota ''ftpuser1''|
Replace {{ic|$USER}} as appropriate and edit the quota as root:
+
Disk quotas for user ftpuser1 (uid ''1000''):
{{hc|$ edquota ''$USER''|
 
Disk quotas for user '''$USER''' (uid 1000):
 
 
   Filesystem                  blocks      soft      hard    inodes    soft    hard
 
   Filesystem                  blocks      soft      hard    inodes    soft    hard
   /dev/sda1                      1944         0          0        120       0        0
+
   /dev/sda3                        24    1000000    1048576         6       0        0
 +
}}
 +
 
 +
In this case if ''ftpuser1'' uses over 976MB of space a warning will be issued. If the hard limit of 1TB has been reached the user will be unable to write any more data.
 +
 
 +
See [[#Specify a grace period]] to give users a specific amount of time to reduce storage usage when they hit their soft limit.
 +
 
 +
{{Warning|The {{ic|hard}} limit applies to all files written by and for the respective user/group, including temporary files by started applications, which may crash at this point.}}
 +
 
 +
=== Specify a grace period ===
 +
To give current users some time to reduce their file usage, a grace period can be configured. This specifies the allowed time a user/group can exceed their soft limit and while under their hard limit:
 +
 
 +
{{hc|# edquota -t|
 +
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/sda3              7days                  7days
 
}}
 
}}
{{note|to edit group quotas, use {{ic|edquota -g $GROUP}}.}}
 
;blocks: Number of 1k blocks currently used by {{ic|$USER}}
 
;inodes: Number of entries by {{ic|$USER}} in directory file
 
;soft: Max number of blocks/inodes {{ic|$USER}} may have on partition before warning is issued and grace period countdown begins. If set to "0" (zero) then no limit is enforced.
 
;hard: Max number of blocks/inodes {{ic|$USER}} may have on partition. If set to "0" (zero) then no limit is enforced.
 
  
Example configuration:
+
The grace period can be set in seconds, minutes, hours, days, weeks or months.
Disk quotas for user testuser (uid 1000):
 
Filesystem      blocks      soft      hard    inodes    soft  hard     
 
/dev/sda1      695879      10000      15000    6741        0      0
 
  
The {{ic|soft}} limit means that once ''testuser'' uses over 10MB of space a warning email gets ensued, and after a period of time the soft limit gets enforced.
+
=== Reports ===
  
The {{ic|hard}} limit is stricter, so to speak; a user cannot go over this limit.
+
Shows all configured quotas:
 +
 +
# repquota -a
  
Next configure the {{ic|soft}} limit grace period:
+
Shows quotas on a specific partition:
# edquota -t
 
  
==Managing==
+
# repquota ''/home''
''Checking for quota limits and advanced operations''
 
  
===Basics===
+
Show quotas that apply to a [[user]]/[[group]]:
Use this command to check for quotas on a specific partition:
 
# repquota /home
 
  
Use this command to check for all quotas that apply to a user:
+
  # quota -u ''user''
  # quota -u $USER
 
for groups;
 
# quota -g $GROUP
 
  
===Copying quota settings===
+
  # quota -g ''group''
To copy quota from one user or group to the other, use this command:
 
  # edquota -p user1 user2
 
User1 is the user you copy from, user2 is the user you copy quota to. Of course you can replace user with group, when necessary.
 
  
====Multiple users====
+
=== Copy quota settings ===
The idea is to make a temporary user acount, modify the quota settings for that user, and then copy the generated quota files for all users to use. After setting quota settings for ''quotauser'', copy the settings:
 
# edquota -p '''quotauser''' `awk -F: '$3 > 999 {print $1}' /etc/passwd`
 
This applies the settings to users with a UID equal to or greater than 1000.
 
  
===Other commands===
+
==== To one or several users ====
There are several useful commands:
+
 
  repquota -a     # Shows the status on disk usage
+
To copy quota settings from {{ic|''user1''}} to {{ic|''user2''}}:
  warnquota       # Can be used to warn the users about their quota
+
 
setquota        # Non-interactive quota setting--useful for scripting
+
# edquota -p ''user1'' ''user2''
 +
 
 +
To copy quota settings to several other users, append {{ic|''user3''}} {{ic|''user4''}} ...
 +
 
 +
==== To groups ====
 +
 
 +
To copy quota settings from {{ic|''group1''}} to {{ic|''group2''}}:
 +
 
 +
  # edquota -g -p ''group1'' ''group2''
 +
 
 +
==== To all users ====
 +
 
 +
The idea is to modify the quota settings for one user and copy the setting to all other users. Set the quota for {{ic|''user1''}} and apply the quota to users with a UID greater than 999:
 +
 
 +
# edquota -p ''user1'' $(awk -F: '$3 > 999 {print $1}' /etc/passwd)
 +
 
 +
== Tips and tricks ==
 +
=== Quota warnings ===
 +
The command {{ic|warnquota}} can be used to warn the users about their quota. Configuration is available in {{ic|/etc/warnquota.conf}}.
 +
 
 +
=== Stats ===
 +
The command {{ic|quotastats}} can be used to give more information about the current quota usage:
  
Lasty, {{ic|quotastats}} is used to give thorough information about the quota system:
 
 
{{hc|$ quotastats|
 
{{hc|$ quotastats|
 
Number of dquot lookups: 101289
 
Number of dquot lookups: 101289
Line 113: Line 157:
 
}}
 
}}
  
==More resources==
+
== See also ==
 +
 
 
* http://tldp.org/HOWTO/Quota.html
 
* http://tldp.org/HOWTO/Quota.html
 
* http://www.sf.net/projects/linuxquota/
 
* http://www.sf.net/projects/linuxquota/
 
* http://www.yolinux.com/TUTORIALS/LinuxTutorialQuotas.html
 
* http://www.yolinux.com/TUTORIALS/LinuxTutorialQuotas.html
* http://www.redhat.com/docs/manuals/linux/RHL-8.0-Manual/admin-primer/s1-storage-quotas.html
+
* [https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Storage_Administration_Guide/ch-disk-quotas.html RHEL7: Disk Quotas]
 +
* https://www.digitalocean.com/community/tutorials/how-to-enable-user-and-group-quotas

Latest revision as of 18:22, 18 November 2017

From Wikipedia:

"A disk quota is a limit set by a system administrator that restricts certain aspects of file system usage on modern operating systems. The function of setting quotas to disks is to allocate limited disk-space in a reasonable way."

This article covers the installation and setup of disk quota.

Installation

Install the quota-tools package.

Configuration

Setup the filesystem

Edit fstab to enable the quota mount option(s) on selected file systems, e.g.:

/etc/fstab
/dev/sda3 /home ext4 defaults,usrquota 0 2

To additionally enable the group quota mount option:

/etc/fstab
/dev/sda3 /home ext4 defaults,usrquota,grpquota 0 2

If supported by the kernel and file system it is recommended to use journaled quota instead:

/etc/fstab
/dev/sda3 /home ext4 defaults,usrjquota=aquota.user,jqfmt=vfsv1 0 2

Append grpjquota=aquota.group to enable group quota.

Remount the partition to apply the change:

 # mount -vo remount /home

Create quota index

To create the quota index for /home:

 # quotacheck -vucm /home

Append the -g parameter to also create a group index.

To enable disk quotas for the desired file system:

# quotaon -v /home

To disable disk quotas for the file system:

# quotaoff -v /home

Usage

Enable quota for user/group

Tip:
  • To find out how many 1 kilobyte blocks are there for a partition use $ df.
  • You may use a online bytes converter to calculate the correct amount of blocks [1].
  • The command # setquota may be used as an alternative of # edquota [2].
Note: Block size is statically set to 1k regardless of filesystem block size [3].

Quotas are configured using # edquota that will be opened in the default configured text editor:

# edquota user
Disk quotas for user user (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda3                        24          0          0          6        0        0
blocks
Indicates number of 1k blocks currently used by the user/group.
soft
Indicates max number of blocks for the user/group before a warning is issued and grace period countdown begins. If set to "0" (zero) then no limit is enforced.
hard
Indicates max number of blocks for the user/group can use. If maximum amount has been reached, no further disk space can be used. If set to "0" (zero) then no limit is enforced.
inodes
Indicates the current inodes amount used by the user/group.
soft
Indicates the soft inode limit for the user/group.
hard
Indicates the hard inode limit for the user/group.

Consider the following configuration for ftpuser1:

# edquota ftpuser1
Disk quotas for user ftpuser1 (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sda3                        24    1000000    1048576          6        0        0

In this case if ftpuser1 uses over 976MB of space a warning will be issued. If the hard limit of 1TB has been reached the user will be unable to write any more data.

See #Specify a grace period to give users a specific amount of time to reduce storage usage when they hit their soft limit.

Warning: The hard limit applies to all files written by and for the respective user/group, including temporary files by started applications, which may crash at this point.

Specify a grace period

To give current users some time to reduce their file usage, a grace period can be configured. This specifies the allowed time a user/group can exceed their soft limit and while under their hard limit:

# edquota -t
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/sda3              7days                  7days

The grace period can be set in seconds, minutes, hours, days, weeks or months.

Reports

Shows all configured quotas:

# repquota -a

Shows quotas on a specific partition:

# repquota /home

Show quotas that apply to a user/group:

# quota -u user
# quota -g group

Copy quota settings

To one or several users

To copy quota settings from user1 to user2:

# edquota -p user1 user2

To copy quota settings to several other users, append user3 user4 ...

To groups

To copy quota settings from group1 to group2:

# edquota -g -p group1 group2

To all users

The idea is to modify the quota settings for one user and copy the setting to all other users. Set the quota for user1 and apply the quota to users with a UID greater than 999:

# edquota -p user1 $(awk -F: '$3 > 999 {print $1}' /etc/passwd)

Tips and tricks

Quota warnings

The command warnquota can be used to warn the users about their quota. Configuration is available in /etc/warnquota.conf.

Stats

The command quotastats can be used to give more information about the current quota usage:

$ quotastats
Number of dquot lookups: 101289
Number of dquot drops: 101271
Number of still active inodes with quota : 18
Number of dquot reads: 93
Number of dquot writes: 2077
Number of quotafile syncs: 134518740
Number of dquot cache hits: 7391
Number of allocated dquots: 90
Number of free dquots: 2036
Number of in use dquot entries (user/group): -1946

See also