Managing disk space is crucial for maintaining a well-organized and optimized Linux system. By implementing disk quotas, you can restrict the amount of storage individual users or groups can consume on a filesystem. This ensures fair resource allocation, prevents abuse and helps maintain optimal system performance. In this beginner's guide, I will walk you through setting up user and group disk quotas on Linux filesystems, providing the necessary steps and insights to implement this important administrative task.
Why use disk quotas
Before diving into the technicalities of how to set up disk quotas on Linux, let's first understand the concept and benefits they offer. Disk quotas allow you to allocate a specific amount of disk space for users or groups, limiting their ability to consume excessive resources. By implementing disk quotas, you can achieve the following advantages:
Resource allocation: Disk quotas enable fair distribution of disk space among users or groups, ensuring that no single entity monopolizes system resources.
Preventing abuse: Quotas prevent users from filling up the filesystem with unnecessary or unwanted data, avoiding disk space wastage.
System optimization: By limiting the amount of storage users or groups can utilize, disk quotas help maintain optimal system performance, preventing disk saturation and potential performance degradation.
Configuring Disk Quotas
Now that you know what disk quotas are and why you might want to use them, let's move on to the practical aspects of configuring disk quotas.
Installing Quota Tools
To start managing disk quotas, you need to install the quota tools package on your system if it is not already installed.
To install disk quota on Debian or Ubuntu-based systems, use the following command:
$ sudo apt-get install quota
To install disk quota on Fedora, CentOS, and Red Hat use the following command:
$ sudo dnf install disk quota
To install disk quota on Arch Linux or any Arch-based systems use the following command:
$ sudo pacman -S quota-tools
Enabling Quotas
To enable quotas on a particular filesystem, we need to mount it with a few quota-related options. We do this by updating the filesystem's entry in the /etc/fstab configuration file.
Open the file using your favorite text editor:
$ sudo nano /etc/fstab
You should see something like this:-
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/3aa0120a-00f9-40d2-89d0-75c08f69204a / ext4 defaults 0 1
/swap.img none swap sw 0 0
/dev/sdb1 /mnt/quotadir ext4 defaults 0 2
Locate the line corresponding to the filesystem you want to enable quotas for. Add the usrquota and grpquota options to enable user and group quotas, respectively:
/dev/sdb1 /mnt/quotadir ext4 defaults,usrqouta,grpquota 0 2
Save and exit the file.
This modification will enable both user-based quotas (usrquota) and group-based quotas (grpquota) on the file system. If you only require one of these options, you can omit the unused one. In case your fstab line already contains additional options instead of the defaults, you should append the new options at the end, making sure to separate each option with a comma and no spaces.
Remounting the filesystem
After modifying the /etc/fstab file, remount the filesystem to apply the changes. Use the "mount -o remount
" command followed by the mount point or device name.
$ sudo mount -o remount /dev/sdb1
You can verify that the new filesystem mount options are being used by examining the /proc/mounts file. Here I used the grep command to show only the/dev/sdb1 filesystem entries in this file:
$ cat /proc/mounts | grep /dev/sdb1
Output:
/dev/sdb1 /mnt/quotadir ext4 rw,relatime,quota,usrquota,grpquota 0 0
Note the two options we have added.
Initializing Quota Indexes
Now that we have installed the tools and updated the file system options, we need to initialize the quota indexes for the filesystem. Use the command "quotacheck -cmug /path/to/filesystem
", replacing "/path/to/filesystem"
with the actual mount point or device name. In our case, this would be:
$ sudo quotacheck -cmug /mnt/quotadir
Let me break down the command for you: -
u
, specifies that a user-based quota file should be created.g
, indicates that a group-based quota file should be created.m
, disables remounting of the file system as a read-only. Although remounting the filesystem as read-only could provide more precise results in situations where users are actively saving files, it is not required for this initial setup.c
, creates a new quota index for the mount point. A quota index is a file that stores the quota limits for each user or group.
You can verify that the required files were created by listing the contents of the "/mnt/quotadir" directory.
$ ls -lah /mnt/quotadir/
Output:
total 24K
drwxrwxrwx 2 root root 4.0K May 21 23:14 .
drwxr-xr-x 3 root root 4.0K May 20 11:38 ..
-rw------- 1 root root 7.0K May 21 22:23 aquota.group
-rw------- 1 root root 7.0K May 21 22:23 aquota.user
Now run the following command to turn on the quota system:
Setting User quotas
To set quotas for individual users, use the edquota command together with the -u option followed by the username of the user that you would like to edit the quotas for. If you wish to set up group qoutas, use the -g option.
$ sudo edquota -u jame
This will open up a configuration file in your system’s default text editor:
Disk quotas for user james (uid 1002):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 0 0 0 0 0 0
Let me break down the meaning of each column:
The filesystems with quota enabled on them (in this case it’s /dev/sdb1)
The "blocks" column represents the current number of blocks (1k each) utilized by the user.
The "soft" column represents the maximum limit of 1k blocks that the user is allowed to use. This limit serves as a flexible threshold.
The "hard" column indicates the maximum limit of 1k blocks that the user is permitted to use. This limit is set as a strict boundary.
The "inodes" column displays the current count of inodes used by the user. Setting an inode-based quota would restrict the number of files and directories a user can create, irrespective of the disk space they consume. However, for most users, block-based quotas are preferred as they specifically limit the usage of disk space while allowing flexibility in the number of files and directories they can create.
In the "soft" column related to inodes, you can find the maximum limit of inodes that the user is allowed to use, serving as a flexible threshold.
Finally, the "hard" column denotes the maximum limit of inodes that the user is permitted to use, acting as a strict boundary.
In the list above, user James uses 0 blocks or 0 KB space on the /dev/sdb1 drive. Both soft and hard limits are disabled with a value of 0.
The displayed information clearly illustrates that each type of quota provides the option to set both a soft limit and a hard limit.
When a user surpasses the soft limit, they are considered over quota, but they are not immediately restricted from consuming additional space or inodes.
Instead, a certain level of flexibility is allowed. By default, users are given a grace period of seven days to bring their disk usage back under the soft limit. If, at the end of this grace period, the user remains above the soft limit, it will be treated as a hard limit.
Unlike the soft limit, the hard limit is less forgiving. It triggers immediate cessation of new block or inode creation as soon as the specified hard limit is reached. This behavior mimics a scenario where the disk is completely full: write operations fail, and the creation of temporary files is unsuccessful. The user encounters warnings and errors while performing routine tasks.
Now let's update our user Jame’s configuration to have a block quota with a 250MB soft limit, and a 3000 MB hard limit:
Disk quotas for user james (uid 1002):
Filesystem blocks soft hard inodes soft hard
/dev/sdb1 0 250MB 300MB 0 0 0
Once you are done save and exit the file. To check the new quota, use the quota command together with the -v
option to verbose and the -s
option to display data in human-readable format:
$ sudo quota -vs james
Output:
Disk quotas for user james (uid 1002):
Filesystem space quota limit grace files quota limit grace
/dev/sdb1 0K 250M 300M 0 0 0
Please note that if you wish to allow your users to check their quotas without requiring sudo access, you must grant them permission to read the quota files generated using the quotacheck command. One approach to achieve this is by creating a user group, setting the files' permissions to be readable by the user group, and ensuring that all users are members of the group as well.
Using the setquota command to set user quotas
Unlike the edquota command, setquota allows us to update our user's quota information in a single command, eliminating the need for interactive editing. In this command, we specify the username, the soft and hard limits for block-based quotas, and the filesystem to which the quota should be applied:
$ sudo setquota -u james 400M 500M 0 0 /dev/sdb1
By executing the above command, James's block-based quota limits will be increased to 400 megabytes for the soft limit and 500 megabytes for the hard limit. A value of 0 0 for the inode-based soft and hard limits indicates that they are not set. It is necessary to include these values, even if we are not setting any inode-based quotas.
To confirm that our changes have been applied, we can use the quota
command:
$ sudo quota -vs james
Disk quotas for user james (uid 1002):
Filesystem space quota limit grace files quota limit grace
/dev/sdb1 0K 400M 500M 0 0 0
Setting up a grace period
To configure the duration during which a user can exceed the soft limit, to do so we can utilize the setquota command as follows:
$ sudo setquota -t 345600 345600 /dev/sdb1
Executing the above command will set both the block and inode grace times to 345600 seconds, equivalent to 4 days. This configuration applies to all users, regardless of whether they utilize block or inode quotas. It is important to note that both values must be provided, even if only one type of quota is utilized.
Please keep in mind that the specified values should be in seconds.
To verify that the changes have been successfully implemented, you can use the repquota command:
$ sudo repquota -s /dev/sdb1
*** Report for user quotas on device /dev/sdb1
Block grace time: 4days; Inode grace time: 4days
Space limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 1280K 0K 0K 2 0 0
james -- 400M 400M 500M 1 0 0 </pre>
The repquota is used to generate a report on the current usage of quotas for all users on a specific filesystem. By utilizing the -s option, the repquota command presents the information in a human-readable format whenever possible. From the output note the "Block grace time: 4 days", along with the "grace" column. The grace column indicates the remaining time a user has to return under the soft limit if they have exceeded it.
Turning off disk quotas
To turn off disk quotas on a filesystem use the quotaoff command. Here is an example of turning off disk quotas on our /dev/sdb1
filesystem which is mounted at /mnt/qoutadir
:
$ sudo quotaoff -v /mnt/qoutadir
Conclusion
Implementing disk quotas on Linux filesystems is a crucial step in managing disk space effectively and efficiently. By following the step-by-step guide provided in this beginner's guide, you can easily set up user and group quotas, ensuring fair resource allocation, preventing abuse, and maintaining optimal system performance. Start implementing disk quotas today to enhance the organization and performance of your Linux system.