[Tutorial] Debian and quota

Many times you’ll find yourself in a position that some users on your machine are using too much disk space and eventually leading into a crash due to low disk-space. Linux enables you to do something about this and luckily it’s very easy. This tutorial is also applicable to other Linux distro’s but I’ll stick to Debian because this is my favourite. You are supposed to have root access to be able to follow this tutorial.

First of all if you haven’t already done so:

apt-get install quota

Which will install the Debian support utility’s for managing quotas for users.

The next thing you will need to do. Is edit your /etc/fstab to enable quota on your file system.

You should add the ‘usrquota’ and ‘grpquota’ if you want to apply quota for groups, to your file system entry.

My fstab looks like the following:

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/hda3       /               ext3    noatime,usrquota,grpquota,errors=remount-ro 0       1
/dev/hda1       /boot           ext2    defaults        0       2
/dev/hda2       none            swap    sw              0       0

Note: there is also an option in the Debian installer to enable these options while installing your computer.

A second note: Also notice that you can decide on which partition you want quota! If you have a separate /home partition it would be wise to enable quota there instead of /.

Now to enable quota, you should issue the following commands:

touch /quota.user /quota.group
chmod 600 /quota.*
mount -o remount /
quotacheck -avugm
quotaon -avug

This will create the quota files for keeping track of the quota’s and it will remount your partition. Finally it enables quota’s! Of course you should note that this is an example for the partition /. Modifying it for /home should be obvious. If not, read a little bit about Linux partitioning on wikipedia

There is one other command which is really usefull because after doing this users can see how much space they have left:

chmod 664 /aquota.*

So much for the setup, now let’s go to the interesting stuff! Management.

There are a couple of commands to use for management:

repquota
The repquota command is a utility for reporting quota summary information.
 
SYNTAX:
repquota [ -u | -g ] [ -a | filesystem ]
The repquota command only displays user quotas by default, but you can specify that you wish to see group quotas by using the "-g" switch. You also need to specify the filesystem for which you wish to see a report, or you can specify the "-a" switch to see a report for all the filesystems for which quotas are enabled.
root@hephaistos:/# repquota -a
*** Report for user quotas on device /dev/hda3
Block grace time: 7days; Inode grace time: 7days
Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      -- 3991116       0       0         104009     0     0
daemon    --      52       0       0              4     0     0
man       --     596       0       0             16     0     0
news      --       4       0       0              1     0     0
www-data  -- 4356468       0       0           2105     0     0

The “block limits” refer to the data blocks (the default is 1 block = 1k). The “file limits” refer to the number of files, or inodes, that have been consumed. The “grace” field corresponds to the number of grace days remaining before the user is locked out of their account. The user has until that time to reduce their quota to below the “soft” level.

edquota
You can use the edquota command to edit user and group quotas.
 
SYNTAX:
edquota [ -u | -g ] <username or groupname>

For example, to edit the quota for “matthias”:

edquota matthias

Which will bring you an editor as specified in your profile, for me it’s vim:

Disk quotas for user matthias (uid 1000):
Filesystem                   blocks       soft       hard     inodes     soft     hard
/dev/hda3                  82424620  104857600  104857600      48139        0        0

The “blocks” and “inodes” fields cannot be edited, they are there for information purposes only. However, you can edit the soft and hard fields. The first pair refer to blocks, and the second to inodes.

As you can see I have a soft and a hard limit of 100G (1024 * 1024  * 100 = 104857600)

You can now save your changes if you made any, if you make an error you will be halted, so making mistakes is not easy, but you could set a lower limit than the size your files you currently posess, so you cannot create files anymore! So be careful when using this tool.

quota
Unlike repquota and edquota, which only the root user can make use of, the quota command is available to all users, and it allows them to query their current quota information.
 
SYNTAX:
quota [ -q ] [ username ]
root@hephaistos:/# quota matthias
Disk quotas for user matthias (uid 1000):
Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
/dev/hda3 82424620  104857600 104857600           48139       0       0

When a normal user calls ‘quota’ it will see its quota.

The -q option is for only showing quota when somebody exceeded it.

The ‘quota’ command is also a nice command to put in your /etc/profile, because it will show a user’s quota when a user logs in! Using the -q option is a nice solution to not always face the person with his quota but only when he/she is exceeding or nearly exceeding the quota.

So that’s about it with quota’s. I suggest you to create a test user to play with and setting low quota’s. In that way you could try to copy a large file to it’s home directory and you will see the file copy will fail because of quota limitations.

Quota is a powerful tool, but use with care!

Tags: , ,

Leave a Reply