How to implement Quotas on your system

From FedoraNEWS.ORG

Written by Anze Vidmar on 2005-09-18

Table of contents

About

Quota allows you to specify limits on two aspects of disk storage: the number of inodes a user or a group of users may possess; and the number of disk blocks that may be allocated to a user or a group of users.


Why do I need Quota?

Let's say you have a nice multi-user server and accounts on it for your friends/family members... Now, since you don't want this server to be running out of disk space, because all your users that are using your system are downloading stuff from the internet, you simply limit the disk space that they can use.


Preparations

Quotas are simple to setup and maintain. FC4 comes already with "built-in" support for Quota, so let's start configuring. In my case, I'll setup Quotas on /home part of the system, and I will limit user Dasa to only allow her use 4Mb of disk space.


Configuration

First, you need to edit the /etc/fstab file. Look in yout /etc/fstab file for line about /home


LABEL=/home             /home                   ext3    defaults               1 2


And add the "usrquota" to the options:


LABEL=/home             /home                   ext3    defaults,usrquota       1 2


Next, you need to remount the filesystem mounted on /home, for changes to take effect. This can be done with


[root@localhost ~]# mount -o remount /home


Next, you need to create the file in which the quota info will be written in. This is done by


[root@localhost ~]# touch /home/aquota.user
[root@localhost ~]# chmod 600 /home/aquota.user


Note, that if you like to setup group-based quotas, you also need to create aquota.group file. Remember, you must always create aquota file(s) on the uppermost directory of the filesystem, in our case /home.

Next, it's time to initialize the Quota table. This is done by


[root@localhost ~]# quotacheck -vagum


You will get an error complaining about missing file, but this is totaly normal for the 1st time Quota initialization. The error will look like this


[root@localhost ~]# quotacheck -vagum
quotacheck: WARNING -  Quotafile /home/aquota.user was probably truncated. Can't save quota settings...
quotacheck: Scanning /dev/hda5 [/home] done
quotacheck: Checked 4 directories and 26 files
[root@localhost ~]#


At this point, the Quota is up and running. Now it's time to define some disk limitations. For this purpose, the edquota tool is used.

Let's setup the Quota for user Dasa now:


[root@localhost ~]# edquota -u Dasa


The vi editor is starts and we end up with Quota table, looking like this


Disk quotas for user Dasa (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/hda5                        16         0          0         0        0         0


The first three objects (blocks, soft and hard) applies to Block Limits, and the second 3 objects (inodes, soft and hard) applies to File limits.

Since, we want user Dasa to be limited to the particular size (4Mb), we will edit Block lines. The first "Blocks" line tells us how much of the disk space for user Dasa is already taken. So all we need is to setup the hard limit.

Note that soft limit is used when you want to have grace time for Quota and hard limit is used when grace period is set to 0.

So, after editing the Quota table, our table looks like this


Disk quotas for user Dasa (uid 500):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/hda5                        16          0       4000          5        0        0


Note that values for Blocks are in kb (kilobytes). This is it, save the Quota table by writing the changes and exiting the vi editor.

If you want to use grace times, use the same tool with different switch. But keep in mind that in FC4 the deafault grace time is 7days.


[root@localhost ~]# edquota -t


The table looks like this


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/hda5                  7days               7days


So, if you don't want grace time, change number 7 to 0 and save the table.

After making any Quota changes, you don't need to restart anything, the changes are applied immediately.

You can check the Quota statistics any time with


[root@localhost ~]# repquota /home


As you can see, the Quota stats are very nice shown in the table


[root@localhost ~]# repquota /home/
*** Report for user quotas on device /dev/hda5
Block grace time: 00:00; Inode grace time: 00:00
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --   35436       0       0              5     0     0
anze      --      24       0    9000              3     0     0
basinac   --    2783       0   25000              56    0     0
Dasa      --      16       0    4000              5     0     0
Pokemon   --      16       0   50000              3     0     0
Izo       --      20       0  500000              4     0     0 


[root@localhost ~]#


Test

Now, let's login from ssh or console (or whatever you prefer) as user Dasa. When logged in, user Dasa can check her Quota limit by entering the command


[Dasa@localhost ~]$ quota


She will see something like this


[Dasa@localhost ~]$ quota
Disk quotas for user Dasa (uid 501):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/hda3      16      0    4000               5       0       0
[Dasa@localhost ~]$


Ok, this is correct. Now, let's test it, by downloading the xmms rpm file (that is 5Mb large) from rpmfind.net


[Dasa@localhost ~]$ wget ftp://rpmfind.net/linux/fedora/core/3/SRPMS/xmms-1.2.10-9.src.rpm


The file will start downloading, and if we set the Quota correct, it should cut-off the file that is downloading when size reaches to 4Mb.

This looks like this


[Dasa@localhost ~]$ wget ftp://rpmfind.net/linux/fedora/core/3/SRPMS/xmms-1.2.10-9.src.rpm
--02:06:11--  ftp://rpmfind.net/linux/fedora/core/3/SRPMS/xmms-1.2.10-9.src.rpm
           => `xmms-1.2.10-9.src.rpm'
Resolving rpmfind.net... 194.199.20.114, 195.220.108.108
Connecting to rpmfind.net[194.199.20.114]:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD /linux/fedora/core/3/SRPMS ... done.
==> PASV ... done.    ==> RETR xmms-1.2.10-9.src.rpm ... done.
Length: 5,290,751 (unauthoritative)
73% [==============================================================>                      ] 3,878,592      1.41M/s             
hda5: write failed, user block limit reached.
77% [=======================================================================>             ] 4,089,264      1.42M/s

xmms-1.2.10-9.src.rpm: Disk quota exceeded, closing control connection.
[Dasa@localhost ~]$


And if we check the space usage in her home folder, we will see that all 4M are used and no more fun for user Dasa.


[Dasa@localhost ~]$ du -h
4.0M    .
[Dasa@localhost ~]$

This is it. Quotas are great thing to implemenet on your multi-user box. Use it wisely :)

Personal tools