Kelson Martins Blog

Linux disk quotas – Introduction

Disk Quotas is an interesting limitation tool provided by Linux that can control disk space used by individual users.
On Linux, disk quota works by managing 2 components, blocks, and inodes:
Blocks: The amount of space that a user is currently using.
Inodes: The number of files a user is currently using.
Saying that on this post we will be focusing on managing blocks for the simple reason that managing inodes is not very popular. The reason for that is that managing inodes do not give us much control. For example, if we allow 100 inodes for a user, he would hypothetically be able to create 100 files of 1GB or more.
When setting quotas, we also have to manage the following:
Soft Limit: Is a limit that triggers a limit when a user hits the limit. The user does not get blocked but the warning “disk quota exceeded” makes the user aware of the situation, giving it time to clean it up.
Hard Limit: Is a limit that blocks the user from creating further files. Once hit, the user is unable to create files and there is nothing that he can do.
Grace Time: Is a time period that allows the user to create extra files even after they hit their configured soft limit, up to the allowed hard limit.

Using Disk Quotas

By default, quota application usually does not come installed on Linux by default.
To install it, you can perform the following:
apt-get install quota # Ubuntu
yum install quota # RHEL/CentOS 
With quota installed, next step is to add quota capabilities on a partition.
On my machine, I have a partition /dev/sdb1 mounted on /data, which I will be using for demonstration purposes. You can follow along with any partition available to you.
My partition can be seen as follows:
kelson@kelson-xubuntu:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            981M     0  981M   0% /dev
tmpfs           201M  3.6M  197M   2% /run
/dev/sda1        18G  4.7G   13G  28% /
tmpfs          1001M  220K 1000M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
/dev/sdb1       991M  1.3M  923M   1% /data
tmpfs           201M   40K  200M   1% /run/user/1000
To add quota capabilities in a partition, we must edit /etc/fstab.
Note that usrquota and grpquota statements are added. We are interested in users quota but group quota was added for awareness purposes.
/dev/sdb1       /data           ext4    defaults,usrquota,grpquota 1 2

Next. we perform the command quotacheck, which will scan a filesystem for disk usage, create, check and repair quota files.

kelson@kelson-xubuntu:~$ quotacheck -mavug
quotacheck: Scanning /dev/sdb1 [/data] done
quotacheck: Cannot stat old user quota file /data/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /data/ No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old user quota file /data/aquota.user: No such file or directory. Usage will not be subtracted.
quotacheck: Cannot stat old group quota file /data/ No such file or directory. Usage will not be subtracted.

-mavug parameters stand for:

-m, --no-remount
-a, --all
    Check all mounted non-NFS filesystems in /etc/mtab
-v, --verbose
    quotacheck reports its operation as it progresses.  Normally it operates silently.  If the option is specified twice, also the current directory is printed (note that printing can slow down the scan measurably).
-u, --user
     Only user quotas listed in /etc/mtab or on the filesystems specified are to be checked.  This is the default action.
-g, --group

We can then check our /data directory, where quota files are now created.

drwxr-xr-x  3 root root  4096 Nov 16 16:06 ./
drwxr-xr-x 26 root root  4096 Nov  2 15:44 ../
-rw-------  1 root root  6144 Nov  2 15:55
-rw-------  1 root root  7168 Nov  2 15:55 aquota.user

Quota Commands

Moving to the interesting stuff, let’s manipulate quota settings for particular users.
To visualize quota settings for a user we may perform:
root@kelson-xubuntu:/data# quota -vu kelson
Disk quotas for user kelson (uid 1000):
     Filesystem  blocks   quota   limit   grace   files   quota   limit   grace
      /dev/sdb1       0       0       0               0       0       0     
The quota –vu command checks quota in verbose mode for a particular user, kelson in my case.
Now, lets edit quota for my user. For that, we can use the edquota command.
root@kelson-xubuntu:/data# edquota -u kelson
Disk quotas for user kelson (uid 1000):
  Filesystem                   blocks       soft       hard     inodes     soft     hard
  /dev/sdb1                         0          10000          10000          0        0        0             
Note that I have set the soft and hard limit to 100000, meaning that the user will be able to create 100000 blocks of 1KB each, translating into a 100MB limit.
Now, let’s test our quota limits. For that let’s perform the following:
root@kelson-xubuntu:/# quotaon -a root@kelson-xubuntu:/# su kelson
kelson@kelson-xubuntu:/$ dd if=/dev/zero of=/data/bigfile bs=100M count=3
dd: error writing '/data/bigfile': Disk quota exceeded
1+0 records in
0+0 records out
102400000 bytes (102 MB, 98 MiB) cop
Firstly, we ensure that quota controls are enabled on all filesystems. This is done through the quotaon -a command.
Then, I switch to a user where quotas have been added and then I use the dd command to try to create 3 files with 100MB each.
Note that we get quota exceeded warning message.
Just to confirm, let’s now create a file with 80MB, which should be OK for my user to create.
kelson@kelson-xubuntu:/$ dd if=/dev/zero of=/data/bigfile bs=80M count=1
1+0 records in
1+0 records out
83886080 bytes (84 MB, 80 MiB) copied, 0.0653215 s, 1.3 GB/s
kelson@kelson-xubuntu:/$ cd /data/
kelson@kelson-xubuntu:/data$ ls -lh
total 81M
-rw------- 1 root   root   7.0K Nov 16 16:28
-rw------- 1 root   root   7.0K Nov 16 16:28 aquota.user
-rw-rw-r-- 1 kelson kelson  80M Nov 17 11:29 bigfile
drwx------ 2 root   root    16K Nov  2 15:34 lost+found
As expected, we were able to create a file with a size up to 100MB.
Now, let’s use the command repquota, which summarize quota on a filesystem.
root@kelson-xubuntu:/data# repquota -au
*** Report for user quotas on device /dev/sdb1
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
root      --      20       0       0              2     0     0       
kelson    --   81920  100000  100000              1     0     0       
Note that the parameters a (all) and u (user) were used to summarize user quotas. Parameter g (group) could also be used to summarize group quota but in our example, user quota is enough.
Also note that after creating the 80MB file the repquota command informs us that 81920 blocks are used, out of the 100000 limit.

Other Quota Commands

Some other useful quota commands that were not approached on the post are:
# copy quota settings from user kelson to peter
root@kelson-xubuntu:/# edquota -p kelson peter

# copy quota settings from user kelson to all users with a UID greater than 999
root@kelson-xubuntu:/# edquota -p kelson $(awk -F: '$3 > 999 {print $1}' /etc/passwd)

# edit default grace time (period in which a user/group can exceed their soft limit but still staying below the hard limit). This setting has no effect if both soft and hard limits are set to the same value.
root@kelson-xubuntu:/# edquota -t
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/sdb1                     7days                  7days

# stop quota management on all filesystems
quotaoff -a


This post presented you with the basic commands and usage of disk quota Linux tool. The content provided here would also be valuable if you are studying for the LPIC-1 exam, where you will possibly encounter disk quota questions.
There are many other scenarios that were not covered here but you can dive into the manual page to learn more about different usage options.
Hopefully, this post was useful and do not hesitate on adding comments, critics or any other observation.
Stay tuned!

Software engineer, geek, traveler, wannabe athlete and a lifelong learner. Works at @IBM

Next Post