Setting up quotas for users/groups on a Linux server

Last updated on September 10th, 2016

v1.0, Jan 21, 2007

This document provides ‘step-by-step’ instructions for setting up quotas on Linux — which allows you to limit disk space to users and/or groups.

This document assumes that you have the quota software installed on your Linux box; if not, download and install the latest version:

Linux DiskQuota: http://sourceforge.net/projects/linuxquota/

 

Contents

  1. Creating a new virtual file system.
  2. Enabling quotas on the file system.
  3. Setting up groups.
  4. Set quotas for a group or a user.
  5. Enabling quota checks.
  6. Checking quotas.
  7. Sending e-mail to users over quota.

Creating a new virtual file system

If you already have a file system that you wish to enable quotas on (e.g. ‘/home’, or ‘/usr’), you can skip this section; howerver, if you wish to enable quotas for a specific directory, for example ‘/usr/share/downloads’, but you do not wish to enable quotas for the entire ‘/usr’ file system, you can create a new virtual file system as follows:

1) Log in as root.

$ su root

2) Create the directory.

# mkdir -p /usr/share/downloads/

3) Create a new disk image.

Create a directory to hold the disk image (this can be created wherever you choose):

# mkdir -p /usr/downloads-image/

Create the image file within the disk image directory:

# dd if=/dev/zero of=/usr/downloads-image/downloads count=102400

The dd command above creates a disk image that is 50Mb in size — as specified by the ‘count’ parameter. The dd command uses 512 bytes for each block; so, once you have calculated the required size in bytes you need to divide it by 512:

50Mb = 52428800 bytes
52428800 / 512 = 102400

Just for example, 1Gb would be:

1Gb = 1073741824 bytes
1073741824 / 512 = 2097152

4) Format the disk image.

The image that was created needs to be formatted as a file system:

# mkfs -t ext3 -q /usr/downloads-image/downloads -F

The file system’s type is specified by ‘-t’ (ext3 in this case), ‘-q’ specifies the device, and ‘-F’ forces creation without confirmation messages.

Enabling quotas on the file system.

To ensure that your quotas remain enabled on any file system, you will need to make changes in /etc/fstab. Quotas can be enabled without having to reboot the computer.

1) Log in as root (if you have not already done so).

$ su root

2) Edit /etc/fstab.

If you wish to enable quotas on an existing file system such as ‘/home’, for example, add the options ‘usrquota,grpquota’ as follows:

Existing line:

LABEL=/home          /home                   ext3    defaults        1 2

Edited line:

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

If you have created a new file system — ‘/usr/share/downloads’ for example — you need to add it here as follows:

/usr/downloads-image/downloads   /usr/share/downloads   ext3    rw,loop,usrquota,
grpquota   0   0

3) Mount the file system.

If the file system is already mounted, such as ‘/home’, you need to remount it:

# mount -o remount /home

If you have created a new filesystem that has not yet been mounted, such as ‘/usr/share/downloads’, type:

# mount /usr/share/downloads

4) Create the database files.

File required for quotas, named ‘aquota.user’ and ‘aquota.group’, need to be created in the file system you wish to enable quotas for:

# quotacheck -cug /usr/share/downloads

Setting up groups.

If you wish to set a quota for a particular group, rather than an individual, do the following:

1) Create the group.

If you do not currently have a group that you wish to assign a quota for, create the group by the following command:

# groupadd quotagrp

Obviously, you can name the group whatever you like. The above command creates a new group named ‘quotagrp’.

2) Add existing users to the ‘quotagrp’ group.

If you wish to add users to the group, to find out what group(s) a particular user is already part of, you can use the ‘groups’ command, followed by the user name:

# groups fred
fred : fred staff admin

The above example shows user fred as belonging to groups ‘fred’, ‘staff’ and ‘admin’.

If user ‘fred’ is not part of any existing groups, do the following:

# usermod -G quotagrp fred

If user ‘fred’ is currently part of groups ‘staff’ and ‘admin’, do the following:

# usermod -G quotagrp,staff,admin fred

Note: groups are seperated by commas, without spaces!

If the user belongs to other groups and you do not specify them then the user is removed from these groups. A user is not removed from a group that is the same as their user name, so you do not need to specify this.

3) Set access rights for the ‘quotagrp’ group.

# chown -R root.quotagrp /usr/share/downloads

The above command gives the user group ‘quotagrp’ access rights to ‘/usr/share/downloads’. ‘-R’ specifies that access rights are also given to any files or directories currently beneath this directory.

# chmod 775 /usr/share/downloads

# chmod g+s /usr/share/downloads

