Linux Disk Quotas: How to Configure and Manage User Quotas
It is possible to enable quotas on Linux to better manage the use of available disk space. This feature is especially important in multi-user environments, web servers, file servers, mail servers, etc.
When managing quotas, it is possible to define the amount of disk space for each user or user group.
You need to enable quota management before using this feature.
Follow the steps below:
1) You need to have quota support compiled in the Kernel.
2) Edit the /etc/fstab file and add quota management for user (usrquota) and for group (grpquota) on the desired device.
$ vi /etc/fstab
/dev/hda6 /home ext2 defaults,usrquota,grpquota 1 2
3) Create the quota.user and quota.group files in the /home directory with the "quotacheck –cug" command and set read and write permissions only for the superuser. In some distributions these files are named aquota.user and aquota.group. These two files will be the databases for user and group quotas respectively.
To create quota files manually:
$ touch /home/quota.user
$ touch /home/quota.group
$ chmod 600 /home/quota.user
$ chmod 600 /home/quota.group
Or
$ quotacheck -cug
4) Run the quotacheck –avug command to initialize the newly created database.
5) Verify that the database was initialized. The quota.user and quota.group files should not have zero size.
$ ls –lga /home
6) Enable the quota service:
$ quotaon -a
7) Make sure the quota service is enabled at boot. If the file /etc/rc.d/quotas does not exist, create a script file:
$ vi /etc/rc.d/quotas
#!/bin/bash
/sbin/quotaon -avug
Change the permissions to 755:
$ chmod 755 /etc/rc.d/quotas
Create the symbolic link to the quotas file for runlevel 3 and runlevel 5:
$ ln –s /etc/rc.d/quotas /etc/rc.d/rc3.d/S20quotas
$ ln –s /etc/rc.d/quotas /etc/rc.d/rc5.d/S20quotas
8) Run a quota system check once per week by putting the quotacheck command in root's cron:
$ crontab –e
0 3 * * 0 /sbin/quotacheck –avug
Once these steps are done, the quota system can be used.
There are four types of quota limits as follows:
- User Hard Limit: The hard limit is the total amount of disk space a user can have. The user will not be able to write anything else when this limit is reached;
- User Soft Limit: The soft limit works as a warning area. When this limit is reached the system warns the user that their disk space is running out. The user will be able to write to disk until the hard limit is reached;
- Group Hard Limit: The group hard limit defines the maximum disk space a user group can have. If this limit is reached, users in the group will no longer be able to write even if there is space available for a specific user;
- Group Soft Limit: It has the same concept as the group hard limit, but works as a warning area.
We can also configure a Grace Period in quota management so that a user or group that has reached the soft limit can delete some files and get back within the established limits.
Once the time specified in the grace period has passed and no action is taken, the soft limit becomes the hard limit, preventing users from working until the situation is actually resolved. This limit can be in minutes, hours, days, weeks, and even months.
Now let us look at the commands for quota management:
Command quota
The quota command shows the disk space quotas configured for a specific user or group and the space used.
Its most frequent options are:
- -u: Shows the quota defined for a specific user (default);
- -g: Shows the quota defined for a specific group;
- -q: Shows only when the quota is exceeded;
- -v: Shows the quotas even if none are defined.
Example:
$ quota –v uira
Disk quota for user uira (uid 502):Filesystem blks quota limit grace files quota limit grace/dev/hda6 18320 19000 20000 58 0 0
In this example user uira is using 18320 blocks. The soft limit is 19000 blocks and the hard limit is 20000 blocks.
Only the superuser can view other users' quotas. A regular user can view their own quota and the quota of the group they are a member of.
Command quotaon
The quotaon command enables quota management on a previously configured device.
Its most frequent options are:
- -a: Enables quota management for all devices configured for quota control in the /etc/fstab file;
- -g: Enables quota management for groups on a specific device;
- -u: Enables quota management for users on a specific device;
- -v: Shows all devices where quota management was enabled.
Example:
$ quotaon –avug
Enables user and group quotas for all devices configured for this purpose in /etc/fstab.
Command quotaoff
This command disables quota management on specific devices.
The options for this command are similar to the quotaon command.
Example:
$ quotaoff –gu /dev/hda6
Command quotacheck
This command scans a specific device and builds the quota database files. It is important that this command be run regularly so that the quota database is faithful to the reality of the file system.
The options for this command are:
- -a: Scans all devices configured for quota management in the /etc/fstab file and builds the database for users and groups;
- -g group: Builds the quota database for a specific group;
- -u user: Builds the quota database for a specific user;
- -v: Shows all procedures executed by quotacheck.
Example:
$ quotaoff –a
$ quotacheck –aug
$ quotaon
If you do not disable quotas for a specific device when running the quotacheck command, the collected information will remain in memory, but the files with the database will not be updated.
edquota command
The edquota command edits quotas for users and groups. It uses the vi text editor or another default text editor to configure disk quotas. The quota configuration file may look a bit strange, but its configuration is simple.
The available options for the command are:
- -g: If the –g option is specified, edquota should receive the group name as a parameter;
- -u: This is the default option, to edit user quotas;
- -p exampleuser: This option is called a prototype user. You can use this option to copy the settings from a default user to another user;
- -t: This option is used to configure the grace period for users or a specific group. Must be combined with the –u or –g option.
Example:
$ edquota –u uira
Quotas for user: uira/dev/hda6: blocks in use: 136, limits (soft = 19000, hard = 20000)Inodes in use: 132, limits (soft = 0, hard = 0)
In this example the soft limit is 19000 blocks and the hard limit is 20000.
$ edquota –tu
Time units may be: days, hours, minutes, or secondsGrace period before enforcing soft limits for users:/dev/hda6: block grace period: 15 days.File grace period: 7 days
In this example the grace period is 15 days for blocks and 7 days for files (inodes).
repquota command
This command creates a report of quotas and disk usage for users on a specific device or group.
The options for this command are:
- -a: Creates a quota report for all devices configured for quota management in the /etc/fstab file;
- -u: This is the default option, which creates a usage report per user;
- -g: This option reports quota usage by group;
- -v: Creates a descriptive header for the report.
Example:
$ repquota –va
*** Report for user quotas on /dev/hda6 (/home)Block limits File limitsUser used soft hard grace used soft hard graceroot 718000 0 0 418 0 0
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Enjoyed it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.