Note: ‘chmod g+s’ sets the ‘Group ID’ (GID) for the directory, meaning that any new files/directories that are created in the directory will be of the same group as the directory itself.

Any user currently logged in only becomes part of the group once they have logged out and logged back in again.

Set quotas for a group or a user.

You can limit the size of the data (blocks) that can be stored within the file system, and also the number of files (inodes) that can be created. For both data and files you can set a ‘soft limit’ and a ‘hard limit’.

Blocks – the size of data within the directory – soft limit = 49000k (49Mb), hard limit = 50000k (50Mb)
Inodes – the number of files/directories within the directory – soft limit = 1000 files, hard limit = 1100 files

The soft limit is the maximum allowed space and the hard limit is the maximum allowed space given for a grace period of 7 days.

If any of the values are set to ‘0’ then no limit is set.

1) Setting quotas for a group.

If you wish to set a quota for users belonging to a particular group, do the following:

# setquota -g quotagrp 49000 50000 1000 1100 -a /dev/loop0

The above specifies that the group ‘quotagrp’ has the following allowances:

49Mb of data is allowed, with 50Mb allowed for a grace period of 7 days.
1000 files are allowed, with 1100 files allowed for a grace period of 7 days.

2) Setting quotas for users.

If you wish to set a quota for an individual user, do the following:

# setquota -u fred 49000 50000 0 0 -a /dev/loop0

The above specifies that the user ‘fred’ has the following allowances:

49Mb of data is allowed, with 50Mb allowed for a grace period of 7 days.
An unlimited number of files are allowed.

3) Changing the grace period.

By default the grace period is 7 days.

If you wish to change the grace period for a group, do the following:

# setquota -g quotagrp -T 604800 0 -a /dev/loop0

If you wish to change the grace period for a user, do the following:

# setquota -u fred -T 604800 0 -a /dev/loop0

The two values (604800 and 0) represent the block and inode grace period, respectively, specified in seconds. 7 days = 604800 seconds.

Enabling quota checks.

For each file system that you wish to enable quota checks on do the following:

# quotaon /usr/share/downloads

Note: Quotas can be disabled by using the ‘quotaoff’ command in the same way.

Checking quotas.

To see a report for the file system type:

# repquota -vugsi /usr/share/downloads

This should show all user and group quotas. Use ‘-vusi’ for user reports only, or ‘-vgsi’ for group reports only.

An example group report on an empty file system should look like this:

*** Report for group quotas on device /dev/loop0
Block grace time: 7days; Inode grace time: 00:00
                        Block limits                File limits
Group           used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --    4924       0       0              4     0     0
quotagrp  --       0   49000   50000              0     0     0

Statistics:
Total blocks: 7
Data blocks: 1
Entries: 2
Used average: 2.000000

The two ‘-‘ signs after the user/group name represent the status of the block and file quota respectively. A ‘-‘ signifies that the user/group is under quota, and a ‘+’ sign signifies that the user/group is over quota.

If group ‘quotagrp’ exceeded its quota you should see something like the following:

                        Block limits                File limits
Group           used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --    4924       0       0              4     0     0
quotagrp  +-   49108   49000   50000  7days       3     0     0

The ‘+-‘ signifies that group ‘quotagrp’ is over block quota, but not file quota.

Sending e-mail to users over quota.

1) Set the e-mail address for a group.

Edit ‘/etc/quotagrpadmins’ with a text editor, and add a line in the following format:

:

For example:

quotagrp: me@my-domain.com

This specifies the user that has been assigned as administrator for the group ‘quotagrp’.

2) Add file systems for quota checking.

Edit ‘/etc/quotatab’ and add a line in the following format:

:

For example:

/usr/share/downloads: downloads directory

3) Create warning messages.

Edit ‘/etc/warnquota.conf’ and change the following:

change to suit:

MAIL_CMD        = "/usr/sbin/sendmail -t"
FROM            = "you@your-domain.com"
SUBJECT         = NOTE: You are exceeding your allocated disk space limits

If you would like a copy of any warning e-mails sent to your address, specify it using the following parameter:

CC_TO             = "you@your-domain.com"
MESSAGE         = Your disk usage has exceeded the agreed limits\
 on this server.|Please delete any unnecessary files on
 the following filesystems:|
SIGNATURE       = Thank you.|                   Administrator.|
GROUP_MESSAGE   = The disk usage for group '%s', which you are administrator of,
 has exceeded the agreed limits.|\
Please delete any unnecessary files on the following filesystems:|
GROUP_SIGNATURE = Thank you.|                   Administrator.|

4) Run a check.

# warnquota -g /usr/share/downloads

This checks whether the directory is over quota for the group, and if so e-mails the group admin. It can be ran manually or added to crontab to be run automatically.